Skip to content

Commit

Permalink
Merge pull request #1758 from ballerina-platform/refactor
Browse files Browse the repository at this point in the history
Refactor the code
  • Loading branch information
dilanSachi committed Sep 14, 2023
2 parents 1c092fe + 8eed67b commit 35cf9a2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 35cf9a2

Please sign in to comment.