File tree 2 files changed +40
-0
lines changed
2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -833,6 +833,22 @@ func (c *Client) Head(url string) (resp *Response, err error) {
833
833
return c .Do (req )
834
834
}
835
835
836
+ // CloseIdleConnections closes any connections on its Transport which
837
+ // were previously connected from previous requests but are now
838
+ // sitting idle in a "keep-alive" state. It does not interrupt any
839
+ // connections currently in use.
840
+ //
841
+ // If the Client's Transport does not have a CloseIdleConnections method
842
+ // then this method does nothing.
843
+ func (c * Client ) CloseIdleConnections () {
844
+ type closeIdler interface {
845
+ CloseIdleConnections ()
846
+ }
847
+ if tr , ok := c .transport ().(closeIdler ); ok {
848
+ tr .CloseIdleConnections ()
849
+ }
850
+ }
851
+
836
852
// cancelTimerBody is an io.ReadCloser that wraps rc with two features:
837
853
// 1) on Read error or close, the stop func is called.
838
854
// 2) On Read failure, if reqDidTimeout is true, the error is wrapped and
Original file line number Diff line number Diff line change @@ -1888,3 +1888,27 @@ func TestTransportBodyReadError(t *testing.T) {
1888
1888
t .Errorf ("close calls = %d; want 1" , closeCalls )
1889
1889
}
1890
1890
}
1891
+
1892
+ type roundTripperWithoutCloseIdle struct {}
1893
+
1894
+ func (roundTripperWithoutCloseIdle ) RoundTrip (* Request ) (* Response , error ) { panic ("unused" ) }
1895
+
1896
+ type roundTripperWithCloseIdle func () // underlying func is CloseIdleConnections func
1897
+
1898
+ func (roundTripperWithCloseIdle ) RoundTrip (* Request ) (* Response , error ) { panic ("unused" ) }
1899
+ func (f roundTripperWithCloseIdle ) CloseIdleConnections () { f () }
1900
+
1901
+ func TestClientCloseIdleConnections (t * testing.T ) {
1902
+ c := & Client {Transport : roundTripperWithoutCloseIdle {}}
1903
+ c .CloseIdleConnections () // verify we don't crash at least
1904
+
1905
+ closed := false
1906
+ var tr RoundTripper = roundTripperWithCloseIdle (func () {
1907
+ closed = true
1908
+ })
1909
+ c = & Client {Transport : tr }
1910
+ c .CloseIdleConnections ()
1911
+ if ! closed {
1912
+ t .Error ("not closed" )
1913
+ }
1914
+ }
You can’t perform that action at this time.
0 commit comments