From 4bf60486e0135e53dcca26a71dc8edfe9635e8f6 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 17 Nov 2022 00:05:09 +0300 Subject: [PATCH] add context dialer support Signed-off-by: Vasiliy Tolstov --- http.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/http.go b/http.go index 3b1fe7d..800eab6 100644 --- a/http.go +++ b/http.go @@ -692,11 +692,21 @@ func NewClient(opts ...client.Option) client.Client { opts: options, } - dialer, ok := options.Context.Value(httpDialerKey{}).(*net.Dialer) - if !ok { - dialer = &net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, + var dialer func(context.Context, string) (net.Conn, error) + if v, ok := options.Context.Value(httpDialerKey{}).(*net.Dialer); ok { + dialer = func(ctx context.Context, addr string) (net.Conn, error) { + return v.DialContext(ctx, "tcp", addr) + } + } + if options.ContextDialer != nil { + dialer = options.ContextDialer + } + if dialer == nil { + dialer = func(ctx context.Context, addr string) (net.Conn, error) { + return (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext(ctx, "tcp", addr) } } @@ -705,8 +715,10 @@ func NewClient(opts ...client.Option) client.Client { } else { // TODO customTransport := http.DefaultTransport.(*http.Transport).Clone() tr := &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DialContext: dialer.DialContext, + Proxy: http.ProxyFromEnvironment, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + return dialer(ctx, addr) + }, ForceAttemptHTTP2: true, MaxConnsPerHost: 100, MaxIdleConns: 20,