feat: jitter dns cache ttl to avoid expiry bursts - #110
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
客户端 DNS 查询结果缓存后存在一个突出问题:大量域名(尤其是 TTL 被 clamp 到统一下限/上限的条目)的缓存几乎在同一时刻过期,导致短时间内爆发大量 DNS 查询请求。无论从性能(上游 DNS 服务器瞬时压力)还是流量特征(突发的查询模式容易被识别)角度都不友好。
改动
client/dns/cache.go:新增jitterTTL(),写入缓存时的实际存活时间 = 基础 TTL + 随机(0, 基础 TTL),即最终 TTL 落在[基础TTL, 2×基础TTL)区间,让过期时刻均匀错开。client/dns/cache_test.go:新增TestJitterTTL_NeverExpire(服务器域名永不过期语义不变)与TestJitterTTL_Range(1000 次采样结果均落在[base, 2×base)且取值分散)。设计要点
minCacheTTL的条目会被重新统一 clamp 成同一个 30 分钟,抖动完全失效。dnsCacheTTL保持纯函数,clamp 逻辑与既有测试不变。math/rand/v2顶层rand.IntN,并发安全,无需额外加锁。验证
go test -v ./client/dns/...全部通过(14 个测试)make lint0 issues