Skip to content

Commit

Permalink
Merge pull request #1810 from dilanSachi/fix-keep-alive-issue
Browse files Browse the repository at this point in the history
Update client-side keep-alive handling logic
  • Loading branch information
dilanSachi authored Nov 17, 2023
2 parents 99cd18f + 1d8d78b commit 6652d1b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ballerina-tests/http-misc-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "constraint"
version = "1.4.0"
version = "1.5.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Fixed
- [Fix URL encoded form data binding with encoded `&` and `=` characters](https://github.com/ballerina-platform/ballerina-standard-library/issues/5068)
- [Fix client not honouring server-initiated connection closures](https://github.com/ballerina-platform/ballerina-library/issues/5793)

### Changed
- [Make some of the Java classes proper utility classes](https://github.com/ballerina-platform/ballerina-standard-library/issues/4923)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public static Object createSimpleHttpClient(BObject httpClient, BMap globalPoolC
get(HttpConstants.HTTP2_SETTINGS);
boolean http2PriorKnowledge = (boolean) http2Settings.get(HTTP2_PRIOR_KNOWLEDGE);
senderConfiguration.setForceHttp2(http2PriorKnowledge);
if (!http2PriorKnowledge) {
BMap<BString, Object> http1Settings = (BMap<BString, Object>) clientEndpointConfig.get(
HttpConstants.HTTP1_SETTINGS);
senderConfiguration.setKeepAliveConfig(HttpUtil.getKeepAliveConfig(http1Settings
.getStringValue(HttpConstants.CLIENT_EP_IS_KEEP_ALIVE).getValue()));
}
senderConfiguration.setHttp2InitialWindowSize(http2Settings
.getIntValue(CLIENT_EP_HTTP2_INITIAL_WINDOW_SIZE).intValue());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
Expand Down Expand Up @@ -902,20 +903,26 @@ public static HttpCarbonMessage createInboundRespCarbonMsg(ChannelHandlerContext
* @return true if the connection should be kept alive
* @throws ConfigurationException for invalid configurations
*/
public static boolean isKeepAlive(KeepAliveConfig keepAliveConfig, HttpCarbonMessage outboundRequestMsg)
throws ConfigurationException {
public static boolean isKeepAlive(KeepAliveConfig keepAliveConfig, HttpCarbonMessage outboundRequestMsg,
HttpCarbonMessage inboundRequestMsg) throws ConfigurationException {
switch (keepAliveConfig) {
case AUTO:
return Float.valueOf(outboundRequestMsg.getHttpVersion()) > Constants.HTTP_1_0;
if (Float.valueOf(outboundRequestMsg.getHttpVersion()) <= Constants.HTTP_1_0) {
return false;
}
if (inboundRequestMsg.getHeaders().contains(HttpHeaderNames.CONNECTION)) {
return !inboundRequestMsg.getHeaders().get(HttpHeaderNames.CONNECTION)
.equalsIgnoreCase(HttpHeaderValues.CLOSE.toString());
}
return true;
case ALWAYS:
return true;
case NEVER:
return false;
default:
// The execution will never reach here. In case execution reach here means it should be an invalid value
// for keep-alive configurations.
throw new ConfigurationException("Invalid keep-alive configuration value : "
+ keepAliveConfig.toString());
throw new ConfigurationException("Invalid keep-alive configuration value : " + keepAliveConfig);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public void readInboundResponseEntityBody(ChannelHandlerContext ctx, HttpContent
safelyRemoveHandlers(targetHandler.getTargetChannel().getChannel().pipeline(), IDLE_STATE_HANDLER);
senderReqRespStateManager.state = new EntityBodyReceived(senderReqRespStateManager);

if (!isKeepAlive(targetHandler.getKeepAliveConfig(), targetHandler.getOutboundRequestMsg())) {
if (!isKeepAlive(targetHandler.getKeepAliveConfig(),
targetHandler.getOutboundRequestMsg(), inboundResponseMsg)) {
targetHandler.closeChannel(ctx);
}
targetHandler.getConnectionManager().returnChannel(targetHandler.getTargetChannel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ public void testResetChannelAttributes() {
@Test
public void testIsKeepAlive() throws ConfigurationException {
HttpCarbonMessage outboundRequestMsg = mock(HttpCarbonMessage.class);
Assert.assertFalse(Util.isKeepAlive(KeepAliveConfig.NEVER, outboundRequestMsg));
HttpCarbonMessage inboundRequestMsg = mock(HttpCarbonMessage.class);
Assert.assertFalse(Util.isKeepAlive(KeepAliveConfig.NEVER, outboundRequestMsg, inboundRequestMsg));
}

}

0 comments on commit 6652d1b

Please sign in to comment.