Repository navigation
Count opening connections towards pool_size - #529
Merged
Dreamsorcerer merged 5 commits intoNov 24, 2026
Merged
Conversation
When the pool is empty, at start-up or after every connection was lost, each concurrent command opens its own connection, because a connection being opened does not count towards the pool size. 50 concurrent gets with pool_size=2 open 50 connections and close 48 of them. Since connections are discarded unless a command completes, the same happens each time a connection is replaced: while the replacement opens, size() drops, so every command that arrives opens a connection too. If the replacement then fails, its slot is put back next to a connection another command opened meanwhile, and size() exceeds pool_size. Also cover a failed connect and a connect cancelled while another command waits for it, so that a fix can't leave waiters hanging or leave a cancelled slot counted in size().
A connection that was still being opened did not count towards size(), so every concurrent acquire() on an empty pool, or while a discarded connection was being replaced, opened its own connection, and the extras were closed right after. Count them while they open, so callers that arrive meanwhile wait for a connection instead. Up to pool_size connections still open in parallel. If opening fails or is cancelled while other callers wait, hand one of them the free slot (None) so it tries to connect itself instead of waiting forever. This replaces the unconditional put back in acquire(), which could leave a slot queued next to a connection opened meanwhile. The hand-off runs on the next loop pass and only if a waiter is still unserved and the pool has room, so waiters cancelled at the same moment (for example by a shared deadline) don't leave a stale slot queued.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #529 +/- ##
==========================================
+ Coverage 97.97% 98.33% +0.35%
==========================================
Files 10 10
Lines 1037 1201 +164
Branches 57 63 +6
==========================================
+ Hits 1016 1181 +165
Misses 12 12
+ Partials 9 8 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
[Medium impact] The change affects connection use across memcache client commands. Fix the failed-open marker path before merging; commands can fail despite having a healthy idle connection. Reviews (3) · Last reviewed commit: "Keep a failed connection's slot instead ..." · Reviewed by Greptile |
With connections counted while they open, a pool with pool_minsize below pool_size stopped growing at all: the first command's connection made size() reach pool_minsize, so every other command waited for it. Before, the pool only grew past pool_minsize when concurrent commands on an empty pool each opened a connection, and once pool_minsize connections were open, a command that found them all busy waited instead of opening another. When no idle connection is queued and the pool has room, open a new connection instead of waiting. Up to pool_size connections still open in parallel, and never more than pool_size.
The failed-connect test used a missing Unix socket, which fails without yielding, so the acquire() calls failed one after another and none ever waited for another's connection; it passed even if a failed connect woke no waiter. The connect-count test only counted opens, so it also passed if connections opened one at a time. Gate the opens instead, and check that callers queue behind a pending open, that up to pool_size opens are in flight at once, and that a failed open hands its slot to each waiter in turn. Add a test that a failed connect does not hand its slot to a waiter when another command has already taken it, which would make size() exceed pool_size. Bound each scenario, setup included, with asyncio.timeout(), so a regression fails instead of hanging the suite, and cancel the tasks a test started (and close the spare connection) in a finally block.
Put None back in the queue as soon as opening a connection fails or is cancelled, as acquire() already did on reconnect. Drop the deferred _wake_waiter() and simplify the pool tests.
Contributor
Author
|
@Dreamsorcerer Thanks for the detailed feedback, this is great. I agree with all of it, and we've simplified it a lot in d831409. |
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.
Fixes #524.
A connection that was still being opened didn't count towards the pool size, so every concurrent command on an empty pool, or while a discarded connection was being replaced, opened its own connection, ignoring
pool_size. Connections are now counted while they open, so the pool never opens more thanpool_size, and still opens up topool_sizein parallel.Counting them exposed a second bug: with
pool_minsizebelowpool_size, the pool only grew pastpool_minsizethrough that storm of connections. Oncepool_minsizeconnections were open, a command that found them all busy waited for one instead of opening another, so e.g.Client(pool_size=10, pool_minsize=1)stayed at one connection under load.acquire()now opens a new connection when none is idle and the pool has room.size()includes connections being opened; the post-connect check that closed the surplus is gone.size() < pool_size,acquire()opens a new one instead of waiting.exceptinacquire()that put the slot back unconditionally, which could makesize()exceedpool_size.pool_sizeconnections at start-up, after losing every connection and after one connection is discarded, with the opens gated so they must overlap while the other commands wait; that the pool grows pastpool_minsizeup topool_size, both one command at a time and under concurrent load; that a failed connect hands its slot to each waiter in turn, but not when another command took the slot first; that a failed reconnect doesn't pushsize()overpool_size; and a cancelled connect. Each scenario runs underasyncio.timeout(), so a regression fails instead of hanging.size() == 1). With connections counted while they open, there is no placeholder: a failed reconnect leavessize() == 0, and the nextacquire()opens a connection. The test now asserts that.