Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
shafreenAnfar committed Sep 14, 2023
1 parent 301843f commit 8eed67b
Showing 1 changed file with 28 additions and 16 deletions.
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));

Check warning on line 220 in native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java#L219-L220

Added lines #L219 - L220 were not covered by tests
} 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()));

Check warning on line 303 in native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java#L301-L303

Added lines #L301 - L303 were not covered by tests
}

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 8eed67b

Please sign in to comment.