From ae1ba45350eee08a48adc8ef2827edd6107e3621 Mon Sep 17 00:00:00 2001 From: yangjianbo Date: Fri, 6 Feb 2026 21:22:38 +0800 Subject: [PATCH] =?UTF-8?q?perf(service):=20jitterTTL=20=E6=94=B9=E7=94=A8?= =?UTF-8?q?=20rand/v2=20=E5=B9=B6=E7=A7=BB=E9=99=A4=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/service/api_key_auth_cache_impl.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/backend/internal/service/api_key_auth_cache_impl.go b/backend/internal/service/api_key_auth_cache_impl.go index 9a7f62e4..4bf84ddd 100644 --- a/backend/internal/service/api_key_auth_cache_impl.go +++ b/backend/internal/service/api_key_auth_cache_impl.go @@ -22,11 +22,6 @@ type apiKeyAuthCacheConfig struct { singleflight bool } -var ( -// 认证缓存抖动直接使用 rand/v2 的顶层函数。 -// rand/v2 顶层函数并发安全,避免全局互斥锁成为热点。 -) - func newAPIKeyAuthCacheConfig(cfg *config.Config) apiKeyAuthCacheConfig { if cfg == nil { return apiKeyAuthCacheConfig{} @@ -54,6 +49,8 @@ func (c apiKeyAuthCacheConfig) negativeEnabled() bool { return c.negativeTTL > 0 } +// jitterTTL 为缓存 TTL 添加抖动,避免多个请求在同一时刻同时过期触发集中回源。 +// 这里直接使用 rand/v2 的顶层函数:并发安全,无需全局互斥锁。 func (c apiKeyAuthCacheConfig) jitterTTL(ttl time.Duration) time.Duration { if ttl <= 0 { return ttl