File tree Expand file tree Collapse file tree 4 files changed +134
-164
lines changed Expand file tree Collapse file tree 4 files changed +134
-164
lines changed Original file line number Diff line number Diff line change @@ -5,21 +5,17 @@ import (
5
5
"time"
6
6
)
7
7
8
- var client HttpClient
9
-
10
- type HttpClient interface {
11
- Do (req * http.Request ) (* http.Response , error )
12
- }
8
+ var httpClient * http.Client
13
9
14
10
// getHTTPClient returns the shared HTTP client
15
- func getHTTPClient () HttpClient {
16
- if client == nil {
11
+ func getHTTPClient () * http. Client {
12
+ if httpClient == nil {
17
13
tr := http .DefaultTransport .(* http.Transport ).Clone ()
18
14
tr .MaxIdleConnsPerHost = 50
19
- client = & http.Client {
15
+ httpClient = & http.Client {
20
16
Timeout : time .Second * 10 ,
21
17
Transport : tr ,
22
18
}
23
19
}
24
- return client
20
+ return httpClient
25
21
}
Original file line number Diff line number Diff line change @@ -7,20 +7,15 @@ import (
7
7
)
8
8
9
9
func TestValidateClient (t * testing.T ) {
10
- oldRef := getHTTPClient ()
11
- defer func () {
12
- client = oldRef
13
- }()
14
- client = nil
10
+ httpClient = nil
15
11
client := getHTTPClient ()
16
12
if client == nil {
17
13
t .Error ("client should not be nil" )
18
14
}
19
- cl := client .(* http.Client )
20
- if cl .Timeout != 10 * time .Second {
15
+ if client .Timeout != 10 * time .Second {
21
16
t .Error ("default timeout should be 10 seconds" )
22
17
}
23
- tr := cl .Transport .(* http.Transport )
18
+ tr := client .Transport .(* http.Transport )
24
19
if tr .MaxIdleConnsPerHost != 50 {
25
20
t .Error ("default max conns idle conns per host should be 150" )
26
21
}
You can’t perform that action at this time.
0 commit comments