Optimize network hosts

This commit is contained in:
Ben Adrian Sarmiento
2024-06-16 09:14:12 +02:00
parent 5aea569be7
commit f4d3f273f6
3 changed files with 88 additions and 47 deletions

View File

@@ -23,15 +23,15 @@ import (
)
type HTTPClient struct {
client *http.Client
maxRetries int
timeoutSecs int
backoff func(attempt int) time.Duration
bearerToken string
cfg config.ConfigInterface
dnsCache cmap.ConcurrentMap[string, string]
ipv6Hosts []string
log *logutil.Logger
client *http.Client
maxRetries int
timeoutSecs int
backoff func(attempt int) time.Duration
bearerToken string
cfg config.ConfigInterface
dnsCache cmap.ConcurrentMap[string, string]
optimalHosts []string
log *logutil.Logger
}
type ApiErrorResponse struct {
@@ -48,19 +48,20 @@ func NewHTTPClient(
maxRetries int,
timeoutSecs int,
forceIPv6 bool,
optimalHosts []string,
cfg config.ConfigInterface,
log *logutil.Logger,
) *HTTPClient {
client := HTTPClient{
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
backoff: backoffFunc,
cfg: cfg,
dnsCache: cmap.New[string](),
ipv6Hosts: []string{},
log: log,
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
backoff: backoffFunc,
cfg: cfg,
dnsCache: cmap.New[string](),
optimalHosts: optimalHosts,
log: log,
}
var dialer proxy.Dialer = &net.Dialer{
@@ -148,7 +149,9 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
resp.Body.Close()
}
// r.optimizeHost(req)
if len(r.optimalHosts) > 0 {
r.optimizeHost(req)
}
resp, err = r.client.Do(req)
@@ -188,23 +191,11 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
}
func (r *HTTPClient) optimizeHost(req *http.Request) {
if !strings.HasSuffix(req.Host, ".download.real-debrid.com") {
if !strings.Contains(req.Host, ".download.real-debrid.") {
return
}
req.Host = strings.Replace(req.Host, ".com", ".cloud", 1)
req.Host = r.optimalHosts[rand.Intn(len(r.optimalHosts))]
req.URL.Host = req.Host
// check if this host is in the list of IPv6 hosts
for _, h := range r.ipv6Hosts {
if h == req.Host {
return
}
}
// if not then just assign a random IPv6 host
req.Host = r.ipv6Hosts[rand.Intn(len(r.ipv6Hosts))]
req.URL.Host = req.Host
r.log.Debugf("Host %s is not a valid IPv6 host, assigning a random IPv6 host: %s", req.URL.Host, req.Host)
}
func (r *HTTPClient) proxyDialer(proxyURL *url.URL) (proxy.Dialer, error) {