Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66fd5c9

Browse files
committed
chore: allow setting cache-max-size in dns section
1 parent c3a3009 commit 66fd5c9

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

config/config.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ type DNS struct {
156156
EnhancedMode C.DNSMode
157157
DefaultNameserver []dns.NameServer
158158
CacheAlgorithm string
159+
CacheMaxSize int
159160
FakeIPRange *fakeip.Pool
160161
Hosts *trie.DomainTrie[resolver.HostValue]
161162
NameServerPolicy []dns.Policy
@@ -223,6 +224,7 @@ type RawDNS struct {
223224
FakeIPFilterMode C.FilterMode `yaml:"fake-ip-filter-mode" json:"fake-ip-filter-mode"`
224225
DefaultNameserver []string `yaml:"default-nameserver" json:"default-nameserver"`
225226
CacheAlgorithm string `yaml:"cache-algorithm" json:"cache-algorithm"`
227+
CacheMaxSize int `yaml:"cache-max-size" json:"cache-max-size"`
226228
NameServerPolicy *orderedmap.OrderedMap[string, any] `yaml:"nameserver-policy" json:"nameserver-policy"`
227229
ProxyServerNameserver []string `yaml:"proxy-server-nameserver" json:"proxy-server-nameserver"`
228230
DirectNameServer []string `yaml:"direct-nameserver" json:"direct-nameserver"`
@@ -1352,6 +1354,8 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
13521354
IPv6: cfg.IPv6,
13531355
UseSystemHosts: cfg.UseSystemHosts,
13541356
EnhancedMode: cfg.EnhancedMode,
1357+
CacheAlgorithm: cfg.CacheAlgorithm,
1358+
CacheMaxSize: cfg.CacheMaxSize,
13551359
}
13561360
var err error
13571361
if dnsCfg.NameServer, err = parseNameServer(cfg.NameServer, cfg.RespectRules, cfg.PreferH3); err != nil {
@@ -1485,12 +1489,6 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
14851489
dnsCfg.Hosts = hosts
14861490
}
14871491

1488-
if cfg.CacheAlgorithm == "" || cfg.CacheAlgorithm == "lru" {
1489-
dnsCfg.CacheAlgorithm = "lru"
1490-
} else {
1491-
dnsCfg.CacheAlgorithm = "arc"
1492-
}
1493-
14941492
return dnsCfg, nil
14951493
}
14961494

dns/resolver.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,18 @@ type Config struct {
459459
Hosts *trie.DomainTrie[resolver.HostValue]
460460
Policy []Policy
461461
CacheAlgorithm string
462+
CacheMaxSize int
462463
}
463464

464465
func (config Config) newCache() dnsCache {
465-
if config.CacheAlgorithm == "" || config.CacheAlgorithm == "lru" {
466-
return lru.New(lru.WithSize[string, *D.Msg](4096), lru.WithStale[string, *D.Msg](true))
467-
} else {
468-
return arc.New(arc.WithSize[string, *D.Msg](4096))
466+
if config.CacheMaxSize == 0 {
467+
config.CacheMaxSize = 4096
468+
}
469+
switch config.CacheAlgorithm {
470+
case "arc":
471+
return arc.New(arc.WithSize[string, *D.Msg](config.CacheMaxSize))
472+
default:
473+
return lru.New(lru.WithSize[string, *D.Msg](config.CacheMaxSize), lru.WithStale[string, *D.Msg](true))
469474
}
470475
}
471476

hub/executor/executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ func updateDNS(c *config.DNS, generalIPv6 bool) {
260260
DirectServer: c.DirectNameServer,
261261
DirectFollowPolicy: c.DirectFollowPolicy,
262262
CacheAlgorithm: c.CacheAlgorithm,
263+
CacheMaxSize: c.CacheMaxSize,
263264
}
264265

265266
r := dns.NewResolver(cfg)

0 commit comments

Comments
 (0)