@@ -632,6 +632,7 @@ type oauth2RoundTripper struct {
632
632
secret string
633
633
mtx sync.RWMutex
634
634
opts * httpClientOptions
635
+ client * http.Client
635
636
}
636
637
637
638
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
677
678
return nil , err
678
679
}
679
680
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
+
680
694
var t http.RoundTripper
681
695
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 )
686
697
} 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 )
693
699
if err != nil {
694
700
return nil , err
695
701
}
@@ -699,7 +705,8 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
699
705
t = NewUserAgentRoundTripper (rt .opts .userAgent , t )
700
706
}
701
707
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 )
703
710
tokenSource := config .TokenSource (ctx )
704
711
705
712
rt .mtx .Lock ()
@@ -708,6 +715,10 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
708
715
Base : rt .next ,
709
716
Source : tokenSource ,
710
717
}
718
+ if rt .client != nil {
719
+ rt .client .CloseIdleConnections ()
720
+ }
721
+ rt .client = client
711
722
rt .mtx .Unlock ()
712
723
}
713
724
@@ -718,7 +729,9 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
718
729
}
719
730
720
731
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
+ }
722
735
if ci , ok := rt .next .(closeIdler ); ok {
723
736
ci .CloseIdleConnections ()
724
737
}
0 commit comments