Skip to content

True connection floor for the pool#1323

Open
shatilov-diman wants to merge 6 commits into
MagicStack:masterfrom
shatilov-diman:feat/min_pool
Open

True connection floor for the pool#1323
shatilov-diman wants to merge 6 commits into
MagicStack:masterfrom
shatilov-diman:feat/min_pool

Conversation

@shatilov-diman

@shatilov-diman shatilov-diman commented May 18, 2026

Copy link
Copy Markdown

Introduce min_size as a true connection floor for the pool

Background

Previously, Pool.min_size controlled only how many connections were opened at startup — it had no effect on the pool's steady-state size. Once connections expired via max_inactive_connection_lifetime, the pool could drain to zero regardless of min_size.

What changed

min_size / init_size split — the single min_size parameter is split into two distinct concepts:

Parameter Meaning
init_size Number of connections opened when the pool is created (replaces the old min_size role)
min_size Minimum number of live connections the pool must maintain at all times

Connection floor enforcement — when max_inactive_connection_lifetime fires and the pool size is at or below min_size, the holder re-schedules the expiry callback instead of closing the connection. This keeps a warm connection floor without blocking or reconnecting on demand.

New accessorPool.get_init_size() returns the configured init_size.

Validation — the constructor now rejects invalid combinations:

  • init_size < 0
  • init_size > max_size
  • init_size < min_size
  • min_size > max_size (pre-existing, unchanged)

Migration

Old code:

await asyncpg.create_pool(..., min_size=5, max_size=10)

New code — keep old behaviour (no floor):

await asyncpg.create_pool(..., init_size=5, min_size=0, max_size=10)

New code — with a connection floor of 2:

await asyncpg.create_pool(..., init_size=5, min_size=2, max_size=10)

Tests added

  • test_pool_min_size_keeps_connections_alive — floor prevents expiry when pool_size == min_size
  • test_pool_min_size_partial_keep — excess connections expire; floor connections survive
  • test_pool_min_size_zero_allows_full_expirymin_size=0 restores the old drain-to-zero behaviour
  • test_pool_min_size_validation — all four invalid parameter combinations raise ValueError
  • test_pool_init_size_and_min_size_gettersget_init_size() / get_min_size() return correct values
  • test_pool_min_size_reconnect_after_expiry — a floor-kept connection remains functional

Issues

Closes #1268

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

Labels

None yet

1 participant