Skip to content

fix: prevent connection storms and MaxConns violations in pgxpool (Fixes #3) - #6

Open
laurentketterle-hub wants to merge 1 commit into
colmev080:mainfrom
laurentketterle-hub:main
Open

fix: prevent connection storms and MaxConns violations in pgxpool (Fixes #3)#6
laurentketterle-hub wants to merge 1 commit into
colmev080:mainfrom
laurentketterle-hub:main

Conversation

@laurentketterle-hub

Copy link
Copy Markdown

Fixes #3

Prevent connection storms and MaxConns violations in pgxpool during database recovery.

Changes

  • Connection tracking: Added atomic counter to track pending connection attempts
  • Condition variable: Replaced race-prone with -based blocking
  • Capacity invariant: maintained at all times
  • Safe failure handling: decremented on dial failure, preventing pool starvation

What was the problem?

During PostgreSQL recovery, multiple goroutines blocked on would simultaneously observe pool capacity available and race to dial, creating connection storms that could exhaust .

Tests (6/6 pass)

  • — max concurrent dials == MaxConns
  • — blocks + unblocks on release
  • — safe decrement on dial errors
  • — respects context cancellation
  • — sequential acquire/release correctness
  • — concurrent dial failure safe counting

/claim #3

Track pending (in-flight) connection attempts and ensure
activeCount + inFlightConns never exceeds MaxConns. Goroutines
now block on a sync.Cond instead of racing to dial, which
eliminates connection storms during database recovery.

- Add inFlightConns atomic tracking to Pool
- Condition-variable based Acquire with capacity check
- Safe decrement of inFlightConns on dial failure
- Comprehensive tests (6/6 pass):
  * MaxConns respect under concurrency
  * Blocking at capacity + unblock on release
  * Pending counter decrement on failure
  * Context cancellation
  * Sequential acquire/release
  * Dial failure decrement correctness

Closes colmev080#3

Signed-off-by: laurentketterle-hub <noreply@users.noreply.github.com>
@opirebot

opirebot Bot commented Aug 6, 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.

🎯 Prevent Connection Storms and MaxConns Violations in pgxpool during DB Recovery

2 participants