From 02f29b0ef360efd9ade4ee2509678fcc044ea82d Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 26 Mar 2021 15:48:39 +0300 Subject: [PATCH] fix tls issues Signed-off-by: Vasiliy Tolstov --- go.mod | 2 +- go.sum | 4 ++-- http.go | 29 +++++++++++++++++++++-------- options.go | 12 ++---------- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 1fa81ee..c612953 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/unistack-org/micro-client-http/v3 go 1.16 -require github.com/unistack-org/micro/v3 v3.3.2 +require github.com/unistack-org/micro/v3 v3.3.3 diff --git a/go.sum b/go.sum index c8744e1..74ab27a 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= -github.com/unistack-org/micro/v3 v3.3.2 h1:1r7fmaobJVZBKDhIewSBy8R4H/6YazQFetsDgULV6Mw= -github.com/unistack-org/micro/v3 v3.3.2/go.mod h1:tX95c0Qx4w6oqU7qKThs9lya9P507BdZ29MsTVDmU6w= +github.com/unistack-org/micro/v3 v3.3.3 h1:Igkzl8tWPlIacEK9z8hHVIzhdyzi8drQPt0Am2iHAcA= +github.com/unistack-org/micro/v3 v3.3.3/go.mod h1:tX95c0Qx4w6oqU7qKThs9lya9P507BdZ29MsTVDmU6w= golang.org/x/net v0.0.0-20210324205630-d1beb07c2056/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/http.go b/http.go index 1190dcc..c65dfe6 100644 --- a/http.go +++ b/http.go @@ -190,7 +190,7 @@ func (h *httpClient) stream(ctx context.Context, addr string, req client.Request if err == nil && u.Scheme != "" && u.Host != "" { dialAddr = u.Host } - cc, err := h.dialer.DialContext(ctx, "tcp", addr) + cc, err := (h.httpcli.Transport).(*http.Transport).DialContext(ctx, "tcp", addr) if err != nil { return nil, errors.InternalServerError("go.micro.client", fmt.Sprintf("Error dialing: %v", err)) } @@ -587,17 +587,30 @@ 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, + } + } + if httpcli, ok := options.Context.Value(httpClientKey{}).(*http.Client); ok { rc.httpcli = httpcli } else { - rc.httpcli = http.DefaultClient + tr := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: dialer.DialContext, + ForceAttemptHTTP2: true, + MaxConnsPerHost: 100, + MaxIdleConns: 20, + IdleConnTimeout: 60 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + TLSClientConfig: options.TLSConfig, + } + rc.httpcli = &http.Client{Transport: tr} } - if dialer, ok := options.Context.Value(httpDialerKey{}).(*net.Dialer); ok { - rc.dialer = dialer - } else { - rc.dialer = &net.Dialer{} - } - c := client.Client(rc) // wrap in reverse diff --git a/options.go b/options.go index b80afc4..8db6f1e 100644 --- a/options.go +++ b/options.go @@ -1,7 +1,6 @@ package http import ( - "crypto/tls" "net" "net/http" @@ -33,28 +32,21 @@ type tlsAuth struct{} type maxRecvMsgSizeKey struct{} type maxSendMsgSizeKey struct{} -// maximum streams on a connectioin +// PoolMaxStreams maximum streams on a connectioin func PoolMaxStreams(n int) client.Option { return client.SetOption(poolMaxStreams{}, n) } -// maximum idle conns of a pool +// PoolMaxIdle maximum idle conns of a pool func PoolMaxIdle(d int) client.Option { return client.SetOption(poolMaxIdle{}, d) } -// AuthTLS should be used to setup a secure authentication using TLS -func AuthTLS(t *tls.Config) client.Option { - return client.SetOption(tlsAuth{}, t) -} - -// // MaxRecvMsgSize set the maximum size of message that client can receive. func MaxRecvMsgSize(s int) client.Option { return client.SetOption(maxRecvMsgSizeKey{}, s) } -// // MaxSendMsgSize set the maximum size of message that client can send. func MaxSendMsgSize(s int) client.Option { return client.SetOption(maxSendMsgSizeKey{}, s)