fix(sns): collapse concurrent cert-cache misses via singleflight (bug-bash #1/#9)#231
Merged
Merged
Conversation
…-bash #1/#9) snsVerifier.getCert read the cert cache under RLock and, on a miss, fetched the cert and wrote it under Lock — with nothing between the read-miss and the write. A burst of concurrent SNS deliveries that all miss the cache (cold start, or the instant after the 24h TTL expires) each fire their own fetchCert over the network and then race to overwrite the map: a thundering herd against the AWS cert endpoint. Collapse the miss path through a singleflight.Group keyed by certURL (same primitive already used in internal/cache/redis.go, team_summary.go) so a concurrent burst issues exactly ONE fetch between them; followers receive the leader's result. The fast-path RLock cache hit is unchanged. Test: TestSNSVerifyFinal2_GetCert_SingleflightCollapsesConcurrentMisses — 20 concurrent missing-cache getCert calls, fetch held open on a channel so all collapse onto one in-flight call; asserts exactly 1 fetchCert. Passes under -race; getCert 100% covered. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
What
snsVerifier.getCertread the cert cache underRLockand, on a miss, fetched the cert over the network and wrote it underLock— with nothing between the read-miss and the write. A burst of concurrent SNS deliveries that all miss the cache (cold start, or the instant after the 24h TTL expires) each fire their ownfetchCertand then race to overwrite the map: a thundering herd against the AWS cert endpoint.Fix
Collapse the miss path through a
singleflight.Groupkeyed bycertURL(the same primitive already used ininternal/cache/redis.go,team_summary.go,billing_usage.go) so a concurrent burst issues exactly one fetch between them; followers receive the leader's result. The fast-pathRLockcache hit is unchanged.Coverage
20 concurrent missing-cache
getCertcalls, fetch held open on a channel so all collapse onto one in-flight call; asserts exactly 1fetchCert.🤖 Generated with Claude Code