Skip to content

Support :max_lifetime, :idle_limit, and :checkout_retries DBConnection options - #329

Open
eltonplima wants to merge 2 commits into
commanded:masterfrom
eltonplima:add-missing-dbconnection-opts
Open

eltonplima wants to merge 2 commits into
commanded:masterfrom
eltonplima:add-missing-dbconnection-opts

Conversation

@eltonplima

Copy link
Copy Markdown

Summary

Three DBConnection options added after EventStore's @postgrex_connection_opts allowlist was last updated are silently dropped when configuring an event store. This PR adds them.

:idle_limit (DBConnection v2.4.3, Nov 2022)

Caps how many idle connections are disconnected during a single ping interval. Without this option, all idle connections in a large pool can be pinged and recycled simultaneously, causing a thundering-herd reconnect spike.

Added to @postgrex_connection_opts — safe for all pools.

:checkout_retries (DBConnection v2.9.0, Jan 2026)

Retries a pool checkout when the queue times out, rather than immediately returning an error. Improves resilience under burst traffic.

Added to @postgrex_connection_opts — safe for all pools.

:max_lifetime (DBConnection v2.10.0, Apr 2026)

Recycles connections that exceed a configurable age (supports a min..max range for jitter). This is the idiomatic way to prevent long-lived connections from accumulating stale state or hitting infrastructure-level idle timeouts (e.g. RDS Proxy).

Intentionally restricted to the main pool only via a new @main_pool_only_opts list. It must not flow through default_postgrex_opts/1, which is shared by two other pools:

  • Advisory locks pool (backoff_type: :stop): this pool is deliberately configured to stop — not restart — when its connection drops, so that subscriptions can detect the lost distributed lock. If :max_lifetime were forwarded here, each recycle cycle would permanently terminate the advisory lock process, breaking distributed subscription assignment.
  • Notifications pool: holds LISTEN registrations that are dropped on disconnect. Recycling would silently miss events until the subscription reconnects and re-registers.

postgrex_opts/2 (the main pool) uses @postgrex_connection_opts ++ @main_pool_only_opts so :max_lifetime is available there, while default_postgrex_opts/1 continues to use only @postgrex_connection_opts.

Checklist

  • mix format passes
  • Tests pass (no functional logic changed; existing test suite covers config option forwarding)

Add three DBConnection options that were previously silently dropped by
the @postgrex_connection_opts allowlist in EventStore.Config.postgrex_opts/2.

:idle_limit (DBConnection v2.4.3) caps the number of connections
disconnected during a single idle ping interval, preventing thundering-herd
reconnects on large pools.

:checkout_retries (DBConnection v2.9.0) retries a pool checkout on
transient queue timeouts instead of immediately returning an error,
improving resilience under burst traffic.

:max_lifetime (DBConnection v2.10.0) enables native per-connection
recycling by disconnecting connections that exceed a configurable age.
This is intentionally restricted to the main connection pool via a
separate @main_pool_only_opts list and is NOT forwarded to
default_postgrex_opts, which feeds both the advisory locks pool and
the notifications pool:

- Advisory lock connections use backoff_type: :stop so that a lost
  connection terminates the process, signalling to subscribers that
  the distributed lock has been released. If :max_lifetime were
  forwarded here, each recycle cycle would terminate the advisory lock
  process permanently, breaking distributed subscription assignment.

- Notification connections hold LISTEN registrations that would be
  silently dropped on each recycle cycle, causing missed events until
  the subscription reconnects and re-registers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@eltonplima
eltonplima marked this pull request as ready for review September 28, 2026 15:20
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@eltonplima
eltonplima force-pushed the add-missing-dbconnection-opts branch from ced1698 to 672fd0b Compare September 29, 2026 09:10
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