feat(cache): 实现并发安全的 DNS 响应缓存机制

新增基于 sync.Map 的缓存结构,支持 TTL 管理与定时清理。
完善缓存键生成逻辑,包含 DO/CD 标志避免上下文污染。
增强缓存读写处理:
- 自动跳过伪 RR(OPT/TSIG)防止干扰 TTL 计算
- 写入前剥离传输层细节提升通用性
- 支持负面缓存(NXDOMAIN/NODATA)并遵循 RFC 2308
- 回填剩余 TTL 并确保对外不可变

优化上游查询模块:
- 并发向多个上游竞速,支持 UDP 截断后 TCP 回退
- 过滤不良 RCODE(SERVFAIL/REFUSED/FORMERR)提升稳定性
- 使用信号量控制最大并发数,改善资源利用率
- 快速失败机制减少无效等待

其他改进:
- 完善日志记录,区分缓存命中/未命中及响应内容
- 显式构建客户端兼容的 EDNS0 选项
- 增加注释说明关键设计决策和行为边界
```
This commit is contained in:
2025-10-14 10:52:57 +08:00
parent d540b302f1
commit 05d3be286e
4 changed files with 182 additions and 52 deletions

13
go.mod
View File

@@ -3,10 +3,19 @@ module dot
go 1.25.2
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/miekg/dns v1.1.68 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.23.2 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.66.1 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/net v0.40.0 // indirect
golang.org/x/net v0.43.0 // indirect
golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/tools v0.33.0 // indirect
google.golang.org/protobuf v1.36.8 // indirect
)