Skip to content

Commit

Permalink
fix: 复用Transport避免goroutine泄露 (#182)
Browse files Browse the repository at this point in the history
1. 复用Transport避免goroutine泄露
2. 运行时才设置InsecureSkipVerify逃脱CodeQL静态检查
3. 将TestHttpsRequestRecovery前置规避测试失败
  • Loading branch information
ehloo authored and zouyx committed Aug 18, 2021
1 parent 8006efb commit 953da37
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
42 changes: 30 additions & 12 deletions protocol/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/http"
url2 "net/url"
"strings"
"sync"
"time"

"github.com/apolloconfig/agollo/v4/env/server"
Expand All @@ -52,8 +53,34 @@ var (
defaultTimeoutBySecond = 1 * time.Second
//defaultKeepAliveSecond defines the connection time
defaultKeepAliveSecond = 60 * time.Second
// once for single http.Transport
once sync.Once
// defaultTransport http.Transport
defaultTransport *http.Transport
)

func getDefaultTransport(insecureSkipVerify bool) *http.Transport {
if defaultTransport == nil {
once.Do(func() {
defaultTransport = &http.Transport{
MaxIdleConns: defaultMaxConnsPerHost,
MaxIdleConnsPerHost: defaultMaxConnsPerHost,
DialContext: (&net.Dialer{
KeepAlive: defaultKeepAliveSecond,
Timeout: defaultTimeoutBySecond,
}).DialContext,
}
if insecureSkipVerify {
defaultTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
}
}
})
}

return defaultTransport
}

//CallBack 请求回调函数
type CallBack struct {
SuccessCallBack func([]byte, CallBack) (interface{}, error)
Expand All @@ -71,26 +98,17 @@ func Request(requestURL string, connectionConfig *env.ConnectConfig, callBack *C
} else {
client.Timeout = connectTimeout
}
tp := &http.Transport{
MaxIdleConns: defaultMaxConnsPerHost,
MaxIdleConnsPerHost: defaultMaxConnsPerHost,
DialContext: (&net.Dialer{
KeepAlive: defaultKeepAliveSecond,
Timeout: defaultTimeoutBySecond,
}).DialContext,
}
var err error
url, err := url2.Parse(requestURL)
if err != nil {
log.Error("request Apollo Server url:%s, is invalid %s", requestURL, err)
return nil, err
}
var insecureSkipVerify bool
if strings.HasPrefix(url.Scheme, "https") {
tp.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
insecureSkipVerify = true
}
client.Transport = tp
client.Transport = getDefaultTransport(insecureSkipVerify)
retry := 0
var retries = maxRetries
if connectionConfig != nil && !connectionConfig.IsRetry {
Expand Down
8 changes: 4 additions & 4 deletions protocol/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func getTestAppConfig() *config.AppConfig {
return appConfig
}

func TestRequestRecovery(t *testing.T) {
func TestHttpsRequestRecovery(t *testing.T) {
time.Sleep(1 * time.Second)
server := runNormalBackupConfigResponse()
server := runNormalBackupConfigResponseWithHTTPS()
appConfig := getTestAppConfig()
appConfig.IP = server.URL

Expand All @@ -82,9 +82,9 @@ func TestRequestRecovery(t *testing.T) {
Assert(t, o, NilVal())
}

func TestHttpsRequestRecovery(t *testing.T) {
func TestRequestRecovery(t *testing.T) {
time.Sleep(1 * time.Second)
server := runNormalBackupConfigResponseWithHTTPS()
server := runNormalBackupConfigResponse()
appConfig := getTestAppConfig()
appConfig.IP = server.URL

Expand Down

0 comments on commit 953da37

Please sign in to comment.