Skip to content

Implementation of ConnectionPool and HTTP client pool as a centralized state machine - #6587

Merged
vietj merged 18 commits into
masterfrom
default-connection-pool
Dec 1, 2026
Merged

vietj merged 18 commits into
masterfrom
default-connection-pool

Conversation

@vietj

@vietj vietj commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Reimplement Vert.x ConnectionPool interface to develop new features:

  • dynamic configuration (e.g. maximum number of connections)
  • actual state versus desired state (e.g. minimum number of connections)
  • connection failure retry policies

Such features are not yet leveraged in HTTP client that requires a few other changes before.

Related issues

@vietj vietj added this to the 5.3.0 milestone Nov 24, 2026
@franz1981

Copy link
Copy Markdown
Contributor

Nice direction, the single progress() is much easier to reason about than the per-action logic.

Two behaviour differences vs SimpleConnectionPool I hit while comparing them (the existing suite doesn't cover either):

  1. Cold burst with maxWaiters < max size. pool(mgr, {10}, 2) and 4 acquires before any connect completes: Simple opens 4 connects, Default opens 2 and rejects the other 2 with ConnectionPoolTooBusyException. Connecting waiters now sit in the wait queue, so they count against maxWaiters.
  2. maxWaiters = 0. A single acquire on an empty pool is rejected; Simple connects. isWaitersQueueFull() is true before the submission can be admitted.

Maybe the bound should apply to waiters.size() - requests, like waiters() already reports?

A few questions:

  • Connecting waiters are no longer bound to their connect, so they can take a recycled connection first. I like it. It also means a connect failure now fails the queue head rather than the initiator. Is that the intended default policy?
  • Since actions are now pure recordings, would it make sense to run progress() once per combiner drain instead of once per action? Under contention that would apply all the pending recycles before matching waiters.
  • Recycle now scans the slots on each progress(), and admitted is allocated on every acquire, including the immediate-grant path. I haven't measured either, happy to do it once the shape settles.
  • Is the waiter selection order (FIFO, stop at the first unsatisfiable waiter) meant to become pluggable too, or only the failure policy?

@vietj
vietj force-pushed the default-connection-pool branch 5 times, most recently from 40604ad to 47f960b Compare November 26, 2026 09:35
@vietj

vietj commented Nov 26, 2026

Copy link
Copy Markdown
Member Author
  • Connecting waiters are no longer bound to their connect, so they can take a recycled connection first. I like it. It also means a connect failure now fails the queue head rather than the initiator. Is that the intended default policy?

Yes that is the intended behavior and actually we might go even further, we could require every submission to the waiter queue to have a timeout and rely on that instead. Currently we are dequeuing from the waiter list just to make progress because there is no guaranteed timeout per waiter.

@vietj

vietj commented Nov 26, 2026

Copy link
Copy Markdown
Member Author
  • Since actions are now pure recordings, would it make sense to run progress() once per combiner drain instead of once per action? Under contention that would apply all the pending recycles before matching waiters.

that's a good point, I'll investigate it. I think that each progress call has a impact on the state mutation done by the action state mutation.

@vietj

vietj commented Nov 26, 2026

Copy link
Copy Markdown
Member Author
  • Recycle now scans the slots on each progress(), and admitted is allocated on every acquire, including the immediate-grant path. I haven't measured either, happy to do it once the shape settles.

we could recycle data structures on the queue I think to avoid allocation, I'll take care of that.

@vietj

vietj commented Nov 26, 2026

Copy link
Copy Markdown
Member Author
  • Is the waiter selection order (FIFO, stop at the first unsatisfiable waiter) meant to become pluggable too, or only the failure policy?

that's to be discussed, usually we try to do based on submission order to the queue but it could be based on other criteria, not sure it's a real use case but I may overlook

@vietj

vietj commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

Two behaviour differences vs SimpleConnectionPool I hit while comparing them (the existing suite doesn't cover either):

  1. Cold burst with maxWaiters < max size. pool(mgr, {10}, 2) and 4 acquires before any connect completes: Simple opens 4 connects, Default opens 2 and rejects the other 2 with ConnectionPoolTooBusyException. Connecting waiters now sit in the wait queue, so they count against maxWaiters.
  2. maxWaiters = 0. A single acquire on an empty pool is rejected; Simple connects. isWaitersQueueFull() is true before the submission can be admitted.

I'll investigate.

@vietj
vietj force-pushed the default-connection-pool branch from 47f960b to c1a612f Compare November 26, 2026 09:59
@vietj vietj changed the title Implementation of ConnectionPool has a centralized state machine Implementation of ConnectionPool as a centralized state machine Nov 26, 2026
@tsegismont

Copy link
Copy Markdown
Member

@vietj as discussed offlist, can you check my last comment in eclipse-vertx/vertx-sql-client#1807

Perhaps we need a new callback in the pool to handle timeout properly.

@tsegismont

Copy link
Copy Markdown
Member

@vietj another point we discussed, perhaps adding some sort of statistics on the lease (e.g. timestamp when a connection goes idle) would allow 1/ to extract information from the pool that the metrics spi cannot give 2/ to implement nicely downstream features like max idle time in sql connection pool

@vietj
vietj marked this pull request as ready for review December 1, 2026 09:16
@vietj vietj changed the title Implementation of ConnectionPool as a centralized state machine Implementation of ConnectionPool and HTTP client pool as a centralized state machine Dec 1, 2026
@vietj vietj self-assigned this Dec 1, 2026
vietj added 10 commits December 1, 2026 11:28
The expiration timestamp of an HTTP/2 or HTTP/3 client connection is
only set when a stream closes: a freshly established connection reports
itself expired until its first stream closes. The periodic expiry check
of the connection pool masks it in practice; an eager check evicts and
closes the connection before it serves its first stream.

Set the expiration timestamp at creation for the HTTP/2 codec, HTTP/2
multiplex and HTTP/3 implementations.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

The codec client channel initializer handles a successful clear text
upgrade by falling through the rejected case: UpgradeResult's
upgradeRejected is signalled on success as a pipeline cleanup and sets
the upgrade processed flag, polluting the rejection semantics. The
multiplex initializer signals the rejection only on rejection, so a
successful multiplex upgrade never marks the upgrade processed: the
upgraded connection reports a concurrency of one.

Changes:

A successful upgrade marks the upgrade processed when the upgraded
connection is swapped in; the codec initializer separates the
successful case - the pipeline cleanup - from the rejected case - the
rejection signal. A successfully upgraded connection reports the HTTP/2
concurrency.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

LazyFuture and the POOL_CLOSED future are not used anymore.

Changes:

Remove them.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

Reimplement the connection pool state machine declaratively: actions
record state changes and a single method progresses the pool toward its
desired state. Starting from a verbatim copy, each subsequent change
reviews as a plain diff against SimpleConnectionPool.

Changes:

Copy SimpleConnectionPool to DefaultConnectionPool, construction exposed
by pool(...) static factories and a builder. Route pool construction in
ConnectionPoolTest through overridable methods and run the same suite
against the new implementation with DefaultConnectionPoolTest.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

Building a task list juggles a head/tail pair of locals, repeating the
append lines at every site.

Changes:

A TaskList builds the task list with an append method returning the
first task, used wherever a task list is built.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

Concurrent acquisitions bet on a connection kind, the actual kind is
known on connect success only: when the back-end establishes heavier
connections, the pool temporarily exceeds its maximum capacity. This
expected outlier is accepted and was not pinned by the test suite.

Changes:

Add testCapacityOvershoot to the shared suite: five concurrent
acquisitions of the first kind established as the heavier kind exceed
the maximum capacity and are all served; nothing is created while the
pool is over capacity, the next acquisition waits.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

Actions record state changes and a single progress method makes the
decisions, instead of duplicating the logic across actions.

Changes:

The wait queue is the demand: connecting waiters stay in it, covered by
the in-flight connects, and a connect no longer references its
initiating waiter. Actions record their state change and delegate to
progress(), made of flat sections: the connect failure policy (the
default fails one pending acquisition per recorded failure), the
satisfaction loop (selector grant, connection creation for uncovered
demand, fallback grant), the admission of submitted acquisitions
bounded by maxWaiters, the onEnqueue notifications and the connect
result signalling. Cancel succeeds iff the acquisition is not settled,
and settles it.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
StressTest builds its pool through an overridable method and
DefaultConnectionPoolStressTest runs the same test against
DefaultConnectionPool.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
The pool is refreshed periodically to check and perform the maintenance
tasks to do: expired connection eviction, and later connect retry
backoff or minimum connections maintenance. Depending on the
granularity of the period, deadlines might be passed, which is
acceptable. The implementations are empty for now.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

Periodic pool maintenance should not run in the middle of other changes
being applied, and detections like the connections to close should
happen at the very end of an execution round, on the settled state.

Changes:

Executor.trySubmit executes an action only when the ownership of the
execution can be acquired (acquire first then enqueue), otherwise does
nothing - fine for idempotent periodic maintenance since any in-flight
execution performs the same work. CombinerExecutor accepts an optional
Supplier<Action<S>> finalAction, evaluated at the end of each execution
round with exclusive access, after the submitted actions, its tasks
running after the other tasks; the supplier decides (can return null).
A refresh then reduces to try submitting a no-op action.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
vietj added 7 commits December 1, 2026 11:28
Motivation:

A connection that is not valid is expired: the notion of expiration
belongs to the connector. Evicting the stale connections before any
demand is served establishes the invariant that within an execution
round, every action operates on valid connections - the first progress
of the round checks, the others assume.

Changes:

PoolConnector gains a close method to close the connections evicted by
the pool. The first progress of an execution round evicts the stale
connections and closes them, gated by a first flag re-armed by the
final action at the end of the round. refresh() try submits a progress
nudge: when the combiner is busy, the in-flight round already performed
the eviction.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

The connection acquisition timeout is implemented by the pool user with
a timer and a cancellation. The pool can check the deadlines itself at
the beginning of each execution round, like the stale connections.

Changes:

acquire overloads take a timeout, recorded as a nano time deadline on
the waiter; the default implementations ignore it. At the beginning of
an execution round, the pool removes the timed out waiters from the
wait queue and signals the new PoolWaiter.Listener#onTimeout, nothing
else: handling the timed out acquisition is up to the pool user. The
timeout is checked when the pool executes, deadlines might be passed.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

After a connect failure, the pool immediately attempts to reconnect for
the remaining waiters, hammering a failed back-end. A reconnection
strategy is the single gatekeeper for connect attempts; the simplest
one suspends them for a delay.

Changes:

A DefaultConnectionPool builder configures an optional connect retry
delay, kept final. Each execution round reasons against a single
timestamp captured by its first progress, assuming tasks execute
quickly. A connect failure suspends connection creation until the
deadline and captures the number of connect successes since the pool
creation; the first progress of a round lifts the suspension when the
deadline has passed or when that number has increased, the evidence a
connect succeeded since the suspension. While suspended, waiters
covered by in-flight connects are still granted connections.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

Maintain a warm set of connections without demand, e.g. after start-up
or a back-end restart.

Changes:

The builder configures minimum sizes, a default kind and a context
supplier for demand-less connects. The minimum sizes are normalized to
a minimum capacity, the maximum of minSizes[i] * capacityFactors[i]:
each entry expresses the same floor in its kind units, like the maximum
sizes. At the end of each execution round, the Cleanup final action
asks the pool to satisfy the minimum capacity, creating one connection
at a time of the default kind with the supplied context, when no
connect is in flight: the actual weight of a connection is known on
connect success only, connecting one at a time avoids overshooting the
capacity. A floor connect counts as connecting - the requests field is
renamed to connecting, requests is too close to HTTP - covering waiters
arriving while it is in flight. The reconnection strategy paces the
floor retries, hence a minimum requires a connect retry delay, enforced
with the other guards by the constructor with explaining messages.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

The satisfaction loop bound-checks and rejects an admitted submission
it cannot serve, duplicating the logic of the move that follows the
loop.

Changes:

When nothing is possible for a submission, the loop gives it back to
the submitted queue and stops; the move below the loop is the single
place admitting the remaining submissions in the wait queue or
rejecting them over the bound. A submission served by a grant or a
connect remains exempted from the bound. The admitted submissions are
tracked with a deque, the give back drops the last one.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

The pool checks the connection validity with PoolConnector#isValid: the
connector composes the transport validity with the expiration policy,
e.g. the HTTP client connector checks the connection and the maximum
lifetime. The connection is not aware of the timing in the pool and the
pool is not aware of the expiration policy.

Changes:

The builder configures a connection checker predicate owning the whole
connection validity, defaulting to always valid; the pool does not use
PoolConnector#isValid anymore, kept on the connector contract for
SimpleConnectionPool. The beginning of the execution round evicts the
idle connections that do not pass the check, before any demand is
served: a connection checked at the beginning of a round is assumed
valid for the round, otherwise it is caught in the next round; a busy
connection finishes its leases. An evicted floor connection is refilled
at the end of the same round, rotating the connection.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation:

The kind of a connection can be discovered after the connect success,
e.g. a clear text upgrade resolving to HTTP/1.1: the provisional
classification must be adjusted.

Changes:

PoolConnector.Listener gets a default onKindChange signal: the pool
adjusts the connection capacity to the new kind and makes progress,
using or returning the capacity difference.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
@vietj
vietj force-pushed the default-connection-pool branch from 4f0f4bd to a044ac6 Compare December 1, 2026 10:39
Motivation:

Validate the new pool with the HTTP client, as an iso feature
implementation.

Changes:

SharedHttpClientConnectionGroup builds a DefaultConnectionPool. The
acquisition timeout moves into the pool: the request timeout is the
acquire timeout and the timer machinery is removed, the pool signals
onTimeout. The periodic expiration check becomes a pool refresh: the
connection checker owns the whole connection validity - the transport
validity composed with the maximum lifetime - and the pool closes the
evicted connections with the connector close; the evict method and
PoolConnector#isValid are not used anymore, isValid throws
UnsupportedOperationException. A connection established after its
waiter timed out is preserved in the pool and reused by the next
acquisition instead of being closed - a timeout is a statement about
the waiter, not about the connection; the test asserting the former
behavior now asserts the reuse.

A pending clear text upgrade is classified with the HTTP/2 intent, kind
1 with a concurrency of one: the pool has no capacity to dial into for
the queued waiters, they wait, covered - the previous pool never dialed
for queued waiters, masking the classification by the materialized
HTTP/1.1 version. A successful upgrade raises the concurrency with
onConcurrencyChange; a rejected upgrade reclassifies the connection
with onKindChange, returning the capacity so the pool grows to the
HTTP/1 maximum size instead of staying capped by the HTTP/2 one.

Assisted-by: Anthropic Claude (claude-fable-5)
Signed-off-by: Julien Viet <julien@julienviet.com>
@vietj
vietj force-pushed the default-connection-pool branch from a044ac6 to ed55745 Compare December 1, 2026 11:11
@vietj
vietj merged commit 9aff4a0 into master Dec 1, 2026
8 checks passed
@vietj
vietj deleted the default-connection-pool branch December 1, 2026 12:33
@tsegismont

tsegismont commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Nice work, I'll take care of adding min pool size to the SQL connection pool.

@vietj as discussed offlist, can you check my last comment in eclipse-vertx/vertx-sql-client#1807

Perhaps we need a new callback in the pool to handle timeout properly.

Have you got some time to look into this?

@vietj another point we discussed, perhaps adding some sort of statistics on the lease (e.g. timestamp when a connection goes idle) would allow 1/ to extract information from the pool that the metrics spi cannot give 2/ to implement nicely downstream features like max idle time in sql connection pool

What about this one?

@vietj

vietj commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Nice work, I'll take care of adding min pool size to the SQL connection pool.

@vietj as discussed offlist, can you check my last comment in eclipse-vertx/vertx-sql-client#1807
Perhaps we need a new callback in the pool to handle timeout properly.

Have you got some time to look into this?

I am currently migrating the SQL pool to the new pool implementation.

I'll leave up to you the min pool size after the migration is complete.

@vietj another point we discussed, perhaps adding some sort of statistics on the lease (e.g. timestamp when a connection goes idle) would allow 1/ to extract information from the pool that the metrics spi cannot give 2/ to implement nicely downstream features like max idle time in sql connection pool

What about this one?

I think we should open an issue for it

vietj added a commit to eclipse-vertx/vertx-sql-client that referenced this pull request Dec 1, 2026
Motivation:

Adopt the new Vert.x connection pool implementation
(eclipse-vertx/vert.x#6587) that will provide more features, one of them
is getting away from a timer per acquisition since the pool owns the
acquisition lifecycle (#1807, #1342).

Changes:

The pool checks staleness, closes the connections it evicts and times
out acquisitions with a TimeoutException. As a side effect this fixes
the bug addressed by #1807: a timed out query no longer keeps its slot
in the wait queue, asserted by a test ported from that PR. A periodic
refresh, configured by the new refresh period option (100 ms default),
drives all the maintenance tasks; the pool cleaner period is deprecated.
The queue metric only accounts acquisitions that effectively wait.

Assisted-by: Anthropic Claude (claude-fable-5)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Julien Viet <julien@julienviet.com>
vietj added a commit to eclipse-vertx/vertx-sql-client that referenced this pull request Dec 2, 2026
Motivation:

Adopt the new Vert.x connection pool implementation
(eclipse-vertx/vert.x#6587) that will provide more features, one of them
is getting away from a timer per acquisition since the pool owns the
acquisition lifecycle (#1807, #1342).

Changes:

The pool checks staleness with the connection checker configured in the
builder, closes the connections it evicts and times out acquisitions
with a TimeoutException. As a side effect this fixes the bug addressed
by #1807: a timed out query no longer keeps its slot in the wait queue,
asserted by a test ported from that PR. A mandatory periodic refresh,
configured by the new refresh period option (a Duration of at least one
millisecond, 100 ms by default), drives all the maintenance tasks; the
pool cleaner period is deprecated. The queue metric only accounts
acquisitions that effectively wait.

Assisted-by: Anthropic Claude (claude-fable-5)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Julien Viet <julien@julienviet.com>
vietj added a commit to eclipse-vertx/vertx-sql-client that referenced this pull request Dec 2, 2026
Motivation:

Adopt the new Vert.x connection pool implementation
(eclipse-vertx/vert.x#6587) that will provide more features, one of them
is getting away from a timer per acquisition since the pool owns the
acquisition lifecycle (#1807, #1342).

Changes:

The pool checks staleness with the connection checker configured in the
builder, closes the connections it evicts and times out acquisitions
with a TimeoutException. As a side effect this fixes the bug addressed
by #1807: a timed out query no longer keeps its slot in the wait queue,
asserted by a test ported from that PR. A mandatory periodic refresh,
configured by the new refresh period option (a Duration of at least one
millisecond, 100 ms by default), drives all the maintenance tasks; the
pool cleaner period is deprecated. The queue metric only accounts
acquisitions that effectively wait.

Assisted-by: Anthropic Claude (claude-fable-5)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Julien Viet <julien@julienviet.com>
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.

3 participants