11package request
22
33import (
4+ "crypto/tls"
45 "net"
56 "net/http"
67 "time"
@@ -10,21 +11,41 @@ var NoRedirection = func(*http.Request, []*http.Request) error {
1011 return http .ErrUseLastResponse
1112}
1213
13- var defaultTransport = & http.Transport {
14- Proxy : http .ProxyFromEnvironment ,
14+ var defaultTransport = CreateTransport ()
1515
16- DialContext : (& net.Dialer {
17- Timeout : time .Second * 5 ,
18- KeepAlive : time .Second * 15 ,
19- }).DialContext ,
16+ type TransportOption func (* http.Transport )
2017
21- TLSHandshakeTimeout : time .Second * 5 ,
22- ExpectContinueTimeout : time .Second * 1 ,
18+ func WithInsecure () TransportOption {
19+ return func (instance * http.Transport ) {
20+ instance .TLSClientConfig = & tls.Config {
21+ InsecureSkipVerify : true ,
22+ }
23+ }
24+ }
25+
26+ func CreateTransport (opts ... TransportOption ) * http.Transport {
27+ transport := & http.Transport {
28+ Proxy : http .ProxyFromEnvironment ,
29+
30+ DialContext : (& net.Dialer {
31+ Timeout : time .Second * 5 ,
32+ KeepAlive : time .Second * 15 ,
33+ }).DialContext ,
34+
35+ TLSHandshakeTimeout : time .Second * 5 ,
36+ ExpectContinueTimeout : time .Second * 1 ,
37+
38+ MaxConnsPerHost : 512 ,
39+ MaxIdleConns : 256 ,
40+ MaxIdleConnsPerHost : 128 ,
41+ IdleConnTimeout : time .Second * 60 ,
42+ }
43+
44+ for _ , opt := range opts {
45+ opt (transport )
46+ }
2347
24- MaxConnsPerHost : 512 ,
25- MaxIdleConns : 256 ,
26- MaxIdleConnsPerHost : 128 ,
27- IdleConnTimeout : time .Second * 60 ,
48+ return transport
2849}
2950
3051func CreateClient (timeout time.Duration , onRedirect func (* http.Request , []* http.Request ) error ) * http.Client {
0 commit comments