Skip to content

Commit

Permalink
Set default transport that copy from the http.DefaultTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev committed Dec 23, 2021
1 parent 040bb19 commit 389f72c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package util

import (
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
)

var defaultClient = &http.Client{}
var noRedirectClient = &http.Client{}
var defaultTransport http.RoundTripper

// HttpGet get http resource
func HttpGet(url string) (resp *http.Response, err error) {
Expand Down Expand Up @@ -37,16 +40,23 @@ func HttpPostWithoutRedirect(url string, data url.Values) (resp *http.Response,
}

func init() {
defaultClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
noRedirectClient.Transport = &http.Transport{
defaultTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
defaultClient.Transport = defaultTransport
noRedirectClient.Transport = defaultTransport
noRedirectClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
Expand Down

0 comments on commit 389f72c

Please sign in to comment.