|
|
@ -18,7 +18,6 @@ package http |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"context" |
|
|
|
"log" |
|
|
|
|
|
|
|
"math/rand" |
|
|
|
"math/rand" |
|
|
|
"net" |
|
|
|
"net" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
@ -96,6 +95,7 @@ type DNSCache struct { |
|
|
|
|
|
|
|
|
|
|
|
lookupHostFn func(ctx context.Context, host string) ([]string, error) |
|
|
|
lookupHostFn func(ctx context.Context, host string) ([]string, error) |
|
|
|
lookupTimeout time.Duration |
|
|
|
lookupTimeout time.Duration |
|
|
|
|
|
|
|
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{}) |
|
|
|
|
|
|
|
|
|
|
|
cache map[string][]string |
|
|
|
cache map[string][]string |
|
|
|
doneOnce sync.Once |
|
|
|
doneOnce sync.Once |
|
|
@ -105,7 +105,7 @@ type DNSCache struct { |
|
|
|
// NewDNSCache initializes DNS cache resolver and starts auto refreshing
|
|
|
|
// NewDNSCache initializes DNS cache resolver and starts auto refreshing
|
|
|
|
// in a new goroutine. To stop auto refreshing, call `Stop()` function.
|
|
|
|
// in a new goroutine. To stop auto refreshing, call `Stop()` function.
|
|
|
|
// Once `Stop()` is called auto refreshing cannot be resumed.
|
|
|
|
// 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 { |
|
|
|
if freq <= 0 { |
|
|
|
freq = defaultFreq |
|
|
|
freq = defaultFreq |
|
|
|
} |
|
|
|
} |
|
|
@ -117,6 +117,7 @@ func NewDNSCache(freq time.Duration, lookupTimeout time.Duration) *DNSCache { |
|
|
|
r := &DNSCache{ |
|
|
|
r := &DNSCache{ |
|
|
|
lookupHostFn: net.DefaultResolver.LookupHost, |
|
|
|
lookupHostFn: net.DefaultResolver.LookupHost, |
|
|
|
lookupTimeout: lookupTimeout, |
|
|
|
lookupTimeout: lookupTimeout, |
|
|
|
|
|
|
|
loggerOnce: loggerOnce, |
|
|
|
cache: make(map[string][]string, cacheSize), |
|
|
|
cache: make(map[string][]string, cacheSize), |
|
|
|
doneCh: make(chan struct{}), |
|
|
|
doneCh: make(chan struct{}), |
|
|
|
} |
|
|
|
} |
|
|
@ -179,7 +180,7 @@ func (r *DNSCache) Refresh() { |
|
|
|
for _, host := range hosts { |
|
|
|
for _, host := range hosts { |
|
|
|
ctx, cancelF := context.WithTimeout(context.Background(), r.lookupTimeout) |
|
|
|
ctx, cancelF := context.WithTimeout(context.Background(), r.lookupTimeout) |
|
|
|
if _, err := r.LookupHost(ctx, host); err != nil { |
|
|
|
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() |
|
|
|
cancelF() |
|
|
|
} |
|
|
|
} |
|
|
|