Skip to content

Commit

Permalink
chore: refine dns cache var
Browse files Browse the repository at this point in the history
  • Loading branch information
crimson-gao committed Jun 14, 2024
1 parent 3390fa4 commit ebe3cef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 12 additions & 5 deletions dnsCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import (
"time"
)

const (
defaultDnsCacheTimeOut = 1 * time.Minute
defaultMaxAllowDnsCacheNum = 10000
)

var (
dnsCacheTimeOut = 2 * time.Minute
maxAllowDnsCacheNum = 10000
DnsCacheEnabled = false
DnsCacheTimeOut = defaultDnsCacheTimeOut
MaxAllowDnsCacheNum = defaultMaxAllowDnsCacheNum
defaultDnsResolver = newDnsResolver()
)

type ipInfo struct {
Expand All @@ -33,7 +40,7 @@ func (r *dnsCachedResolver) Get(ctx context.Context, host string) ([]string, err
r.lock.RLock()
ipInfo, exists := r.cache[host]
r.lock.RUnlock()
if exists && ipInfo.refreshTime.Add(dnsCacheTimeOut).After(time.Now()) {
if exists && ipInfo.refreshTime.Add(DnsCacheTimeOut).After(time.Now()) {
return ipInfo.ips, nil
}
return r.lookup(ctx, host)
Expand All @@ -52,7 +59,7 @@ func (r *dnsCachedResolver) Clear() {
}

func (r *dnsCachedResolver) deleteTimoutCachedIps(expireTimeSecond int) {
expireTime := dnsCacheTimeOut
expireTime := DnsCacheTimeOut
if expireTimeSecond >= 60 {
expireTime = time.Duration(expireTimeSecond) * time.Second
}
Expand Down Expand Up @@ -85,7 +92,7 @@ func (r *dnsCachedResolver) lookup(ctx context.Context, host string) ([]string,
r.cache[host] = ipInfo{ips: strIPs, refreshTime: time.Now()}
l := len(r.cache)
r.lock.Unlock()
if l > maxAllowDnsCacheNum {
if l > MaxAllowDnsCacheNum {
r.deleteTimoutCachedIps(0)
}
return strIPs, nil
Expand Down
8 changes: 3 additions & 5 deletions log_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const (
)

var (
ipRegex = regexp.MustCompile(ipRegexStr)
resolver = newDnsResolver()
DnsCacheEnabled = false
ipRegex = regexp.MustCompile(ipRegexStr)
)

// this file is deprecated and no maintenance
Expand Down Expand Up @@ -1130,7 +1128,7 @@ func (p *LogProject) init() {
}

func (p *LogProject) getBaseURL() string {
if len(p.baseURL) > 0 && p.baseUrlUpdateTime.After(time.Now().Add(-dnsCacheTimeOut)) {
if len(p.baseURL) > 0 && p.baseUrlUpdateTime.After(time.Now().Add(-DnsCacheTimeOut)) {
return p.baseURL
}
p.parseEndpoint()
Expand Down Expand Up @@ -1161,7 +1159,7 @@ func (p *LogProject) parseEndpoint() {
setHTTPProxy(p.httpClient, url)
} else if DnsCacheEnabled {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = newDnsDialContext(resolver, nil)
transport.DialContext = newDnsDialContext(defaultDnsResolver, nil)
p.httpClient = &http.Client{
Transport: transport,
Timeout: defaultRequestTimeout,
Expand Down

0 comments on commit ebe3cef

Please sign in to comment.