Retry transient pool-acquire timeouts with exponential backoff#104
Merged
Conversation
When a long-running worker task (heartbeat, validate_workers, leader, process, spawn_new_tasks, load_leader_id) hit a transient `sqlx::Error::PoolTimedOut` while acquiring a pool connection, the error propagated out of the task, the `ShutdownGuard` raised the shutdown flag on drop, and the entire worker tore itself down -- relying on the supervisor to rebuild it (worker row churn, re-acquiring leadership, re-claiming orphaned tasks) for what is often a momentary connection-pool spike. Add `SharedState::acquire`/`SharedState::begin` helpers that retry the pool operation with exponential backoff on transient pool-acquire timeouts, only escalating to a worker teardown once the condition is sustained. The backoff sleep goes through the runtime clock (so DST stays deterministic) and remains responsive to shutdown so a graceful shutdown is not delayed by the full backoff period. Retry behaviour is tunable via three new (defaulted, backwards-compatible) config options: `pool_acquire_max_retries` (default 5), `pool_acquire_backoff` (default 1s), and `pool_acquire_max_backoff` (default 30s). Setting `pool_acquire_max_retries` to 0 restores the previous fail-on-first-timeout behaviour. The task-execution paths (run_task/run_task_impl) already treat pool timeouts as recoverable by suspending the task, and task_cleanup/ stuck_notify already log-and-continue, so those are left unchanged. https://claude.ai/code/session_01D4z4t3D4r4pTAYcXCSK8Ex
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.
Summary
Add resilience to transient database connection pool timeouts by implementing automatic retry logic with exponential backoff instead of immediately tearing down the worker.
Key Changes
SharedStatemethods: Addedacquire()andbegin()methods that wrap the underlying pool operations with retry logic for transientPoolTimedOuterrorsPoolRetrystruct that tracks exponential backoff state and decides whether to retry or propagate errorssqlx::Error::PoolTimedOuterrorsConfig:pool_acquire_max_retries: Maximum number of retries (default: 5)pool_acquire_backoff: Initial backoff duration (default: 1 second)pool_acquire_max_backoff: Maximum backoff duration (default: 30 seconds)pool.acquire()andpool.begin()calls throughout the worker with the new resilient methodsnext_backoff()helper function to verify correct doubling behavior and overflow handlingImplementation Details
https://claude.ai/code/session_01D4z4t3D4r4pTAYcXCSK8Ex