Skip to content

Commit 53a31cc

Browse files
committed
feat: shorten dns cache ttl of empty record
1 parent ca85c30 commit 53a31cc

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

pkg/common/dns_util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func ReadCache(domain string, qtype uint16, ttl int64) []dns.RR {
5757
}
5858

5959
// WriteCache record to cache
60-
func WriteCache(domain string, qtype uint16, answer []dns.RR) {
61-
nsCache.Store(getCacheKey(domain, qtype), NsEntry{answer, time.Now().Unix()})
60+
func WriteCache(domain string, qtype uint16, answer []dns.RR, timestamp int64) {
61+
nsCache.Store(getCacheKey(domain, qtype), NsEntry{answer, timestamp})
6262
}
6363

6464
func notExpired(timestamp int64, ttl int64) bool {

pkg/kt/service/dns/dnsserver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ func query(req *dns.Msg, dnsAddresses []string) []dns.RR {
8888
if res != nil && len(res.Answer) > 0 {
8989
// only record none-empty result of cluster dns
9090
log.Debug().Msgf("Found domain %s (%d) in dns (%s)", domain, qtype, ipAndPort)
91-
common.WriteCache(domain, qtype, res.Answer)
91+
common.WriteCache(domain, qtype, res.Answer, time.Now().Unix())
9292
return res.Answer
9393
} else if err != nil && !common.IsDomainNotExist(err) {
9494
// usually io timeout error
9595
log.Warn().Err(err).Msgf("Failed to lookup %s (%d) in dns (%s)", domain, qtype, ipAndPort)
9696
}
9797
}
9898
log.Debug().Msgf("Empty answer for domain lookup %s (%d)", domain, qtype)
99-
common.WriteCache(domain, qtype, []dns.RR{})
99+
common.WriteCache(domain, qtype, []dns.RR{}, time.Now().Unix() - opt.Get().ConnectOptions.DnsCacheTtl / 2)
100100
return []dns.RR{}
101101
}

pkg/shadow/dnsserver/dnsserver.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/rs/zerolog/log"
99
"net"
1010
"strings"
11+
"time"
1112
)
1213

1314
// DnsServer nds server
@@ -77,7 +78,7 @@ func (s *DnsServer) query(req *dns.Msg) (rr []dns.RR) {
7778
break
7879
}
7980
}
80-
common.WriteCache(name, qtype, rr)
81+
common.WriteCache(name, qtype, rr, time.Now().Unix())
8182
return
8283
}
8384

0 commit comments

Comments
 (0)