Fix data race on injectedHTTPClient in client package
Add sync.RWMutex to protect concurrent read/write access to the package-level injectedHTTPClient variable, which was causing flaky test failures when TestGetHTTPClient and TestHttpClientProvidesOAuth2BearerToken ran in parallel with -race. Agent-Logs-Url: https://github.com/TwiN/gatus/sessions/5eccf19f-b033-4bf0-9d3b-4e462e7e89c4 Co-authored-by: TwiN <15699766+TwiN@users.noreply.github.com>
This commit is contained in:
1 parent
5fed1f69f5
commit
2be0bd717d
1 file changed
+7
-1
+7
-1
@@ -16,6 +16,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/TwiN/gocache/v2"
|
"github.com/TwiN/gocache/v2"
|
||||||
@@ -36,7 +37,8 @@ const (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
// injectedHTTPClient is used for testing purposes
|
// injectedHTTPClient is used for testing purposes
|
||||||
injectedHTTPClient *http.Client
|
injectedHTTPClient *http.Client
|
||||||
|
injectedHTTPClientM sync.RWMutex
|
||||||
|
|
||||||
whoisClient = whois.NewClient().WithReferralCache(true)
|
whoisClient = whois.NewClient().WithReferralCache(true)
|
||||||
whoisExpirationDateCache = gocache.NewCache().WithMaxSize(10000).WithDefaultTTL(24 * time.Hour)
|
whoisExpirationDateCache = gocache.NewCache().WithMaxSize(10000).WithDefaultTTL(24 * time.Hour)
|
||||||
@@ -45,6 +47,8 @@ var (
|
|||||||
|
|
||||||
// GetHTTPClient returns the shared HTTP client, or the client from the configuration passed
|
// GetHTTPClient returns the shared HTTP client, or the client from the configuration passed
|
||||||
func GetHTTPClient(config *Config) *http.Client {
|
func GetHTTPClient(config *Config) *http.Client {
|
||||||
|
injectedHTTPClientM.RLock()
|
||||||
|
defer injectedHTTPClientM.RUnlock()
|
||||||
if injectedHTTPClient != nil {
|
if injectedHTTPClient != nil {
|
||||||
return injectedHTTPClient
|
return injectedHTTPClient
|
||||||
}
|
}
|
||||||
@@ -508,7 +512,9 @@ func QueryDNS(queryType, queryName, url string) (connected bool, dnsRcode string
|
|||||||
|
|
||||||
// InjectHTTPClient is used to inject a custom HTTP client for testing purposes
|
// InjectHTTPClient is used to inject a custom HTTP client for testing purposes
|
||||||
func InjectHTTPClient(httpClient *http.Client) {
|
func InjectHTTPClient(httpClient *http.Client) {
|
||||||
|
injectedHTTPClientM.Lock()
|
||||||
injectedHTTPClient = httpClient
|
injectedHTTPClient = httpClient
|
||||||
|
injectedHTTPClientM.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// rdapQuery returns domain expiration via RDAP protocol
|
// rdapQuery returns domain expiration via RDAP protocol
|
||||||
|
|||||||
Reference in new issue
Block a user