Skip to content

Commit 71dad6a

Browse files
wuguangkuoslandelle
authored andcommitted
Expose Remote address to the KeepAliveStrategy (#1622)
1 parent 816b0a8 commit 71dad6a

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

client/src/main/java/org/asynchttpclient/channel/DefaultKeepAliveStrategy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io.netty.handler.codec.http.HttpUtil;
66
import org.asynchttpclient.Request;
77

8+
import java.net.InetSocketAddress;
9+
810
import static io.netty.handler.codec.http.HttpHeaderValues.CLOSE;
911

1012
/**
@@ -16,7 +18,7 @@ public class DefaultKeepAliveStrategy implements KeepAliveStrategy {
1618
* Implemented in accordance with RFC 7230 section 6.1 https://tools.ietf.org/html/rfc7230#section-6.1
1719
*/
1820
@Override
19-
public boolean keepAlive(Request ahcRequest, HttpRequest request, HttpResponse response) {
21+
public boolean keepAlive(InetSocketAddress remoteAddress, Request ahcRequest, HttpRequest request, HttpResponse response) {
2022
return HttpUtil.isKeepAlive(response)
2123
&& HttpUtil.isKeepAlive(request)
2224
// support non standard Proxy-Connection

client/src/main/java/org/asynchttpclient/channel/KeepAliveStrategy.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
import io.netty.handler.codec.http.HttpResponse;
1818
import org.asynchttpclient.Request;
1919

20+
import java.net.InetSocketAddress;
21+
2022
public interface KeepAliveStrategy {
2123

2224
/**
2325
* Determines whether the connection should be kept alive after this HTTP message exchange.
2426
*
25-
* @param ahcRequest the Request, as built by AHC
26-
* @param nettyRequest the HTTP request sent to Netty
27-
* @param nettyResponse the HTTP response received from Netty
27+
* @param remoteAddress the remote InetSocketAddress associated with the request
28+
* @param ahcRequest the Request, as built by AHC
29+
* @param nettyRequest the HTTP request sent to Netty
30+
* @param nettyResponse the HTTP response received from Netty
2831
* @return true if the connection should be kept alive, false if it should be closed.
2932
*/
30-
boolean keepAlive(Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse);
33+
boolean keepAlive(InetSocketAddress remoteAddress, Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse);
3134
}

client/src/main/java/org/asynchttpclient/netty/handler/HttpHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.asynchttpclient.netty.request.NettyRequestSender;
3131

3232
import java.io.IOException;
33+
import java.net.InetSocketAddress;
3334

3435
@Sharable
3536
public final class HttpHandler extends AsyncHttpClientHandler {
@@ -69,7 +70,7 @@ private void handleHttpResponse(final HttpResponse response, final Channel chann
6970
HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
7071
logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response);
7172

72-
future.setKeepAlive(config.getKeepAliveStrategy().keepAlive(future.getTargetRequest(), httpRequest, response));
73+
future.setKeepAlive(config.getKeepAliveStrategy().keepAlive((InetSocketAddress) channel.remoteAddress(), future.getTargetRequest(), httpRequest, response));
7374

7475
NettyResponseStatus status = new NettyResponseStatus(future.getUri(), response, channel);
7576
HttpHeaders responseHeaders = response.headers();

client/src/test/java/org/asynchttpclient/BasicHttpsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void multipleSequentialPostRequestsOverHttps() throws Throwable {
107107
public void multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy() throws Throwable {
108108
logger.debug(">>> multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy");
109109

110-
KeepAliveStrategy keepAliveStrategy = (ahcRequest, nettyRequest, nettyResponse) -> !ahcRequest.getUri().isSecured();
110+
KeepAliveStrategy keepAliveStrategy = (remoteAddress, ahcRequest, nettyRequest, nettyResponse) -> !ahcRequest.getUri().isSecured();
111111

112112
withClient(config().setSslEngineFactory(createSslEngineFactory()).setKeepAliveStrategy(keepAliveStrategy)).run(client ->
113113
withServer(server).run(server -> {

0 commit comments

Comments
 (0)