Hands out the most recently returned connection first in connection_pool #546
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 577 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.380
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #546"
general
weight: 0.35
anarthal
Closes #545. Follow-up to #538.
The pool handed out idle connections from the front of the idle list and put returned ones at its back, so requests cycled through every connection. Under a steady trickle of requests, each connection was used before
idle_timeoutelapsed, and the pool only shrank once the load dropped. The pool now hands out the most recently returned connection, so the connections that aren't needed stay idle and get closed.Design
In #538 you said the first attempt needed some encapsulation, and maybe another data structure (comment). This PR does both.
idle_connection_listis a new internal class that owns the order, with its own unit tests. Besidesemptyandsize, it has four operations:push_back: the connection has just been returned, and becomes the most recently returned one.back: the most recently returned connection.erase: the connection leaves the list, and remembers its place.restore: the connection gets back to the place it had.The node only tells the list how a connection became idle. After a ping it calls
restore. In any other case (connect, reconnect, reset, return without reset) it callspush_back. This is the condition that already decides whether the idle period restarts, so the hand-out order and the close deadline follow the same rule.Why the order has to survive pings. A ping takes a connection out of the idle list and puts it back. If that made it the most recently returned connection, the next request would get it. With
ping_intervalat its default of 10 seconds, every idle connection would be used long before a realisticidle_timeoutelapsed, and the pool wouldn't shrink.Why a set. The list numbers returned connections with a counter, and keeps them in a
boost::intrusive::multisetordered by that number. A connection that gets back from a ping may belong anywhere in the order:restoreis O(log n).backis O(1), andpush_backanderasetake constant time on average.intrusive::list,restorehas to walk past every connection returned after the one being restored. Every idle connection pings once perping_interval, so this adds up to O(n²) node visits per interval.The cost is one more pointer per node. If you'd rather keep the list and accept the walk, the change stays inside
idle_connection_list.Why a counter instead of the time of return. It has no ties, and it keeps the list independent of the clock type. It's a
std::uint64_tthat only advances when a connection is returned (pings don't touch it), so it doesn't wrap in practice. If it did, connections would be handed out in the wrong order, but the list would stay valid: it's a multiset, andpush_backis an insertion withend()as the hint, not an unchecked one.Behaviour change
This applies to every pool, also with
idle_timeoutdisabled. Requests now concentrate on the most recently used connections, and the others are only exercised by pings. That is what lets the pool shrink, and it's pgbouncer's default too. pgbouncer also hasserver_round_robin, for servers behind a load-balanced address. I haven't added an option, since in #538 you preferred changing the order over adding strategies. Tell me if you want one.Requests waiting for a connection are still served in arrival order.
Tests
test_idle_connection_listcovers the ordering rules, including elements restored after their neighbours were returned or removed.get_connection_most_recently_returned_first, in the pool tests, returns two connections one second apart, and checks that a request gets the second one, before and after the first one pings. It fails if the least recently returned connection is handed out, and if a pinged connection is treated as a returned one.Built and run against Boost
develop, with warnings as errors: clang 21 (C++11, and C++20 header-only) and gcc 15 (C++23, debug and release).Commits
1. Adds
idle_connection_listand its tests. The pool doesn't use it yet.2. Switches the pool to it, and hands out the most recently returned connection.
3. Docs: replaces the limitation note in the pool size section.
After the first review:
4. Makes the list valid even if the counter wraps (
multiset, hinted insertion).5. Checks the hand-out order before the ping in the pool test.
6. Replaces the
draintest helper with a non-mutatingget_ids.🤖 Generated with Claude Code