Skip to content

Commit

Permalink
Expose Remote address to the KeepAliveStrategy (#1622)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuguangkuo authored and slandelle committed May 9, 2019
1 parent 816b0a8 commit 71dad6a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.netty.handler.codec.http.HttpUtil;
import org.asynchttpclient.Request;

import java.net.InetSocketAddress;

import static io.netty.handler.codec.http.HttpHeaderValues.CLOSE;

/**
Expand All @@ -16,7 +18,7 @@ public class DefaultKeepAliveStrategy implements KeepAliveStrategy {
* Implemented in accordance with RFC 7230 section 6.1 https://tools.ietf.org/html/rfc7230#section-6.1
*/
@Override
public boolean keepAlive(Request ahcRequest, HttpRequest request, HttpResponse response) {
public boolean keepAlive(InetSocketAddress remoteAddress, Request ahcRequest, HttpRequest request, HttpResponse response) {
return HttpUtil.isKeepAlive(response)
&& HttpUtil.isKeepAlive(request)
// support non standard Proxy-Connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
import io.netty.handler.codec.http.HttpResponse;
import org.asynchttpclient.Request;

import java.net.InetSocketAddress;

public interface KeepAliveStrategy {

/**
* Determines whether the connection should be kept alive after this HTTP message exchange.
*
* @param ahcRequest the Request, as built by AHC
* @param nettyRequest the HTTP request sent to Netty
* @param nettyResponse the HTTP response received from Netty
* @param remoteAddress the remote InetSocketAddress associated with the request
* @param ahcRequest the Request, as built by AHC
* @param nettyRequest the HTTP request sent to Netty
* @param nettyResponse the HTTP response received from Netty
* @return true if the connection should be kept alive, false if it should be closed.
*/
boolean keepAlive(Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse);
boolean keepAlive(InetSocketAddress remoteAddress, Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.asynchttpclient.netty.request.NettyRequestSender;

import java.io.IOException;
import java.net.InetSocketAddress;

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

future.setKeepAlive(config.getKeepAliveStrategy().keepAlive(future.getTargetRequest(), httpRequest, response));
future.setKeepAlive(config.getKeepAliveStrategy().keepAlive((InetSocketAddress) channel.remoteAddress(), future.getTargetRequest(), httpRequest, response));

NettyResponseStatus status = new NettyResponseStatus(future.getUri(), response, channel);
HttpHeaders responseHeaders = response.headers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void multipleSequentialPostRequestsOverHttps() throws Throwable {
public void multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy() throws Throwable {
logger.debug(">>> multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy");

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

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

0 comments on commit 71dad6a

Please sign in to comment.