Skip to content

Commit 279d3b9

Browse files
author
Stephane Landelle
committed
Merge pull request AsyncHttpClient#753 from barnardb/1.9.x-keep-alive-strategy
[1.9.x] Allow users to override connection keep alive logic
2 parents 01ac293 + ec05154 commit 279d3b9

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProviderConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.ning.http.client.AsyncHttpProviderConfig;
2222
import com.ning.http.client.SSLEngineFactory;
2323
import com.ning.http.client.providers.netty.channel.pool.ChannelPool;
24+
import com.ning.http.client.providers.netty.handler.ConnectionStrategy;
25+
import com.ning.http.client.providers.netty.handler.Http1Point1ConnectionStrategy;
2426
import com.ning.http.client.providers.netty.ws.NettyWebSocket;
2527

2628
import java.util.Map;
@@ -145,6 +147,8 @@ public Set<Map.Entry<String, Object>> propertiesSet() {
145147

146148
private boolean keepEncodingHeader = false;
147149

150+
private ConnectionStrategy connectionStrategy = new Http1Point1ConnectionStrategy();
151+
148152
public boolean isUseDeadLockChecker() {
149153
return useDeadLockChecker;
150154
}
@@ -305,6 +309,14 @@ public void setKeepEncodingHeader(boolean keepEncodingHeader) {
305309
this.keepEncodingHeader = keepEncodingHeader;
306310
}
307311

312+
public ConnectionStrategy getConnectionStrategy() {
313+
return connectionStrategy;
314+
}
315+
316+
public void setConnectionStrategy(ConnectionStrategy connectionStrategy) {
317+
this.connectionStrategy = connectionStrategy;
318+
}
319+
308320
public static interface NettyWebSocketFactory {
309321
NettyWebSocket newNettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig);
310322
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package com.ning.http.client.providers.netty.handler;
15+
16+
import org.jboss.netty.handler.codec.http.HttpRequest;
17+
import org.jboss.netty.handler.codec.http.HttpResponse;
18+
19+
/**
20+
* Provides an interface for decisions about HTTP connections.
21+
*/
22+
public interface ConnectionStrategy {
23+
24+
/**
25+
* Determines whether the connection should be kept alive after this HTTP message exchange.
26+
* @param request the HTTP request
27+
* @param response the HTTP response
28+
* @return true if the connection should be kept alive, false if it should be closed.
29+
*/
30+
boolean keepAlive(HttpRequest request, HttpResponse response);
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package com.ning.http.client.providers.netty.handler;
15+
16+
import org.jboss.netty.handler.codec.http.HttpHeaders;
17+
import org.jboss.netty.handler.codec.http.HttpMessage;
18+
import org.jboss.netty.handler.codec.http.HttpRequest;
19+
import org.jboss.netty.handler.codec.http.HttpResponse;
20+
21+
/**
22+
* Connection strategy implementing standard HTTP 1.1 behaviour.
23+
*/
24+
public class Http1Point1ConnectionStrategy implements ConnectionStrategy {
25+
26+
/**
27+
* Implemented in accordance with RFC 7230 section 6.1
28+
* https://tools.ietf.org/html/rfc7230#section-6.1
29+
*/
30+
@Override
31+
public boolean keepAlive(HttpRequest httpRequest, HttpResponse response) {
32+
return isConnectionKeepAlive(httpRequest) && isConnectionKeepAlive(response);
33+
}
34+
35+
public boolean isConnectionKeepAlive(HttpMessage message) {
36+
return !HttpHeaders.Values.CLOSE.equalsIgnoreCase(message.headers().get(HttpHeaders.Names.CONNECTION));
37+
}
38+
}

src/main/java/com/ning/http/client/providers/netty/handler/HttpProtocol.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@
5656

5757
public final class HttpProtocol extends Protocol {
5858

59+
private final ConnectionStrategy connectionStrategy;
60+
5961
public HttpProtocol(ChannelManager channelManager, AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig nettyConfig,
6062
NettyRequestSender requestSender) {
6163
super(channelManager, config, nettyConfig, requestSender);
64+
65+
connectionStrategy = nettyConfig.getConnectionStrategy();
6266
}
6367

6468
private Realm.RealmBuilder newRealmBuilder(Realm realm) {
@@ -402,10 +406,6 @@ private boolean exitAfterHandlingBody(Channel channel, NettyResponseFuture<?> fu
402406
return false;
403407
}
404408

405-
private boolean isConnectionKeepAlive(HttpHeaders headers) {
406-
return !HttpHeaders.Values.CLOSE.equalsIgnoreCase(headers.get(HttpHeaders.Names.CONNECTION));
407-
}
408-
409409
private boolean handleHttpResponse(final HttpResponse response,//
410410
final Channel channel,//
411411
final NettyResponseFuture<?> future,//
@@ -419,7 +419,7 @@ private boolean handleHttpResponse(final HttpResponse response,//
419419
// the handler in case of trailing headers
420420
future.setHttpHeaders(response.headers());
421421

422-
future.setKeepAlive(isConnectionKeepAlive(httpRequest.headers()) && isConnectionKeepAlive(response.headers()));
422+
future.setKeepAlive(connectionStrategy.keepAlive(httpRequest, response));
423423

424424
NettyResponseStatus status = new NettyResponseStatus(future.getUri(), config, response);
425425
int statusCode = response.getStatus().getCode();

0 commit comments

Comments
 (0)