From c222bde14b8492e4fe9c53d9ebf5138d03b349e0 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 15 Jan 2021 14:04:56 -0800 Subject: [PATCH] fix: use common logging implementation for DNSCache (#11284) --- cmd/common-main.go | 2 +- cmd/http/dial_dnscache.go | 7 ++++--- cmd/http/dial_dnscache_test.go | 6 +++++- cmd/test-utils_test.go | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/common-main.go b/cmd/common-main.go index c9626e45d..b495821c8 100644 --- a/cmd/common-main.go +++ b/cmd/common-main.go @@ -51,7 +51,7 @@ func init() { logger.RegisterError(config.FmtError) rand.Seed(time.Now().UTC().UnixNano()) - globalDNSCache = xhttp.NewDNSCache(10*time.Second, 10*time.Second) + globalDNSCache = xhttp.NewDNSCache(10*time.Second, 10*time.Second, logger.LogOnceIf) initGlobalContext() diff --git a/cmd/http/dial_dnscache.go b/cmd/http/dial_dnscache.go index 91785e35b..34612d12c 100644 --- a/cmd/http/dial_dnscache.go +++ b/cmd/http/dial_dnscache.go @@ -18,7 +18,6 @@ package http import ( "context" - "log" "math/rand" "net" "sync" @@ -96,6 +95,7 @@ type DNSCache struct { lookupHostFn func(ctx context.Context, host string) ([]string, error) lookupTimeout time.Duration + loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{}) cache map[string][]string doneOnce sync.Once @@ -105,7 +105,7 @@ type DNSCache struct { // NewDNSCache initializes DNS cache resolver and starts auto refreshing // in a new goroutine. To stop auto refreshing, call `Stop()` function. // Once `Stop()` is called auto refreshing cannot be resumed. -func NewDNSCache(freq time.Duration, lookupTimeout time.Duration) *DNSCache { +func NewDNSCache(freq time.Duration, lookupTimeout time.Duration, loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})) *DNSCache { if freq <= 0 { freq = defaultFreq } @@ -117,6 +117,7 @@ func NewDNSCache(freq time.Duration, lookupTimeout time.Duration) *DNSCache { r := &DNSCache{ lookupHostFn: net.DefaultResolver.LookupHost, lookupTimeout: lookupTimeout, + loggerOnce: loggerOnce, cache: make(map[string][]string, cacheSize), doneCh: make(chan struct{}), } @@ -179,7 +180,7 @@ func (r *DNSCache) Refresh() { for _, host := range hosts { ctx, cancelF := context.WithTimeout(context.Background(), r.lookupTimeout) if _, err := r.LookupHost(ctx, host); err != nil { - log.Println("failed to refresh DNS cache, resolver is unavailable", err) + r.loggerOnce(ctx, err, host) } cancelF() } diff --git a/cmd/http/dial_dnscache_test.go b/cmd/http/dial_dnscache_test.go index f2f2951d4..80e669b7f 100644 --- a/cmd/http/dial_dnscache_test.go +++ b/cmd/http/dial_dnscache_test.go @@ -31,9 +31,13 @@ var ( testDefaultLookupTimeout = 1 * time.Second ) +func logOnce(ctx context.Context, err error, id interface{}, errKind ...interface{}) { + // no-op +} + func testDNSCache(t *testing.T) *DNSCache { t.Helper() // skip printing file and line information from this function - return NewDNSCache(testFreq, testDefaultLookupTimeout) + return NewDNSCache(testFreq, testDefaultLookupTimeout, logOnce) } func TestDialContextWithDNSCache(t *testing.T) { diff --git a/cmd/test-utils_test.go b/cmd/test-utils_test.go index 82e217c89..63722faf9 100644 --- a/cmd/test-utils_test.go +++ b/cmd/test-utils_test.go @@ -107,7 +107,7 @@ func TestMain(m *testing.M) { // Initialize globalConsoleSys system globalConsoleSys = NewConsoleLogger(context.Background()) - globalDNSCache = xhttp.NewDNSCache(3*time.Second, 10*time.Second) + globalDNSCache = xhttp.NewDNSCache(3*time.Second, 10*time.Second, logger.LogOnceIf) globalInternodeTransport = newInternodeHTTPTransport(nil, rest.DefaultTimeout)()