From 46782ff90c29d2bc04ca130763ffbf95e95c5b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Wed, 21 Aug 2024 11:26:17 +0300 Subject: [PATCH] use sync.Once --- pkg/protocols/http/httpclientpool/clientpool.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pkg/protocols/http/httpclientpool/clientpool.go b/pkg/protocols/http/httpclientpool/clientpool.go index da7d5467e..5750d5aea 100644 --- a/pkg/protocols/http/httpclientpool/clientpool.go +++ b/pkg/protocols/http/httpclientpool/clientpool.go @@ -29,10 +29,10 @@ import ( var ( rawHttpClient *rawhttp.Client + rawHttpClientOnce sync.Once forceMaxRedirects int normalClient *retryablehttp.Client clientPool *mapsutil.SyncLockMap[string, *retryablehttp.Client] - rawHttpClientMu sync.Mutex ) // Init initializes the clientpool implementation @@ -148,10 +148,7 @@ func (c *Configuration) HasStandardOptions() bool { // GetRawHTTP returns the rawhttp request client func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client { - rawHttpClientMu.Lock() - defer rawHttpClientMu.Unlock() - - if rawHttpClient == nil { + rawHttpClientOnce.Do(func() { rawHttpOptions := rawhttp.DefaultOptions if types.ProxyURL != "" { rawHttpOptions.Proxy = types.ProxyURL @@ -162,7 +159,7 @@ func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client { } rawHttpOptions.Timeout = options.Options.GetTimeouts().HttpTimeout rawHttpClient = rawhttp.NewClient(rawHttpOptions) - } + }) return rawHttpClient }