From 301843f69b0bd7a2e7c42dc0b114871dcd91a57f Mon Sep 17 00:00:00 2001 From: shafreenAnfar Date: Wed, 13 Sep 2023 15:02:19 +0530 Subject: [PATCH 1/2] [Automated] Update the native jar versions --- ballerina/Ballerina.toml | 4 ++-- ballerina/Dependencies.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index 402e1e3319..60e932c9f3 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -23,13 +23,13 @@ path = "../native/build/libs/http-native-2.10.0-SNAPSHOT.jar" groupId = "io.ballerina.stdlib" artifactId = "mime-native" version = "2.9.0" -path = "./lib/mime-native-2.9.0-20230831-160900-6ecc648.jar" +path = "./lib/mime-native-2.9.0-20230911-153200-3add04c.jar" [[platform.java17.dependency]] groupId = "io.ballerina.stdlib" artifactId = "constraint-native" version = "1.4.0" -path = "./lib/constraint-native-1.4.0-20230831-142400-50e4023.jar" +path = "./lib/constraint-native-1.4.0-20230911-142700-8202202.jar" [[platform.java17.dependency]] groupId = "io.netty" diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 0768a2172b..3f4aa0b0d6 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.8.0-20230830-220400-8a7556d8" +distribution-version = "2201.8.0-20230908-135700-74a59dff" [[package]] org = "ballerina" From 8eed67ba1d517b1585f8d1c5b3c300b6b86eb440 Mon Sep 17 00:00:00 2001 From: shafreenAnfar Date: Thu, 14 Sep 2023 11:15:43 +0530 Subject: [PATCH 2/2] Refactor the code --- .../DefaultHttpClientConnector.java | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java index 5d233d1a1b..1358a8774a 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java @@ -211,22 +211,13 @@ public void onSuccess(String protocol, ChannelFuture channelFuture) { route.toString() + " " + "Original Channel ID is : " + channelFuture.channel().id()); } - if (Constants.HTTP_SCHEME.equalsIgnoreCase(protocol) && http1xSrcHandler != null) { - channelFuture.channel().deregister().addListener(future -> - http1xSrcHandler.getEventLoop() - .register(channelFuture.channel()) - .addListener( - future1 -> - startExecutingOutboundRequest( - protocol, channelFuture))); - } else if (Constants.HTTP_SCHEME.equalsIgnoreCase(protocol) && http2SrcHandler != null) { - channelFuture.channel().deregister().addListener(future -> - http2SrcHandler.getChannelHandlerContext() - .channel().eventLoop() - .register(channelFuture.channel()) - .addListener(future1 -> - startExecutingOutboundRequest( - protocol, channelFuture))); + if (isH1c(protocol)) { + switchEventLoopForH1c(channelFuture).addListener(future -> + startExecutingOutboundRequest(protocol, channelFuture)); + + } else if (isH2c(protocol)) { + switchEventLoopForH2c(channelFuture).addListener(future -> + startExecutingOutboundRequest(protocol, channelFuture)); } else { startExecutingOutboundRequest(protocol, channelFuture); } @@ -299,6 +290,27 @@ private void initializeSenderReqRespStateMgr(Channel targetNettyChannel) { targetChannel.senderReqRespStateManager = senderReqRespStateManager; } + // Switching is done to make sure, inbound request/response and the outbound request/response + // are handle on the same thread and thereby avoid the need for locks + private ChannelFuture switchEventLoopForH1c(ChannelFuture channelFuture) { + return channelFuture.channel().deregister() + .addListener(future -> http1xSrcHandler.getEventLoop().register(channelFuture.channel())); + } + + private ChannelFuture switchEventLoopForH2c(ChannelFuture channelFuture) { + return channelFuture.channel().deregister().addListener(future -> + http2SrcHandler.getChannelHandlerContext().channel().eventLoop() + .register(channelFuture.channel())); + } + + private boolean isH1c(String protocol) { + return Constants.HTTP_SCHEME.equalsIgnoreCase(protocol) && http1xSrcHandler != null; + } + + private boolean isH2c(String protocol) { + return Constants.HTTP_SCHEME.equalsIgnoreCase(protocol) && http2SrcHandler != null; + } + @Override public void onFailure(ClientConnectorException cause) { httpResponseFuture.notifyHttpListener(cause);