Skip to content
Merged
Show file tree
Hide file tree
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
deadlock fix attempt v4
  • Loading branch information
georgebanasios committed Jul 4, 2025
commit f849ec8eab856b6d4721123f71bb8ce5d0f6d442
4 changes: 4 additions & 0 deletions sdk/clientcore/http-netty4/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@
<Bug pattern="VO_VOLATILE_REFERENCE_TO_ARRAY" />
<Class name="io.clientcore.http.netty4.implementation.Netty4ChannelBinaryData" />
</Match>
<Match>
<Bug pattern="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" />
<Class name="io.clientcore.http.netty4.implementation.Netty4ChannelInputStream" />
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;

/**
Expand All @@ -28,9 +29,9 @@ public final class Netty4ChannelInputStream extends InputStream {
// Once this is true, the stream will never return data again.
private boolean streamDone = false;

// Linked list of byte[]s that maintains the last available contents from the Channel / eager content.
// A list is needed as each Channel.read() may result in many channelRead calls.
private final LinkedList<byte[]> additionalBuffers;
// Queue of byte[]s that maintains the last available contents from the Channel / eager content.
// A queue is needed as each Channel.read() may result in many channelRead calls.
private final Queue<byte[]> additionalBuffers;

private byte[] currentBuffer;

Expand All @@ -57,7 +58,7 @@ public final class Netty4ChannelInputStream extends InputStream {
this.currentBuffer = new byte[0];
}
this.readIndex = 0;
this.additionalBuffers = new LinkedList<>();
this.additionalBuffers = new ConcurrentLinkedQueue<>();
this.channel = channel;
if (channel.pipeline().get(Netty4InitiateOneReadHandler.class) != null) {
channel.pipeline().remove(Netty4InitiateOneReadHandler.class);
Expand Down Expand Up @@ -190,7 +191,7 @@ public void close() throws IOException {

private boolean setupNextBuffer() throws IOException {
if (!additionalBuffers.isEmpty()) {
currentBuffer = additionalBuffers.pop();
currentBuffer = additionalBuffers.poll();
readIndex = 0;
return true;
} else if (readMore()) {
Expand Down Expand Up @@ -225,7 +226,7 @@ private boolean readMore() throws IOException {
byte[] buffer = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(buffer);

additionalBuffers.add(buffer);
additionalBuffers.offer(buffer);
}, isHttp2);
channel.pipeline().addLast(Netty4HandlerNames.READ_ONE, handler);
}
Expand Down Expand Up @@ -253,7 +254,7 @@ private boolean readMore() throws IOException {
}

if (!additionalBuffers.isEmpty()) {
currentBuffer = additionalBuffers.pop();
currentBuffer = additionalBuffers.poll();
readIndex = 0;
} else if (channelDone) { // Don't listen to IntelliJ here, channelDone may be false.
// This read contained no data and the channel completed, therefore the stream is also completed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
} catch (IOException | RuntimeException ex) {
ReferenceCountUtil.release(msg);
ctx.fireExceptionCaught(ex);
cleanup(ctx);
latch.countDown();
}
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.fireChannelReadComplete();
if (lastRead) {
cleanup(ctx);
latch.countDown();
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
this.exception = cause;
ctx.fireExceptionCaught(cause);
cleanup(ctx);
latch.countDown();
}

Throwable channelException() {
Expand All @@ -88,13 +88,13 @@ Throwable channelException() {
// TODO (alzimmer): Are the latch countdowns needed for unregistering and inactivity?
@Override
public void channelUnregistered(ChannelHandlerContext ctx) {
cleanup(ctx);
latch.countDown();
ctx.fireChannelUnregistered();
}

@Override
public void channelInactive(ChannelHandlerContext ctx) {
cleanup(ctx);
latch.countDown();
ctx.fireChannelInactive();
}

Expand All @@ -105,15 +105,6 @@ public void handlerAdded(ChannelHandlerContext ctx) {
// an exception. Simply counting down the latch would cause the caller to receive
// an empty/incomplete data stream without any indication of the underlying network error.
ctx.fireExceptionCaught(new ClosedChannelException());
ctx.pipeline().remove(this);
}
}

private void cleanup(ChannelHandlerContext ctx) {
if (ctx.pipeline().get(Netty4EagerConsumeChannelHandler.class) != null) {
ctx.pipeline().remove(this);
}

latch.countDown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
latch.countDown();
if (lastRead) {
ctx.pipeline().remove(this);
}
ctx.fireChannelReadComplete();
}

Expand All @@ -109,9 +106,6 @@ boolean isChannelConsumed() {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
this.exception = cause;
latch.countDown();
if (ctx.pipeline().get(Netty4InitiateOneReadHandler.class) != null) {
ctx.pipeline().remove(this);
}
ctx.fireExceptionCaught(cause);
}

Expand All @@ -123,18 +117,12 @@ Throwable channelException() {
@Override
public void channelUnregistered(ChannelHandlerContext ctx) {
latch.countDown();
if (ctx.pipeline().get(Netty4InitiateOneReadHandler.class) != null) {
ctx.pipeline().remove(this);
}
ctx.fireChannelUnregistered();
}

@Override
public void channelInactive(ChannelHandlerContext ctx) {
latch.countDown();
if (ctx.pipeline().get(Netty4InitiateOneReadHandler.class) != null) {
ctx.pipeline().remove(this);
}
ctx.fireChannelInactive();
}

Expand All @@ -145,7 +133,6 @@ public void handlerAdded(ChannelHandlerContext ctx) {
// an exception. Simply counting down the latch would cause the caller to receive
// an empty/incomplete data stream without any indication of the underlying network error.
ctx.fireExceptionCaught(new ClosedChannelException());
ctx.pipeline().remove(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http2.Http2DataFrame;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -30,7 +28,6 @@ public class Netty4PipelineCleanupHandler extends ChannelDuplexHandler {

private final Netty4ConnectionPool connectionPool;
private final AtomicBoolean cleanedUp = new AtomicBoolean(false);
private boolean lastContentRead;

private static final List<String> HANDLERS_TO_REMOVE;

Expand All @@ -50,46 +47,29 @@ public Netty4PipelineCleanupHandler(Netty4ConnectionPool connectionPool) {
this.connectionPool = connectionPool;
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof LastHttpContent) {
this.lastContentRead = true;
} else if (msg instanceof Http2DataFrame) {
this.lastContentRead = ((Http2DataFrame) msg).isEndStream();
}

ctx.fireChannelRead(msg);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// First, let other handlers process the channelReadComplete event.
ctx.fireChannelReadComplete();

if (lastContentRead) {
cleanup(ctx);
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// An exception has occurred, which means the channel is likely in a bad state.
// We handle this by closing the channel. This prevents it from being
// returned to the connection pool.
ctx.close();
cleanup(ctx, true);
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
public void channelInactive(ChannelHandlerContext ctx) {
ctx.fireChannelInactive();
cleanup(ctx);
cleanup(ctx, true);
}

public void cleanup(ChannelHandlerContext ctx) {
public void cleanup(ChannelHandlerContext ctx, boolean closeChannel) {
if (!cleanedUp.compareAndSet(false, true)) {
return;
}

// Always reset autoRead to false before returning a channel to the pool
// to ensure predictable behavior for the next request.
ctx.channel().config().setAutoRead(false);

ChannelPipeline pipeline = ctx.channel().pipeline();
for (String handlerName : HANDLERS_TO_REMOVE) {
if (pipeline.get(handlerName) != null) {
Expand All @@ -101,6 +81,10 @@ public void cleanup(ChannelHandlerContext ctx) {
pipeline.remove(this);
}

connectionPool.release(ctx.channel());
if (closeChannel) {
ctx.channel().close();
} else {
connectionPool.release(ctx.channel());
}
}
}
Loading