8000 Allow users to override connection keep alive logic, port #753 on ma… · colinger/async-http-client@7e0d370 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e0d370

Browse files
author
Stephane Landelle
committed
Allow users to override connection keep alive logic, port AsyncHttpClient#753 on master
1 parent a377b17 commit 7e0d370

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

providers/netty/src/main/java/org/asynchttpclient/providers/netty/NettyAsyncHttpProviderConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.asynchttpclient.AsyncHttpProviderConfig;
2828
import org.asynchttpclient.SSLEngineFactory;
2929
import org.asynchttpclient.providers.netty.channel.pool.ChannelPool;
30+
import org.asynchttpclient.providers.netty.handler.ConnectionStrategy;
31+
import org.asynchttpclient.providers.netty.handler.Http1Point1ConnectionStrategy;
3032
import org.asynchttpclient.providers.netty.response.EagerNettyResponseBodyPart;
3133
import org.asynchttpclient.providers.netty.response.LazyNettyResponseBodyPart;
3234
import org.asynchttpclient.providers.netty.response.NettyResponseBodyPart;
@@ -171,6 +173,8 @@ public NettyWebSocket newNettyWebSocket(Channel channel, NettyAsyncHttpProviderC
171173

172174
private boolean keepEncodingHeader = false;
173175

176+
private ConnectionStrategy connectionStrategy = new Http1Point1ConnectionStrategy();
177+
174178
public EventLoopGroup getEventLoopGroup() {
175179
return eventLoopGroup;
176180
}
@@ -321,4 +325,12 @@ public boolean isKeepEncodingHeader() {
321325
public void setKeepEncodingHeader(boolean keepEncodingHeader) {
322326
this.keepEncodingHeader = keepEncodingHeader;
323327
}
328+
329+
public ConnectionStrategy getConnectionStrategy() {
330+
return connectionStrategy;
331+
}
332+
333+
public void setConnectionStrategy(ConnectionStrategy connectionStrategy) {
334+
this.connectionStrategy = connectionStrategy;
335+
}
324336
}
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 org.asynchttpclient.providers.netty.handler;
15+
16+
import io.netty.handler.codec.http.HttpRequest;
17+
import io.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+
}
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 org.asynchttpclient.providers.netty.handler;
15+
16+
import io.netty.handler.codec.http.HttpHeaders;
17+
import io.netty.handler.codec.http.HttpMessage;
18+
import io.netty.handler.codec.http.HttpRequest;
19+
import io.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+
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/handler/HttpProtocol.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@
5555

5656
public final class HttpProtocol extends Protocol {
5757

58+
private final ConnectionStrategy connectionStrategy;
59+
5860
public HttpProtocol(ChannelManager channelManager, AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig nettyConfig, NettyRequestSender requestSender) {
5961
super(channelManager, config, nettyConfig, requestSender);
62+
63+
connectionStrategy = nettyConfig.getConnectionStrategy();
6064
}
6165

6266
private Realm.RealmBuilder newRealmBuilder(Realm realm) {
@@ -374,10 +378,6 @@ private boolean exitAfterHandlingHeaders(Channel channel, NettyResponseFuture<?>
374378
return false;
375379
}
376380

377-
private boolean isConnectionKeepAlive(HttpHeaders headers) {
378-
return !HttpHeaders.Values.CLOSE.equalsIgnoreCase(headers.get(HttpHeaders.Names.CONNECTION));
379-
}
380-
381381
private boolean handleHttpResponse(final HttpResponse response, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception {
382382

383383
HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
@@ -388,7 +388,7 @@ private boolean handleHttpResponse(final HttpResponse response, final Channel ch
388388
// the handler in case of trailing headers
389389
future.setHttpHeaders(response.headers());
390390

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

393393
NettyResponseStatus status = new NettyResponseStatus(future.getUri(), config, response);
394394
int statusCode = response.getStatus().code();

0 commit comments

Comments
 (0)
0