[#3] Fix connection storm: prevent MaxConns violations during DB recovery - #8
Open
lincai505011-ops wants to merge 1 commit into
Open
[#3] Fix connection storm: prevent MaxConns violations during DB recovery#8lincai505011-ops wants to merge 1 commit into
lincai505011-ops wants to merge 1 commit into
Conversation
…nns violations during DB recovery - Added totalConns counter (atomic) to track all connections (idle + in-use) - Acquire now checks totalConns + inFlightConns against MaxConns - inFlightConns properly decremented on dial failure (prevents pool starvation) - Added sync.Cond for efficient blocking when pool is at capacity - Added 6 tests: MaxConns invariant, recovery after failure, inFlight cleanup, connection storm prevention, context cancellation, pool close
|
😅 Unfortunately there are no rewards left to claim in this issue! |
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.
/claim #3
What
Fix connection storm in
pgxpool.Pool.Acquire()that allowedMaxConnsviolations during database recovery.Root Cause
The original code used
len(p.conns)to check pool capacity, but this only counts idle connections currently in the pool — not connections that have been checked out. During DB recovery, multiple goroutines exitingwaitForConnwould all observe an empty pool and simultaneously initiate connection creation, exceedingMaxConns.Fix
totalConns int32(atomic) to track ALL connections: idle + in-use + in-flightAcquire()now checkstotalConns + inFlightConns >= maxConnsbefore allowing new connection creationinFlightConnsis properly decremented on dial failure to prevent pool starvationsync.Condfor efficient blocking when pool is at capacityTests (all passing)
TestMaxConnsNotExceeded— 50 concurrent goroutines, maxConns=5, never exceededTestRecoveryAfterFailure— dial failures correctly decrement counters, subsequent acquire succeedsTestInFlightCounterDecrementedOnFailure— all-failure scenario resets counters to 0TestConnectionStormPrevention— recovery burst never exceeds maxConns (max 5 concurrent dials)TestContextCancellation— cancelled context returns errorTestPoolClosed— closed pool returns ErrPoolClosed