Skip to content

Commit b86ea81

Browse files
author
Julien Pivotto
authored
OAuth2: Respect disable keepalives option; Implement close idle connections (#390)
Signed-off-by: Julien Pivotto <[email protected]>
1 parent cdc09f0 commit b86ea81

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

config/http_config.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ type oauth2RoundTripper struct {
632632
secret string
633633
mtx sync.RWMutex
634634
opts *httpClientOptions
635+
client *http.Client
635636
}
636637

637638
func NewOAuth2RoundTripper(config *OAuth2, next http.RoundTripper, opts *httpClientOptions) http.RoundTripper {
@@ -677,19 +678,24 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
677678
return nil, err
678679
}
679680

681+
tlsTransport := func(tlsConfig *tls.Config) (http.RoundTripper, error) {
682+
return &http.Transport{
683+
TLSClientConfig: tlsConfig,
684+
Proxy: http.ProxyURL(rt.config.ProxyURL.URL),
685+
DisableKeepAlives: !rt.opts.keepAlivesEnabled,
686+
MaxIdleConns: 20,
687+
MaxIdleConnsPerHost: 1, // see https://github.com/golang/go/issues/13801
688+
IdleConnTimeout: 10 * time.Second,
689+
TLSHandshakeTimeout: 10 * time.Second,
690+
ExpectContinueTimeout: 1 * time.Second,
691+
}, nil
692+
}
693+
680694
var t http.RoundTripper
681695
if len(rt.config.TLSConfig.CAFile) == 0 {
682-
t = &http.Transport{
683-
TLSClientConfig: tlsConfig,
684-
Proxy: http.ProxyURL(rt.config.ProxyURL.URL),
685-
}
696+
t, _ = tlsTransport(tlsConfig)
686697
} else {
687-
t, err = NewTLSRoundTripper(tlsConfig, rt.config.TLSConfig.CAFile, func(tls *tls.Config) (http.RoundTripper, error) {
688-
return &http.Transport{
689-
TLSClientConfig: tls,
690-
Proxy: http.ProxyURL(rt.config.ProxyURL.URL),
691-
}, nil
692-
})
698+
t, err = NewTLSRoundTripper(tlsConfig, rt.config.TLSConfig.CAFile, tlsTransport)
693699
if err != nil {
694700
return nil, err
695701
}
@@ -699,7 +705,8 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
699705
t = NewUserAgentRoundTripper(rt.opts.userAgent, t)
700706
}
701707

702-
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{Transport: t})
708+
client := &http.Client{Transport: t}
709+
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, client)
703710
tokenSource := config.TokenSource(ctx)
704711

705712
rt.mtx.Lock()
@@ -708,6 +715,10 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
708715
Base: rt.next,
709716
Source: tokenSource,
710717
}
718+
if rt.client != nil {
719+
rt.client.CloseIdleConnections()
720+
}
721+
rt.client = client
711722
rt.mtx.Unlock()
712723
}
713724

@@ -718,7 +729,9 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
718729
}
719730

720731
func (rt *oauth2RoundTripper) CloseIdleConnections() {
721-
// OAuth2 RT does not support CloseIdleConnections() but the next RT might.
732+
if rt.client != nil {
733+
rt.client.CloseIdleConnections()
734+
}
722735
if ci, ok := rt.next.(closeIdler); ok {
723736
ci.CloseIdleConnections()
724737
}

0 commit comments

Comments
 (0)