Add network test command

This commit is contained in:
Ben Adrian Sarmiento
2024-06-16 09:47:29 +02:00
parent f4d3f273f6
commit 42a08583ce
7 changed files with 60 additions and 47 deletions

View File

@@ -14,7 +14,6 @@ import (
"strings"
"time"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/pkg/logutil"
http_dialer "github.com/mwitkow/go-http-dialer"
"golang.org/x/net/proxy"
@@ -23,15 +22,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]
optimalHosts []string
log *logutil.Logger
client *http.Client
maxRetries int
timeoutSecs int
rateLimitSleepSecs int
backoff func(attempt int) time.Duration
bearerToken string
dnsCache cmap.ConcurrentMap[string, string]
optimalHosts []string
log *logutil.Logger
}
type ApiErrorResponse struct {
@@ -49,26 +48,26 @@ func NewHTTPClient(
timeoutSecs int,
forceIPv6 bool,
optimalHosts []string,
cfg config.ConfigInterface,
proxyURL string,
log *logutil.Logger,
) *HTTPClient {
client := HTTPClient{
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
backoff: backoffFunc,
cfg: cfg,
dnsCache: cmap.New[string](),
optimalHosts: optimalHosts,
log: log,
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
rateLimitSleepSecs: 4,
backoff: backoffFunc,
dnsCache: cmap.New[string](),
optimalHosts: optimalHosts,
log: log,
}
var dialer proxy.Dialer = &net.Dialer{
Timeout: time.Duration(timeoutSecs) * time.Second, // timeout for dns resolution, tcp handshake
}
if proxyURLString := cfg.GetProxy(); proxyURLString != "" {
if proxyURLString := proxyURL; proxyURLString != "" {
proxyURL, err := url.Parse(proxyURLString)
if err != nil {
log.Errorf("Failed to parse proxy URL: %v", err)
@@ -81,15 +80,10 @@ func NewHTTPClient(
}
}
maxConnections := cfg.GetNumOfWorkers()
if maxConnections > 32 {
maxConnections = 32 // real-debrid has a limit of 32 connections per server/host
}
client.client.Transport = &http.Transport{
ResponseHeaderTimeout: time.Duration(timeoutSecs) * time.Second,
MaxIdleConns: 0,
MaxConnsPerHost: maxConnections,
MaxConnsPerHost: 32,
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.Dial(network, address)
},
@@ -168,7 +162,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
}
}
incr := r.shouldRetry(req, resp, err, r.cfg.GetRateLimitSleepSecs())
incr := r.shouldRetry(req, resp, err, r.rateLimitSleepSecs)
if incr > 0 {
attempt += incr
if attempt > r.maxRetries {