Skip to content

[#3] Fix connection storm: prevent MaxConns violations during DB recovery - #8

Open
lincai505011-ops wants to merge 1 commit into
colmev080:mainfrom
lincai505011-ops:bounty-3
Open

[#3] Fix connection storm: prevent MaxConns violations during DB recovery#8
lincai505011-ops wants to merge 1 commit into
colmev080:mainfrom
lincai505011-ops:bounty-3

Conversation

@lincai505011-ops

Copy link
Copy Markdown

/claim #3

What

Fix connection storm in pgxpool.Pool.Acquire() that allowed MaxConns violations 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 exiting waitForConn would all observe an empty pool and simultaneously initiate connection creation, exceeding MaxConns.

Fix

  • Added totalConns int32 (atomic) to track ALL connections: idle + in-use + in-flight
  • Acquire() now checks totalConns + inFlightConns >= maxConns before allowing new connection creation
  • inFlightConns is properly decremented on dial failure to prevent pool starvation
  • Added sync.Cond for efficient blocking when pool is at capacity
  • Added comprehensive test coverage

Tests (all passing)

  • TestMaxConnsNotExceeded — 50 concurrent goroutines, maxConns=5, never exceeded
  • TestRecoveryAfterFailure — dial failures correctly decrement counters, subsequent acquire succeeds
  • TestInFlightCounterDecrementedOnFailure — all-failure scenario resets counters to 0
  • TestConnectionStormPrevention — recovery burst never exceeds maxConns (max 5 concurrent dials)
  • TestContextCancellation — cancelled context returns error
  • TestPoolClosed — closed pool returns ErrPoolClosed

…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
@opirebot

opirebot Bot commented Aug 7, 2026

Copy link
Copy Markdown

😅 Unfortunately there are no rewards left to claim in this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant