Add network test command
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -53,12 +53,15 @@ func (r *IPRepository) NetworkTest(forceRun bool) {
|
||||
}
|
||||
if ipv4Loaded && ipv6Loaded {
|
||||
return
|
||||
} else {
|
||||
r.log.Warn("Network test files not found")
|
||||
}
|
||||
}
|
||||
|
||||
r.log.Info("Network test will start now (this will only run once). IGNORE THE WARNINGS!")
|
||||
r.log.Info("Network test will start now. IGNORE THE WARNINGS!")
|
||||
r.runLatencyTest()
|
||||
r.log.Infof("Network test completed. Saving the results to %s and %s", ipv4latencyFile, ipv6latencyFile)
|
||||
r.log.Info("Network test completed!")
|
||||
r.log.Infof("To rerun the network test, run 'zurg network-test', or delete the files %s and %s and run zurg again", ipv4latencyFile, ipv6latencyFile)
|
||||
r.writeLatencyFile(ipv4latencyFile, r.ipv4latencyMap)
|
||||
r.writeLatencyFile(ipv6latencyFile, r.ipv6latencyMap)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user