← Back to Pull requests

Hands out the most recently returned connection first in connection_pool #546

Closed
unknown closed this pull request without merging on Nov 17, 2026 11:48 UTC
View on GitHub
unknown unknown commented on Nov 11, 2026 19:00 UTC
Author

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_timeout elapsed, 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_list is a new internal class that owns the order, with its own unit tests. Besides empty and size, 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 calls push_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_interval at its default of 10 seconds, every idle connection would be used long before a realistic idle_timeout elapsed, and the pool wouldn't shrink.

    Why a set. The list numbers returned connections with a counter, and keeps them in a boost::intrusive::multiset ordered by that number. A connection that gets back from a ping may belong anywhere in the order:

  • With a multiset, restore is O(log n). back is O(1), and push_back and erase take constant time on average.

  • With an intrusive::list, restore has to walk past every connection returned after the one being restored. Every idle connection pings once per ping_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_t that 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, and push_back is an insertion with end() as the hint, not an unchecked one.

    Behaviour change

    This applies to every pool, also with idle_timeout disabled. 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 has server_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_list covers 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.

  • No existing test depended on the hand-out order.
  • 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_list and 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 drain test helper with a non-mutating get_ids.

    🤖 Generated with Claude Code

    Diff: 577 lines (+432 / -145) across 7 files Reviewer: anarthal Review duration: 8208m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%
    Repository