Add opt-in reject_throttle: pace connection opens after establishment failures#1180
Add opt-in reject_throttle: pace connection opens after establishment failures#1180fabio-vapi wants to merge 1 commit into
reject_throttle: pace connection opens after establishment failures#1180Conversation
25949aa to
6469acb
Compare
…lures Opt-in (default off; off is behaviourally identical to today). When a connection fails to ESTABLISH, the pool otherwise retries every connection immediately and in lockstep — stampeding a server that is already rejecting connections. With reject_throttle enabled the pool keeps a single failed connection as the lone prober (retrying with backoff), blocks the rest, and on each prober handshake success admits an exponentially growing batch of waiters (slow-start, capped at `max`), disengaging once the backlog drains. This complements the existing per-connection `backoff`: backoff only delays each retry, and since its counter is pool-shared every connection waits the same delay and then fires together — a delayed herd. reject_throttle instead keeps exactly one attempt in flight during an outage. It governs only the clean-close establishment-reconnect path (`if (initial) return reconnect()`); error-close (RST) and connect-timeout still reject as before. Tests in tests/reject_throttle.js (21 cases): option plumbing, throttle-vs-herd, single prober, ramp collapse, recovery and disengagement (including large backlogs and max:1), drop-path intact via max_lifetime, and boundaries (RST reject, connect_timeout, off == vanilla).
6469acb to
0ecb7e7
Compare
|
Hi @porsager, a gentle ping on this when you get a chance, no rush. It's an opt-in For motivation: we hit a production outage on Neon that came down to this. Admittedly our connection pool was oversized, which only exacerbated it, but the problem is fundamentally there regardless. Even a sensibly sized pool retries every connection at once with no backoff, so as soon as the endpoint starts dropping connections during establishment the whole pool stampedes it. The PR ships a dedicated 21-case test suite, and it passes across every Node and Postgres version in the matrix (verified green on my fork; the checks here are just waiting on your workflow approval, since it's my first contribution). Happy to adjust the naming, design, or scope to whatever fits the project. Thanks for postgres.js! |
Fixes #1179.
Adds an opt-in
reject_throttle(default off; when off, behaviour is identical to today) that fixes the establishment-failure herd described in the issue.What it does
backoffcurve instead of retrying immediately. (Todayclosed()'sif (initial) return reconnect()returns beforeclosedTime/delayare assigned, soreconnect()computessetTimeout(connect, 0)— a tight loop.)1 → 2 → 4 → … → max); a failed admission collapses the ramp back to 1. Once the backlog drains, the breaker disengages back to baseline. Liveness is the prober's own retry — no timers.It complements the existing
backoffrather than duplicating it:backoffonly delays each retry, and because its counter lives on the shared pool object, every connection waits the same delay and then fires together — a delayed herd.reject_throttlekeeps exactly one attempt in flight during an outage and ramps back up on recovery.It governs only the clean-close establishment-reconnect path; error-close (RST) and connect-timeout still reject exactly as before, so it never masks a hard failure. With it enabled, the issue's reproduction drops from ~12.5k attempts/sec to single digits and recovers via slow-start once the endpoint accepts again.
Changes
src/index.js— thereject_throttleoption andshared.throttlestate; the gate (throttleAllows) at the threeclosed → connectingpromotion sites;throttleAdmit(slow-start admission + disengage) inonopen;requeue.src/connection.js— establishment-failure routing (reconnect_with_reject_throttle→prober_reconnect|stand_down),backOffOnError, andthrottle_on_successatReadyForQuery.Tests
tests/reject_throttle.js— 21 cases: option plumbing, throttle-vs-herd, single prober, slow-start recovery and disengagement (including large backlogs andmax: 1), the drop path staying intact viamax_lifetime, and the boundaries (RST reject, connect_timeout,off == vanilla). Wired intotest:esm; kept as a separate file for reviewability — happy to fold intotests/index.jsif you'd prefer.