mirror of https://github.com/minio/minio.git
fix: use common logging implementation for DNSCache (#11284)
This commit is contained in:
parent
4e06a72632
commit
c222bde14b
|
@ -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()
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)()
|
||||
|
||||
|
|
Loading…
Reference in New Issue