Skip to content

Commit 8dea296

Browse files
committed
Allow users to override connection keep alive logic
Implements AsyncHttpClient#733 for 1.9.x.
1 parent 01ac293 commit 8dea296

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ning.http.client.providers.netty.handler;
2+
3+
import org.jboss.netty.handler.codec.http.HttpRequest;
4+
import org.jboss.netty.handler.codec.http.HttpResponse;
5+
6+
/**
7+
* Provides an interface for decisions about HTTP connections.
8+
*/
9+
public interface ConnectionStrategy {
10+
11+
/**
12+
* Determines whether the connection should be kept alive after this HTTP message exchange.
13+
* @param request the HTTP request
14+
* @param response the HTTP response
15+
* @return true if the connection should be kept alive, false if it should be closed.
16+
*/
17+
boolean keepAlive(HttpRequest request, HttpResponse response);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ning.http.client.providers.netty.handler;
2+
3+
import org.jboss.netty.handler.codec.http.HttpHeaders;
4+
import org.jboss.netty.handler.codec.http.HttpMessage;
5+
import org.jboss.netty.handler.codec.http.HttpRequest;
6+
import org.jboss.netty.handler.codec.http.HttpResponse;
7+
8+
public class Http1Point1ConnectionStrategy implements ConnectionStrategy {
9+
10+
@Override
11+
public boolean keepAlive(HttpRequest httpRequest, HttpResponse response) {
12+
return isConnectionKeepAlive(httpRequest) && isConnectionKeepAlive(response);
13+
}
14+
15+
public boolean isConnectionKeepAlive(HttpMessage message) {
16+
return !HttpHeaders.Values.CLOSE.equalsIgnoreCase(message.headers().get(HttpHeaders.Names.CONNECTION));
17+
}
18+
}

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)