use sync.Once

This commit is contained in:
Doğan Can Bakır 2024-08-21 11:26:17 +03:00
parent 50023428d0
commit 46782ff90c

View File

@ -29,10 +29,10 @@ import (
var ( var (
rawHttpClient *rawhttp.Client rawHttpClient *rawhttp.Client
rawHttpClientOnce sync.Once
forceMaxRedirects int forceMaxRedirects int
normalClient *retryablehttp.Client normalClient *retryablehttp.Client
clientPool *mapsutil.SyncLockMap[string, *retryablehttp.Client] clientPool *mapsutil.SyncLockMap[string, *retryablehttp.Client]
rawHttpClientMu sync.Mutex
) )
// Init initializes the clientpool implementation // Init initializes the clientpool implementation
@ -148,10 +148,7 @@ func (c *Configuration) HasStandardOptions() bool {
// GetRawHTTP returns the rawhttp request client // GetRawHTTP returns the rawhttp request client
func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client { func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client {
rawHttpClientMu.Lock() rawHttpClientOnce.Do(func() {
defer rawHttpClientMu.Unlock()
if rawHttpClient == nil {
rawHttpOptions := rawhttp.DefaultOptions rawHttpOptions := rawhttp.DefaultOptions
if types.ProxyURL != "" { if types.ProxyURL != "" {
rawHttpOptions.Proxy = types.ProxyURL rawHttpOptions.Proxy = types.ProxyURL
@ -162,7 +159,7 @@ func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client {
} }
rawHttpOptions.Timeout = options.Options.GetTimeouts().HttpTimeout rawHttpOptions.Timeout = options.Options.GetTimeouts().HttpTimeout
rawHttpClient = rawhttp.NewClient(rawHttpOptions) rawHttpClient = rawhttp.NewClient(rawHttpOptions)
} })
return rawHttpClient return rawHttpClient
} }