Skip to content

Commit

Permalink
Rename ConnectionStrategy into KeepAliveStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Oct 24, 2015
1 parent 5db0ee2 commit 002734a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 108 deletions.
17 changes: 8 additions & 9 deletions client/src/main/java/org/asynchttpclient/AdvancedConfig.java
Expand Up @@ -25,12 +25,11 @@
import java.util.HashMap;
import java.util.Map;

import org.asynchttpclient.channel.pool.ConnectionStrategy;
import org.asynchttpclient.channel.pool.KeepAliveStrategy;
import org.asynchttpclient.netty.EagerNettyResponseBodyPart;
import org.asynchttpclient.netty.LazyNettyResponseBodyPart;
import org.asynchttpclient.netty.NettyResponseBodyPart;
import org.asynchttpclient.netty.channel.pool.ChannelPool;
import org.asynchttpclient.netty.handler.DefaultConnectionStrategy;
import org.asynchttpclient.netty.ws.NettyWebSocket;

public class AdvancedConfig {
Expand All @@ -44,7 +43,7 @@ public class AdvancedConfig {
private final ChannelPool channelPool;
private final Timer nettyTimer;
private final NettyWebSocketFactory nettyWebSocketFactory;
private final ConnectionStrategy connectionStrategy;
private final KeepAliveStrategy connectionStrategy;

public AdvancedConfig(//
Map<ChannelOption<Object>, Object> channelOptions,//
Expand All @@ -56,7 +55,7 @@ public AdvancedConfig(//
ChannelPool channelPool,//
Timer nettyTimer,//
NettyWebSocketFactory nettyWebSocketFactory,//
ConnectionStrategy connectionStrategy) {
KeepAliveStrategy connectionStrategy) {

assertNotNull(responseBodyPartFactory, "responseBodyPartFactory");
assertNotNull(nettyWebSocketFactory, "nettyWebSocketFactory");
Expand Down Expand Up @@ -110,7 +109,7 @@ public NettyWebSocketFactory getNettyWebSocketFactory() {
return nettyWebSocketFactory;
}

public ConnectionStrategy getConnectionStrategy() {
public KeepAliveStrategy getKeepAliveStrategy() {
return connectionStrategy;
}

Expand All @@ -125,7 +124,7 @@ public static class Builder {
private ChannelPool channelPool;
private Timer nettyTimer;
private NettyWebSocketFactory nettyWebSocketFactory = new DefaultNettyWebSocketFactory();
private ConnectionStrategy connectionStrategy = new DefaultConnectionStrategy();
private KeepAliveStrategy keepAliveStrategy = KeepAliveStrategy.DefaultKeepAliveStrategy.INSTANCE;

/**
* @param name the name of the ChannelOption
Expand Down Expand Up @@ -179,8 +178,8 @@ public Builder setNettyWebSocketFactory(NettyWebSocketFactory nettyWebSocketFact
return this;
}

public Builder setConnectionStrategy(ConnectionStrategy connectionStrategy) {
this.connectionStrategy = connectionStrategy;
public Builder setKeepAliveStrategy(KeepAliveStrategy keepAliveStrategy) {
this.keepAliveStrategy = keepAliveStrategy;
return this;
}

Expand All @@ -195,7 +194,7 @@ public AdvancedConfig build() {
channelPool,//
nettyTimer,//
nettyWebSocketFactory,//
connectionStrategy);
keepAliveStrategy);
}
}

Expand Down

This file was deleted.

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.channel.pool;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaders.Values.*;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpVersion;

import org.asynchttpclient.Request;

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
* @return true if the connection should be kept alive, false if it should be closed.
*/
boolean keepAlive(Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse);

/**
* Connection strategy implementing standard HTTP 1.0/1.1 behaviour.
*/
enum DefaultKeepAliveStrategy implements KeepAliveStrategy {

INSTANCE;

/**
* 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) {

String responseConnectionHeader = connectionHeader(response);

if (CLOSE.equalsIgnoreCase(responseConnectionHeader)) {
return false;
} else {
String requestConnectionHeader = connectionHeader(request);

if (request.getProtocolVersion() == HttpVersion.HTTP_1_0) {
// only use keep-alive if both parties agreed upon it
return KEEP_ALIVE.equalsIgnoreCase(requestConnectionHeader) && KEEP_ALIVE.equalsIgnoreCase(responseConnectionHeader);

} else {
// 1.1+, keep-alive is default behavior
return !CLOSE.equalsIgnoreCase(requestConnectionHeader);
}
}
}

private String connectionHeader(HttpMessage message) {
return message.headers().get(CONNECTION);
}
}
}

This file was deleted.

Expand Up @@ -38,7 +38,7 @@
import org.asynchttpclient.Realm;
import org.asynchttpclient.Request;
import org.asynchttpclient.RequestBuilder;
import org.asynchttpclient.channel.pool.ConnectionStrategy;
import org.asynchttpclient.channel.pool.KeepAliveStrategy;
import org.asynchttpclient.handler.StreamedAsyncHandler;
import org.asynchttpclient.netty.Callback;
import org.asynchttpclient.netty.NettyResponseBodyPart;
Expand All @@ -56,12 +56,11 @@

public final class HttpProtocol extends Protocol {

private final ConnectionStrategy connectionStrategy;
private final KeepAliveStrategy connectionStrategy;

public HttpProtocol(ChannelManager channelManager, AsyncHttpClientConfig config, AdvancedConfig advancedConfig, NettyRequestSender requestSender) {
super(channelManager, config, advancedConfig, requestSender);

connectionStrategy = advancedConfig.getConnectionStrategy();
connectionStrategy = advancedConfig.getKeepAliveStrategy();
}

private void kerberosChallenge(Channel channel,//
Expand Down
4 changes: 2 additions & 2 deletions client/src/test/java/org/asynchttpclient/BasicHttpsTest.java
Expand Up @@ -29,7 +29,7 @@

import javax.servlet.http.HttpServletResponse;

import org.asynchttpclient.channel.pool.ConnectionStrategy;
import org.asynchttpclient.channel.pool.KeepAliveStrategy;
import org.asynchttpclient.test.EventCollectingHandler;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -70,7 +70,7 @@ public void multipleSSLRequestsTest() throws Exception {
@Test(groups = { "standalone", "default_provider" })
public void multipleSSLWithoutCacheTest() throws Exception {

AdvancedConfig advancedConfig = advancedConfig().setConnectionStrategy(new ConnectionStrategy() {
AdvancedConfig advancedConfig = advancedConfig().setKeepAliveStrategy(new KeepAliveStrategy() {

@Override
public boolean keepAlive(Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse) {
Expand Down

0 comments on commit 002734a

Please sign in to comment.