Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
1ddd05d
Http-netty4 connection pool implementation
georgebanasios Jun 28, 2025
a379a00
Merge remote-tracking branch 'origin/main' into connection-pool-http-…
georgebanasios Jun 29, 2025
784ade2
fix duplicate http response handler in the pipeline
georgebanasios Jun 29, 2025
7edd7a9
apply ALPN after the correct handler
georgebanasios Jun 29, 2025
ce50c9e
fix formatting
georgebanasios Jun 30, 2025
6b73bb3
fix Netty4ChannelBinaryData
georgebanasios Jun 30, 2025
c8693a1
fix connection pool release race condition
georgebanasios Jun 30, 2025
e1525d8
adjust chunked request
georgebanasios Jun 30, 2025
f26b6c3
Revert "adjust chunked request"
georgebanasios Jun 30, 2025
1602d4b
fix proxy logic when acquiring a channel
georgebanasios Jul 1, 2025
989b302
cleanup the pipeline after the last content
georgebanasios Jul 1, 2025
f0a7847
create channel wrapper
georgebanasios Jul 1, 2025
c8c648b
remove channel closure from response handler
georgebanasios Jul 1, 2025
8dea326
try and fix the release deadlock
georgebanasios Jul 1, 2025
e076602
handle case where read handlers are added to a closed channel
georgebanasios Jul 2, 2025
8de78bd
remove synchronized
georgebanasios Jul 2, 2025
ed27af7
apply ALPN only on new connections
georgebanasios Jul 2, 2025
51fa938
try and fix deadlock
georgebanasios Jul 2, 2025
f3eee88
Revert "try and fix deadlock"
georgebanasios Jul 2, 2025
61c273b
deadlock fix v2
georgebanasios Jul 3, 2025
40b929a
Revert "deadlock fix v2"
georgebanasios Jul 3, 2025
792a06e
deadlock fix v3
georgebanasios Jul 3, 2025
56bbb97
Revert "deadlock fix v3"
georgebanasios Jul 3, 2025
f849ec8
deadlock fix attempt v4
georgebanasios Jul 4, 2025
140a095
fix on input stream close method
georgebanasios Jul 4, 2025
c0f9beb
deadlock fix attempt v5
georgebanasios Jul 4, 2025
a2d0bcd
Revert "deadlock fix attempt v5"
georgebanasios Jul 4, 2025
d66fd1d
deadlock fix attempt v6
georgebanasios Jul 4, 2025
42f72c7
make method more efficient
georgebanasios Jul 5, 2025
4606fe4
fix response handler tests
georgebanasios Jul 5, 2025
6fdf2e7
make close method of netty binary data non blocking
georgebanasios Jul 5, 2025
623503c
cleanup connection on netty binary data
georgebanasios Jul 6, 2025
f085ece
add exception handling on cleanup handler
georgebanasios Jul 6, 2025
d66d154
Netty4ChannelBinaryData adjustments
georgebanasios Jul 6, 2025
aa1fba9
try to not block on send method
georgebanasios Jul 7, 2025
720b395
disable connection pooling functionality
georgebanasios Jul 8, 2025
670687f
adjust cleanups
georgebanasios Jul 9, 2025
c0022f4
use event for pipeline cleanup
georgebanasios Jul 9, 2025
7b3c8fe
attempt to synchronize the pipeline modification and cleaup for the s…
georgebanasios Jul 10, 2025
1a28409
self removal of handlers
georgebanasios Jul 10, 2025
ed9b857
attempt to fix race condition
georgebanasios Jul 10, 2025
fec1ffc
close clients after tests
georgebanasios Jul 13, 2025
ca36245
Revert "close clients after tests"
georgebanasios Jul 13, 2025
437616b
http2 connecetions
georgebanasios Jul 13, 2025
40ef6ee
synchronize connection pool
georgebanasios Jul 13, 2025
59f9240
Revert "synchronize connection pool"
georgebanasios Jul 14, 2025
9e65d78
check connection
georgebanasios Jul 16, 2025
84be996
Revert "check connection"
georgebanasios Jul 16, 2025
e092c9c
try to centralize waiting queue logic
georgebanasios Jul 16, 2025
df003e0
Revert "try to centralize waiting queue logic"
georgebanasios Jul 16, 2025
dbf1c69
enable half closure
georgebanasios Jul 16, 2025
9a3477b
add lock on acquire/release
georgebanasios Jul 16, 2025
6855d45
documentation fix & pr comments
georgebanasios Jul 19, 2025
814285d
fix ssl/alpn on pooled connections
georgebanasios Jul 21, 2025
91f72e0
Merge remote-tracking branch 'origin/main' into connection-pool-http-…
georgebanasios Jul 22, 2025
879c638
remove duplicate line & javadoc fix
georgebanasios Jul 22, 2025
d4694d8
add javadoc on netty provider
georgebanasios Jul 23, 2025
61be579
add HTTP client tests for pooled connections
georgebanasios Jul 23, 2025
5551d47
fix builder logging configuration logic
georgebanasios Jul 23, 2025
c64019a
fix merge conflicts
georgebanasios Aug 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "try to centralize waiting queue logic"
This reverts commit e092c9c.
  • Loading branch information
