Skip to content

Commit eaf3f3c

Browse files
committed
writeBufferedMessages now takes channle object
This make the semantic clearer that we write the buffered messages to channel rather than the context.
1 parent a1cb224 commit eaf3f3c

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

driver/src/main/java/oracle/nosql/driver/httpclient/HttpProtocolNegotiationHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void write(ChannelHandlerContext ctx, Object o, ChannelPromise channelPro
8585
@Override
8686
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
8787
super.handlerRemoved(ctx);
88-
HttpUtil.writeBufferedMessages(ctx, this.bufferedMessages);
88+
HttpUtil.writeBufferedMessages(ctx.channel(), this.bufferedMessages);
8989
}
9090

9191
@Override

driver/src/main/java/oracle/nosql/driver/httpclient/HttpUtil.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.net.SocketAddress;
1313
import java.util.Objects;
1414

15+
import io.netty.channel.Channel;
1516
import io.netty.channel.ChannelDuplexHandler;
1617
import io.netty.channel.ChannelHandlerContext;
1718
import io.netty.channel.ChannelPipeline;
@@ -24,6 +25,7 @@
2425
import io.netty.handler.codec.http2.DelegatingDecompressorFrameListener;
2526
import io.netty.handler.codec.http2.Http2ClientUpgradeCodec;
2627
import io.netty.handler.codec.http2.Http2Connection;
28+
import io.netty.handler.codec.http2.Http2ConnectionHandler;
2729
import io.netty.handler.codec.http2.Http2FrameLogger;
2830
import io.netty.handler.codec.http2.HttpToHttp2ConnectionHandler;
2931
import io.netty.handler.codec.http2.HttpToHttp2ConnectionHandlerBuilder;
@@ -46,22 +48,19 @@ public static void configureHttp1(ChannelPipeline p, int maxChunkSize, int maxCo
4648
}
4749

4850
public static void configureHttp2(ChannelPipeline p, int maxContentLength) {
49-
Http2Connection connection = new DefaultHttp2Connection(false);
50-
HttpToHttp2ConnectionHandler connectionHandler = new HttpToHttp2ConnectionHandlerBuilder()
51-
.frameListener(new DelegatingDecompressorFrameListener(
52-
connection,
53-
new InboundHttp2ToHttpAdapterBuilder(connection)
54-
.maxContentLength(maxContentLength)
55-
.propagateSettings(false)
56-
.build()))
57-
.frameLogger(frameLogger)
58-
.connection(connection)
59-
.build();
60-
61-
p.addLast(connectionHandler);
51+
p.addLast(createHttp2ConnectionHandler(maxContentLength));
6252
}
6353

6454
public static void configureH2C(ChannelPipeline p, int maxChunkSize, int maxContentLength) {
55+
HttpClientCodec sourceCodec = new HttpClientCodec(4096, 8192, maxChunkSize);
56+
Http2ClientUpgradeCodec upgradeCodec = new Http2ClientUpgradeCodec(createHttp2ConnectionHandler(maxContentLength));
57+
HttpClientUpgradeHandler upgradeHandler = new UpgradeHandler(sourceCodec, upgradeCodec, maxContentLength);
58+
p.addLast(sourceCodec,
59+
upgradeHandler,
60+
new UpgradeRequestHandler(maxContentLength));
61+
}
62+
63+
private static Http2ConnectionHandler createHttp2ConnectionHandler(int maxContentLength) {
6564
Http2Connection connection = new DefaultHttp2Connection(false);
6665
HttpToHttp2ConnectionHandler connectionHandler = new HttpToHttp2ConnectionHandlerBuilder()
6766
.frameListener(new DelegatingDecompressorFrameListener(
@@ -73,20 +72,14 @@ public static void configureH2C(ChannelPipeline p, int maxChunkSize, int maxCont
7372
.frameLogger(frameLogger)
7473
.connection(connection)
7574
.build();
76-
77-
HttpClientCodec sourceCodec = new HttpClientCodec(4096, 8192, maxChunkSize);
78-
Http2ClientUpgradeCodec upgradeCodec = new Http2ClientUpgradeCodec(connectionHandler);
79-
HttpClientUpgradeHandler upgradeHandler = new UpgradeHandler(sourceCodec, upgradeCodec, maxContentLength);
80-
p.addLast(sourceCodec,
81-
upgradeHandler,
82-
new UpgradeRequestHandler(maxContentLength));
75+
return connectionHandler;
8376
}
8477

85-
public static void writeBufferedMessages(ChannelHandlerContext ctx, RecyclableArrayList bufferedMessages) {
78+
public static void writeBufferedMessages(Channel ch, RecyclableArrayList bufferedMessages) {
8679
if (!bufferedMessages.isEmpty()) {
8780
for(int i = 0; i < bufferedMessages.size(); ++i) {
8881
Pair<Object, ChannelPromise> p = (Pair<Object, ChannelPromise>)bufferedMessages.get(i);
89-
ctx.channel().write(p.first, p.second);
82+
ch.write(p.first, p.second);
9083
}
9184

9285
bufferedMessages.clear();
@@ -107,12 +100,12 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
107100
}
108101

109102
/**
110-
* A handler that triggers the cleartext upgrade to HTTP/2 by sending an initial HTTP request.
103+
* A handler that holds HttpMessages (Except the first one)
104+
* So the upgrade handler can have time to finish clear text protocol upgrade
111105
*/
112106
private static final class UpgradeRequestHandler extends ChannelDuplexHandler {
113107
private final int maxContentLength;
114108
private final RecyclableArrayList bufferedMessages = RecyclableArrayList.newInstance();
115-
private boolean upgradeTried = false;
116109
private boolean upgrading = false;
117110

118111
public UpgradeRequestHandler(int maxContentLength) {
@@ -122,7 +115,7 @@ public UpgradeRequestHandler(int maxContentLength) {
122115
@Override
123116
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
124117
super.handlerRemoved(ctx);
125-
writeBufferedMessages(ctx, this.bufferedMessages);
118+
writeBufferedMessages(ctx.channel(), this.bufferedMessages);
126119
}
127120

128121
@Override

0 commit comments

Comments
 (0)