set http connect timeout

This commit is contained in:
wanyaoqi
2019-02-24 12:35:19 +08:00
parent 0474dad972
commit c60cffbfa3
2 changed files with 19 additions and 5 deletions
+12 -3
View File
@@ -5,8 +5,10 @@ import (
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -42,12 +44,19 @@ func NewClient(authUrl string, timeout int, debug bool, insecure bool, certFile,
tr := &http.Transport{
TLSClientConfig: tlsConf,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
}
client := Client{authUrl: authUrl,
timeout: timeout,
debug: debug,
httpconn: &http.Client{Transport: tr}}
timeout: timeout,
debug: debug,
httpconn: &http.Client{
Transport: tr,
},
}
return &client
}
+7 -2
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
@@ -102,7 +103,11 @@ func GetAddrPort(urlStr string) (string, int, error) {
func GetClient(insecure bool) *http.Client {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}
return &http.Client{Transport: tr}
}
@@ -116,7 +121,7 @@ func GetTimeoutClient(timeout time.Duration) *http.Client {
var defaultHttpClient *http.Client
func init() {
defaultHttpClient = GetTimeoutClient(time.Second * 10)
defaultHttpClient = GetClient(true)
}
func GetDefaultClient() *http.Client {