georgebanasios committed Jul 16, 2025
commit df003e0dd4c64900e3eae84723a430fa8871a9d1
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ private class PerRoutePool {
private final Deque<Promise<Channel>> pendingAcquirers = new ConcurrentLinkedDeque<>();
// Counter for all connections for a specific route (active and idle).
private final AtomicInteger totalConnections = new AtomicInteger(0);
private final AtomicBoolean dispatching = new AtomicBoolean(false);
private final Netty4ConnectionPoolKey key;
private final SocketAddress route;
private final boolean isHttps;
Expand Down Expand Up @@ -311,111 +310,145 @@ Future<Channel> acquire() {
return connection.channel.eventLoop().newSucceededFuture(connection.channel);
}

Promise<Channel> promise = bootstrap.config().group().next().newPromise();
if (pendingAcquirers.size() >= maxPendingAcquires) {
promise.setFailure(CoreException.from("Pending acquisition queue is full."));
return promise;
// No idle connections, we need to either create a new one or queue.
int currentTotal = totalConnections.getAndIncrement();
if (currentTotal < maxConnectionsPerRoute) {
return createNewConnection();
}

pendingAcquirers.offer(promise);
dispatch();

if (pendingAcquireTimeout != null) {
promise.addListener(future -> {
if (future.isCancelled()) {
pendingAcquirers.remove(promise);
}
});

bootstrap.config().group().schedule(() -> {
if (promise.tryFailure(
CoreException.from("Connection acquisition timed out after " + pendingAcquireTimeout))) {
pendingAcquirers.remove(promise);
}
}, pendingAcquireTimeout.toMillis(), TimeUnit.MILLISECONDS);
}
return promise;
// Pool is full, decrement the counter back and queue the request.
totalConnections.getAndDecrement();
return queueAcquireRequest();
}

void release(PooledConnection connection) {
if (!isHealthy(connection)) {
connection.close();
connection.close(); // The close listener will handle decrementing the counter.
return;
}

connection.idleSince = OffsetDateTime.now(ZoneOffset.UTC);

// Offer to the idle queue and then try to satisfy pending waiters.
idleConnections.offer(connection);
dispatch();
satisfyWaiter();
}

private void dispatch() {
if (!dispatching.compareAndSet(false, true)) {
return;
private PooledConnection pollIdleConnection() {
while (true) {
PooledConnection connection = idleConnections.poll();
if (connection == null) {
return null;
}

if (isHealthy(connection)) {
connection.idleSince = null; // Mark as active
return connection;
}

connection.close(); // The close listener will handle decrementing the counter.
}
}

try {
while (!pendingAcquirers.isEmpty()) {
PooledConnection idleConnection = pollIdleConnection();
if (idleConnection != null) {
Promise<Channel> waiter = pollNextWaiter();
if (waiter != null) {
if (!waiter.trySuccess(idleConnection.channel)) {
release(idleConnection);
}
} else {
idleConnections.addFirst(idleConnection);
break;
}
continue;
/**
* Queues a new promise for a connection.
* This is called when the pool is at max capacity.
*
* @return A Future that will be completed later.
*/
private Future<Channel> queueAcquireRequest() {
if (pendingAcquirers.size() >= maxPendingAcquires) {
return bootstrap.config()
.group()
.next()
.newFailedFuture(CoreException.from("Pending acquisition queue is full."));
}

Promise<Channel> promise = bootstrap.config().group().next().newPromise();
promise.addListener(future -> {
if (future.isCancelled()) {
pendingAcquirers.remove(promise);
}
});
pendingAcquirers.offer(promise);
if (pendingAcquireTimeout != null) {
bootstrap.config().group().schedule(() -> {
if (!promise.isDone()) {
promise.tryFailure(
CoreException.from("Connection acquisition timed out after " + pendingAcquireTimeout));
}
}, pendingAcquireTimeout.toMillis(), TimeUnit.MILLISECONDS);
}
satisfyWaiter();
return promise;
}

if (totalConnections.get() < maxConnectionsPerRoute) {
if (totalConnections.incrementAndGet() > maxConnectionsPerRoute) {
totalConnections.decrementAndGet();
break;
}
/**
* This is the core logic that matches pending waiters with available resources.
* It can be triggered when a connection is released, or when a slot opens up.
*/
private void satisfyWaiter() {
if (pendingAcquirers.isEmpty()) {
return;
}

Promise<Channel> waiter = pollNextWaiter();
if (waiter != null) {
createNewConnection().addListener(future -> {
if (future.isSuccess()) {
if (!waiter.trySuccess((Channel) future.getNow())) {
release(((Channel) future.getNow()).attr(POOLED_CONNECTION_KEY).get());
}
} else {
waiter.tryFailure(future.cause());
}
});
} else {
totalConnections.decrementAndGet();
}
} else {
break;
// First, try to get a ready-to-use idle connection.
PooledConnection idleConnection = pollIdleConnection();
if (idleConnection != null) {
Promise<Channel> waiter = pollNextWaiter();
if (waiter != null) {
if (!waiter.trySuccess(idleConnection.channel)) {
// Waiter was canceled, release the connection back.
release(idleConnection);
}
} else {
// No waiter, put the connection back in the idle queue.
idleConnections.addFirst(idleConnection);
}
} finally {
dispatching.set(false);
return;
}
}

private PooledConnection pollIdleConnection() {
// No idle connections, try to create a new one if there is capacity.
while (true) {
PooledConnection connection = idleConnections.poll();
if (connection == null) {
return null;
int currentTotal = totalConnections.get();
if (currentTotal >= maxConnectionsPerRoute) {
// No capacity, can't create a new connection.
return;
}
if (isHealthy(connection)) {
connection.idleSince = null; // Mark as active
return connection;

if (totalConnections.compareAndSet(currentTotal, currentTotal + 1)) {
// We successfully reserved a slot for a new connection.
Promise<Channel> waiter = pollNextWaiter();
if (waiter != null) {
// Create a new connection for this specific waiter.
createNewConnection().addListener(future -> {
if (future.isSuccess()) {
if (!waiter.trySuccess((Channel) future.getNow())) {
// The Waiter was canceled while we were connecting.
// Release the new connection.
release(((Channel) future.getNow()).attr(POOLED_CONNECTION_KEY).get());
}
} else {
// Connection failed. The close listener on the (failed) channel
// will decrement totalConnections and trigger another satisfyWaiter call.
waiter.tryFailure(future.cause());
}
});
} else {
// We reserved a slot, but there's no waiter. Release the slot.
totalConnections.decrementAndGet();
}
return;
}
connection.close();
// CAS failed, another thread acted. Loop to retry.
}
}

private Promise<Channel> pollNextWaiter() {
while (true) {
Promise<Channel> waiter = pendingAcquirers.poll();
if (waiter == null) {
return null;
return null; // Queue is empty
}
if (!waiter.isCancelled()) {
return waiter;
Expand Down Expand Up @@ -460,16 +493,16 @@ public void initChannel(Channel channel) throws SSLException {
newConnectionBootstrap.connect(route).addListener(future -> {
if (!future.isSuccess()) {
LOGGER.atError().setThrowable(future.cause()).log("Failed connection.");
totalConnections.decrementAndGet();
dispatch();
totalConnections.getAndDecrement();
satisfyWaiter();
promise.setFailure(future.cause());
return;
}

Channel newChannel = ((ChannelFuture) future).channel();
newChannel.closeFuture().addListener(closeFuture -> {
totalConnections.decrementAndGet();
dispatch();
totalConnections.getAndDecrement();
satisfyWaiter();
});

Runnable connectionReadyRunner = () -> {
Expand Down Expand Up @@ -506,7 +539,7 @@ public void initChannel(Channel channel) throws SSLException {
}
});
} else {
connectionReadyRunner.run();
promise.setSuccess(newChannel);
}
});
return promise;
Expand Down
Loading