From 37e97f9e7e0ffafdd10364cb47e10e7f7d8a5037 Mon Sep 17 00:00:00 2001 From: Javad Date: Thu, 10 Nov 2022 10:59:55 +0330 Subject: [PATCH] add: download and default client for gvm --- cli/client.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 cli/client.go diff --git a/cli/client.go b/cli/client.go new file mode 100644 index 0000000..1ef0327 --- /dev/null +++ b/cli/client.go @@ -0,0 +1,54 @@ +package cli + +import ( + "context" + "net" + "net/http" + "runtime" + "time" +) + +const _defaultConcurrentDialsPerCPU = 5 << 1 + +var ( + _defaultTimeOut = 30 * time.Second + _defaultKeepAlive = 30 * time.Second +) + +type dialRateLimiter struct { + dialer *net.Dialer + sem chan struct{} +} + +func DownloadClient(maxIdleConnsPerHost int) *http.Client { + transport := http.DefaultTransport.(*http.Transport).Clone() + dialer := rateLimiter(&net.Dialer{ + Timeout: _defaultTimeOut, + KeepAlive: _defaultKeepAlive, + }) + + transport.DialContext = dialer.DialContext + transport.MaxIdleConns = 0 // not limit + transport.MaxIdleConnsPerHost = maxIdleConnsPerHost + transport.DisableCompression = true + return &http.Client{ + Transport: transport, + } +} + +func DefaultClient() *http.Client { + return http.DefaultClient +} + +func rateLimiter(dialer *net.Dialer) *dialRateLimiter { + return &dialRateLimiter{ + dialer: dialer, + sem: make(chan struct{}, _defaultConcurrentDialsPerCPU*runtime.NumCPU()), + } +} + +func (d *dialRateLimiter) DialContext(ctx context.Context, network, address string) (net.Conn, error) { + d.sem <- struct{}{} + defer func() { <-d.sem }() + return d.dialer.DialContext(ctx, network, address) +}