From 66fd5c9f0cee1f89d16cd80d2eaf6f47322c3bbd Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 27 Jul 2025 10:31:12 +0800 Subject: [PATCH] chore: allow setting `cache-max-size` in `dns` section --- config/config.go | 10 ++++------ dns/resolver.go | 13 +++++++++---- hub/executor/executor.go | 1 + 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index dae686df..77fdd4d6 100644 --- a/config/config.go +++ b/config/config.go @@ -156,6 +156,7 @@ type DNS struct { EnhancedMode C.DNSMode DefaultNameserver []dns.NameServer CacheAlgorithm string + CacheMaxSize int FakeIPRange *fakeip.Pool Hosts *trie.DomainTrie[resolver.HostValue] NameServerPolicy []dns.Policy @@ -223,6 +224,7 @@ type RawDNS struct { FakeIPFilterMode C.FilterMode `yaml:"fake-ip-filter-mode" json:"fake-ip-filter-mode"` DefaultNameserver []string `yaml:"default-nameserver" json:"default-nameserver"` CacheAlgorithm string `yaml:"cache-algorithm" json:"cache-algorithm"` + CacheMaxSize int `yaml:"cache-max-size" json:"cache-max-size"` NameServerPolicy *orderedmap.OrderedMap[string, any] `yaml:"nameserver-policy" json:"nameserver-policy"` ProxyServerNameserver []string `yaml:"proxy-server-nameserver" json:"proxy-server-nameserver"` DirectNameServer []string `yaml:"direct-nameserver" json:"direct-nameserver"` @@ -1352,6 +1354,8 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul IPv6: cfg.IPv6, UseSystemHosts: cfg.UseSystemHosts, EnhancedMode: cfg.EnhancedMode, + CacheAlgorithm: cfg.CacheAlgorithm, + CacheMaxSize: cfg.CacheMaxSize, } var err error 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 dnsCfg.Hosts = hosts } - if cfg.CacheAlgorithm == "" || cfg.CacheAlgorithm == "lru" { - dnsCfg.CacheAlgorithm = "lru" - } else { - dnsCfg.CacheAlgorithm = "arc" - } - return dnsCfg, nil } diff --git a/dns/resolver.go b/dns/resolver.go index adcea094..f5f69c5f 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -459,13 +459,18 @@ type Config struct { Hosts *trie.DomainTrie[resolver.HostValue] Policy []Policy CacheAlgorithm string + CacheMaxSize int } func (config Config) newCache() dnsCache { - if config.CacheAlgorithm == "" || config.CacheAlgorithm == "lru" { - return lru.New(lru.WithSize[string, *D.Msg](4096), lru.WithStale[string, *D.Msg](true)) - } else { - return arc.New(arc.WithSize[string, *D.Msg](4096)) + if config.CacheMaxSize == 0 { + config.CacheMaxSize = 4096 + } + switch config.CacheAlgorithm { + case "arc": + return arc.New(arc.WithSize[string, *D.Msg](config.CacheMaxSize)) + default: + return lru.New(lru.WithSize[string, *D.Msg](config.CacheMaxSize), lru.WithStale[string, *D.Msg](true)) } } diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 386be37a..c6bef525 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -260,6 +260,7 @@ func updateDNS(c *config.DNS, generalIPv6 bool) { DirectServer: c.DirectNameServer, DirectFollowPolicy: c.DirectFollowPolicy, CacheAlgorithm: c.CacheAlgorithm, + CacheMaxSize: c.CacheMaxSize, } r := dns.NewResolver(cfg)