Skip to content

Commit 8912609

Browse files
committed
Cache local DNS persistence settings and streamline flush/load guards
1 parent 3649e04 commit 8912609

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

internal/client/client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type Client struct {
3636
connectionsByKey map[string]int
3737
localDNSCache *dnsCache.Store
3838
dnsResponses *fragmentStore.Store[clientDNSFragmentKey]
39+
localDNSCachePath string
40+
localDNSCachePersist bool
41+
localDNSCacheFlushTick time.Duration
3942
localDNSFragTTL time.Duration
4043
localDNSCacheLoadOnce sync.Once
4144
localDNSCacheFlushOnce sync.Once
@@ -150,7 +153,12 @@ func New(cfg config.ClientConfig, log *logger.Logger, codec *security.Codec) *Cl
150153
time.Duration(cfg.LocalDNSCacheTTLSeconds*float64(time.Second)),
151154
time.Duration(cfg.LocalDNSPendingTimeoutSec*float64(time.Second)),
152155
),
153-
dnsResponses: fragmentStore.New[clientDNSFragmentKey](32),
156+
dnsResponses: fragmentStore.New[clientDNSFragmentKey](32),
157+
localDNSCachePath: cfg.LocalDNSCachePath(),
158+
localDNSCachePersist: cfg.LocalDNSCachePersist,
159+
localDNSCacheFlushTick: time.Duration(
160+
cfg.LocalDNSCacheFlushSec * float64(time.Second),
161+
),
154162
localDNSFragTTL: time.Duration(cfg.LocalDNSFragmentTimeoutSec * float64(time.Second)),
155163
streams: make(map[uint16]*clientStream, 16),
156164
streamTXWindow: cfg.StreamTXWindow,
@@ -161,6 +169,9 @@ func New(cfg config.ClientConfig, log *logger.Logger, codec *security.Codec) *Cl
161169
resolverRecheck: make(map[string]resolverRecheckState, len(cfg.Domains)*len(cfg.Resolvers)),
162170
runtimeDisabled: make(map[string]resolverDisabledState, len(cfg.Domains)*len(cfg.Resolvers)),
163171
}
172+
if c.localDNSCacheFlushTick <= 0 {
173+
c.localDNSCacheFlushTick = time.Minute
174+
}
164175
if c.localDNSFragTTL <= 0 {
165176
c.localDNSFragTTL = 5 * time.Minute
166177
}

internal/client/local_dns_cache_persistence.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@ import (
1212
"time"
1313
)
1414

15+
func (c *Client) hasPersistableLocalDNSCache() bool {
16+
return c != nil &&
17+
c.localDNSCache != nil &&
18+
c.localDNSCachePersist &&
19+
c.localDNSCachePath != ""
20+
}
21+
1522
func (c *Client) persistResolvedLocalDNSCacheEntry(cacheKey []byte, domain string, qType uint16, qClass uint16, rawResponse []byte, now time.Time) {
1623
if c == nil || c.localDNSCache == nil {
1724
return
1825
}
1926

2027
c.localDNSCache.SetReady(cacheKey, domain, qType, qClass, rawResponse, now)
21-
if c.cfg.LocalDNSCachePersist {
28+
if c.hasPersistableLocalDNSCache() {
2229
c.flushLocalDNSCache()
2330
}
2431
}
2532

2633
func (c *Client) ensureLocalDNSCacheLoaded() {
27-
if c == nil {
34+
if !c.hasPersistableLocalDNSCache() {
2835
return
2936
}
3037
c.localDNSCacheLoadOnce.Do(func() {
@@ -33,7 +40,7 @@ func (c *Client) ensureLocalDNSCacheLoaded() {
3340
}
3441

3542
func (c *Client) ensureLocalDNSCachePersistence(ctx context.Context) {
36-
if c == nil {
43+
if !c.hasPersistableLocalDNSCache() {
3744
return
3845
}
3946
c.ensureLocalDNSCacheLoaded()
@@ -43,11 +50,11 @@ func (c *Client) ensureLocalDNSCachePersistence(ctx context.Context) {
4350
}
4451

4552
func (c *Client) loadLocalDNSCache() {
46-
if c == nil || !c.cfg.LocalDNSCachePersist || c.localDNSCache == nil {
53+
if !c.hasPersistableLocalDNSCache() {
4754
return
4855
}
4956

50-
loaded, err := c.localDNSCache.LoadFromFile(c.cfg.LocalDNSCachePath(), c.now())
57+
loaded, err := c.localDNSCache.LoadFromFile(c.localDNSCachePath, c.now())
5158
if err != nil {
5259
if c.log != nil {
5360
c.log.Warnf("\U0001F4BE <cyan>Local DNS Cache</cyan> <magenta>|</magenta> <red>Load Failed</red> <magenta>|</magenta> <yellow>%v</yellow>", err)
@@ -60,11 +67,11 @@ func (c *Client) loadLocalDNSCache() {
6067
}
6168

6269
func (c *Client) flushLocalDNSCache() {
63-
if c == nil || !c.cfg.LocalDNSCachePersist || c.localDNSCache == nil {
70+
if !c.hasPersistableLocalDNSCache() {
6471
return
6572
}
6673

67-
saved, err := c.localDNSCache.SaveToFile(c.cfg.LocalDNSCachePath(), c.now())
74+
saved, err := c.localDNSCache.SaveToFile(c.localDNSCachePath, c.now())
6875
if err != nil {
6976
if c.log != nil {
7077
c.log.Warnf("\U0001F4BE <cyan>Local DNS Cache</cyan> <magenta>|</magenta> <red>Flush Failed</red> <magenta>|</magenta> <yellow>%v</yellow>", err)
@@ -77,16 +84,11 @@ func (c *Client) flushLocalDNSCache() {
7784
}
7885

7986
func (c *Client) runLocalDNSCacheFlushLoop(ctx context.Context) {
80-
if c == nil || !c.cfg.LocalDNSCachePersist || c.localDNSCache == nil {
87+
if !c.hasPersistableLocalDNSCache() {
8188
return
8289
}
8390

84-
interval := time.Duration(c.cfg.LocalDNSCacheFlushSec * float64(time.Second))
85-
if interval <= 0 {
86-
interval = time.Minute
87-
}
88-
89-
ticker := time.NewTicker(interval)
91+
ticker := time.NewTicker(c.localDNSCacheFlushTick)
9092
defer ticker.Stop()
9193
defer c.flushLocalDNSCache()
9294

0 commit comments

Comments
 (0)