Use new pooling connection manager per default

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1174683 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-09-23 12:06:12 +00:00
parent 9ee4a41aa3
commit 28fbd4df7c
10 changed files with 191 additions and 60 deletions

View File

@ -34,15 +34,13 @@ package org.apache.http.client.params;
public interface ClientPNames {
/**
* Defines the class name of the default {@link org.apache.http.conn.ClientConnectionManager}
* <p>
* This parameter expects a value of type {@link String}.
* </p>
* @deprecated do not use
*/
@Deprecated
public static final String CONNECTION_MANAGER_FACTORY_CLASS_NAME = "http.connection-manager.factory-class-name";
/**
* @deprecated use #CONNECTION_MANAGER_FACTORY_CLASS_NAME
* @deprecated do not use
*/
@Deprecated
public static final String CONNECTION_MANAGER_FACTORY = "http.connection-manager.factory-object";

View File

@ -51,6 +51,7 @@ public class ClientParamBean extends HttpAbstractParamBean {
super(params);
}
@Deprecated
public void setConnectionManagerFactoryClassName (final String factory) {
params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, factory);
}

View File

@ -32,6 +32,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.pool.ConnPoolControl;
/**
* This class maintains a map of HTTP routes to maximum number of connections allowed
@ -40,7 +41,10 @@ import org.apache.http.conn.routing.HttpRoute;
* a fine-grained control of connections on a per route basis.
*
* @since 4.0
*
* @deprecated use {@link ConnPoolControl}
*/
@Deprecated
@ThreadSafe
public final class ConnPerRouteBean implements ConnPerRoute {

View File

@ -87,6 +87,16 @@ public final class RouteTracker implements RouteInfo, Cloneable {
this.layered = LayerType.PLAIN;
}
/**
* @since 4.2
*/
public void reset() {
this.connected = false;
this.proxyChain = null;
this.tunnelled = TunnelType.PLAIN;
this.layered = LayerType.PLAIN;
this.secure = false;
}
/**
* Creates a new tracker for the given route.

View File

@ -29,8 +29,8 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.http.client.BackoffManager;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.pool.ConnPoolControl;
/**
* <p>The <code>AIMDBackoffManager</code> applies an additive increase,
@ -54,40 +54,39 @@ import org.apache.http.conn.routing.HttpRoute;
*/
public class AIMDBackoffManager implements BackoffManager {
private ConnPerRouteBean connPerRoute;
private Clock clock;
private final ConnPoolControl<HttpRoute> connPerRoute;
private final Clock clock;
private final Map<HttpRoute,Long> lastRouteProbes;
private final Map<HttpRoute,Long> lastRouteBackoffs;
private long coolDown = 5 * 1000L;
private double backoffFactor = 0.5;
private int cap = ConnPerRouteBean.DEFAULT_MAX_CONNECTIONS_PER_ROUTE;
private Map<HttpRoute,Long> lastRouteProbes =
new HashMap<HttpRoute,Long>();
private Map<HttpRoute,Long> lastRouteBackoffs =
new HashMap<HttpRoute,Long>();
private int cap = 2; // Per RFC 2616 sec 8.1.4
/**
* Creates an <code>AIMDBackoffManager</code> to manage
* per-host connection pool sizes represented by the
* given {@link ConnPerRouteBean}.
* given {@link MockConnPoolControl}.
* @param connPerRoute per-host routing maximums to
* be managed
*/
public AIMDBackoffManager(ConnPerRouteBean connPerRoute) {
public AIMDBackoffManager(ConnPoolControl<HttpRoute> connPerRoute) {
this(connPerRoute, new SystemClock());
}
AIMDBackoffManager(ConnPerRouteBean connPerRoute, Clock clock) {
AIMDBackoffManager(ConnPoolControl<HttpRoute> connPerRoute, Clock clock) {
this.clock = clock;
this.connPerRoute = connPerRoute;
this.lastRouteProbes = new HashMap<HttpRoute,Long>();
this.lastRouteBackoffs = new HashMap<HttpRoute,Long>();
}
public void backOff(HttpRoute route) {
synchronized(connPerRoute) {
int curr = connPerRoute.getMaxForRoute(route);
int curr = connPerRoute.getMaxPerRoute(route);
Long lastUpdate = getLastUpdate(lastRouteBackoffs, route);
long now = clock.getCurrentTime();
if (now - lastUpdate.longValue() < coolDown) return;
connPerRoute.setMaxForRoute(route, getBackedOffPoolSize(curr));
connPerRoute.setMaxPerRoute(route, getBackedOffPoolSize(curr));
lastRouteBackoffs.put(route, Long.valueOf(now));
}
}
@ -99,14 +98,14 @@ public class AIMDBackoffManager implements BackoffManager {
public void probe(HttpRoute route) {
synchronized(connPerRoute) {
int curr = connPerRoute.getMaxForRoute(route);
int curr = connPerRoute.getMaxPerRoute(route);
int max = (curr >= cap) ? cap : curr + 1;
Long lastProbe = getLastUpdate(lastRouteProbes, route);
Long lastBackoff = getLastUpdate(lastRouteBackoffs, route);
long now = clock.getCurrentTime();
if (now - lastProbe.longValue() < coolDown || now - lastBackoff.longValue() < coolDown)
return;
connPerRoute.setMaxForRoute(route, max);
connPerRoute.setMaxPerRoute(route, max);
lastRouteProbes.put(route, Long.valueOf(now));
}
}

View File

@ -76,8 +76,8 @@ import org.apache.http.impl.auth.DigestSchemeFactory;
import org.apache.http.impl.auth.NTLMSchemeFactory;
import org.apache.http.impl.auth.NegotiateSchemeFactory;
import org.apache.http.impl.conn.DefaultHttpRoutePlanner;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.conn.SchemeRegistryFactory;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.cookie.BestMatchSpecFactory;
import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
import org.apache.http.impl.cookie.IgnoreSpecFactory;
@ -178,8 +178,8 @@ import org.apache.http.util.EntityUtils;
*
* @since 4.0
*/
@ThreadSafe
@SuppressWarnings("deprecation")
@ThreadSafe
public abstract class AbstractHttpClient implements HttpClient {
private final Log log = LogFactory.getLog(getClass());
@ -325,7 +325,7 @@ public abstract class AbstractHttpClient implements HttpClient {
if (factory != null) {
connManager = factory.newInstance(params, registry);
} else {
connManager = new SingleClientConnManager(registry);
connManager = new PoolingClientConnectionManager(registry);
}
return connManager;

View File

@ -120,15 +120,19 @@ class ManagedClientConnectionImpl implements ManagedClientConnection {
}
public void close() throws IOException {
OperatedClientConnection conn = getConnection();
if (conn != null) {
HttpPoolEntry local = this.poolEntry;
if (local != null) {
OperatedClientConnection conn = local.getConnection();
local.getTracker().reset();
conn.close();
}
}
public void shutdown() throws IOException {
OperatedClientConnection conn = getConnection();
if (conn != null) {
HttpPoolEntry local = this.poolEntry;
if (local != null) {
OperatedClientConnection conn = local.getConnection();
local.getTracker().reset();
conn.shutdown();
}
}

View File

@ -0,0 +1,116 @@
/*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.client;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.pool.ConnPoolControl;
import org.apache.http.pool.PoolStats;
public final class MockConnPoolControl implements ConnPoolControl<HttpRoute> {
private final ConcurrentHashMap<HttpRoute, Integer> maxPerHostMap;
private volatile int totalMax;
private volatile int defaultMax;
public MockConnPoolControl() {
super();
this.maxPerHostMap = new ConcurrentHashMap<HttpRoute, Integer>();
this.totalMax = 20;
this.defaultMax = 2;
}
public void setMaxTotal(int max) {
this.totalMax = max;
}
public int getMaxTotal() {
return this.totalMax;
}
public PoolStats getTotalStats() {
return new PoolStats(-1, -1, -1, this.totalMax);
}
public PoolStats getStats(final HttpRoute route) {
return new PoolStats(-1, -1, -1, getMaxPerRoute(route));
}
public int getDefaultMaxPerRoute() {
return this.defaultMax;
}
public void setDefaultMaxPerRoute(int max) {
if (max < 1) {
throw new IllegalArgumentException
("The maximum must be greater than 0.");
}
this.defaultMax = max;
}
public void setMaxPerRoute(final HttpRoute route, int max) {
if (route == null) {
throw new IllegalArgumentException
("HTTP route may not be null.");
}
if (max < 1) {
throw new IllegalArgumentException
("The maximum must be greater than 0.");
}
this.maxPerHostMap.put(route, Integer.valueOf(max));
}
public int getMaxPerRoute(final HttpRoute route) {
if (route == null) {
throw new IllegalArgumentException
("HTTP route may not be null.");
}
Integer max = this.maxPerHostMap.get(route);
if (max != null) {
return max.intValue();
} else {
return this.defaultMax;
}
}
public void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
if (map == null) {
return;
}
this.maxPerHostMap.clear();
this.maxPerHostMap.putAll(map);
}
@Override
public String toString() {
return this.maxPerHostMap.toString();
}
}

View File

@ -31,22 +31,20 @@ import java.util.Random;
import org.apache.http.HttpHost;
import org.apache.http.client.BackoffManager;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.routing.HttpRoute;
import org.junit.Before;
import org.junit.Test;
public class TestAIMDBackoffManager {
private AIMDBackoffManager impl;
private ConnPerRouteBean connPerRoute;
private MockConnPoolControl connPerRoute;
private HttpRoute route;
private MockClock clock;
@Before
public void setUp() {
connPerRoute = new ConnPerRouteBean();
connPerRoute = new MockConnPoolControl();
route = new HttpRoute(new HttpHost("localhost:80"));
clock = new MockClock();
impl = new AIMDBackoffManager(connPerRoute, clock);
@ -60,100 +58,100 @@ public class TestAIMDBackoffManager {
@Test
public void halvesConnectionsOnBackoff() {
connPerRoute.setMaxForRoute(route, 4);
connPerRoute.setMaxPerRoute(route, 4);
impl.backOff(route);
assertEquals(2, connPerRoute.getMaxForRoute(route));
assertEquals(2, connPerRoute.getMaxPerRoute(route));
}
@Test
public void doesNotBackoffBelowOneConnection() {
connPerRoute.setMaxForRoute(route, 1);
connPerRoute.setMaxPerRoute(route, 1);
impl.backOff(route);
assertEquals(1, connPerRoute.getMaxForRoute(route));
assertEquals(1, connPerRoute.getMaxPerRoute(route));
}
@Test
public void increasesByOneOnProbe() {
connPerRoute.setMaxForRoute(route, 2);
connPerRoute.setMaxPerRoute(route, 2);
impl.probe(route);
assertEquals(3, connPerRoute.getMaxForRoute(route));
assertEquals(3, connPerRoute.getMaxPerRoute(route));
}
@Test
public void doesNotIncreaseBeyondPerHostMaxOnProbe() {
connPerRoute.setDefaultMaxPerRoute(5);
connPerRoute.setMaxForRoute(route, 5);
connPerRoute.setMaxPerRoute(route, 5);
impl.setPerHostConnectionCap(5);
impl.probe(route);
assertEquals(5, connPerRoute.getMaxForRoute(route));
assertEquals(5, connPerRoute.getMaxPerRoute(route));
}
@Test
public void backoffDoesNotAdjustDuringCoolDownPeriod() {
connPerRoute.setMaxForRoute(route, 4);
connPerRoute.setMaxPerRoute(route, 4);
long now = System.currentTimeMillis();
clock.setCurrentTime(now);
impl.backOff(route);
long max = connPerRoute.getMaxForRoute(route);
long max = connPerRoute.getMaxPerRoute(route);
clock.setCurrentTime(now + 1);
impl.backOff(route);
assertEquals(max, connPerRoute.getMaxForRoute(route));
assertEquals(max, connPerRoute.getMaxPerRoute(route));
}
@Test
public void backoffStillAdjustsAfterCoolDownPeriod() {
connPerRoute.setMaxForRoute(route, 8);
connPerRoute.setMaxPerRoute(route, 8);
long now = System.currentTimeMillis();
clock.setCurrentTime(now);
impl.backOff(route);
long max = connPerRoute.getMaxForRoute(route);
long max = connPerRoute.getMaxPerRoute(route);
clock.setCurrentTime(now + 10 * 1000L);
impl.backOff(route);
assertTrue(max == 1 || max > connPerRoute.getMaxForRoute(route));
assertTrue(max == 1 || max > connPerRoute.getMaxPerRoute(route));
}
@Test
public void probeDoesNotAdjustDuringCooldownPeriod() {
connPerRoute.setMaxForRoute(route, 4);
connPerRoute.setMaxPerRoute(route, 4);
long now = System.currentTimeMillis();
clock.setCurrentTime(now);
impl.probe(route);
long max = connPerRoute.getMaxForRoute(route);
long max = connPerRoute.getMaxPerRoute(route);
clock.setCurrentTime(now + 1);
impl.probe(route);
assertEquals(max, connPerRoute.getMaxForRoute(route));
assertEquals(max, connPerRoute.getMaxPerRoute(route));
}
@Test
public void probeStillAdjustsAfterCoolDownPeriod() {
connPerRoute.setMaxForRoute(route, 8);
connPerRoute.setMaxPerRoute(route, 8);
long now = System.currentTimeMillis();
clock.setCurrentTime(now);
impl.probe(route);
long max = connPerRoute.getMaxForRoute(route);
long max = connPerRoute.getMaxPerRoute(route);
clock.setCurrentTime(now + 10 * 1000L);
impl.probe(route);
assertTrue(max < connPerRoute.getMaxForRoute(route));
assertTrue(max < connPerRoute.getMaxPerRoute(route));
}
@Test
public void willBackoffImmediatelyEvenAfterAProbe() {
connPerRoute.setMaxForRoute(route, 8);
connPerRoute.setMaxPerRoute(route, 8);
long now = System.currentTimeMillis();
clock.setCurrentTime(now);
impl.probe(route);
long max = connPerRoute.getMaxForRoute(route);
long max = connPerRoute.getMaxPerRoute(route);
clock.setCurrentTime(now + 1);
impl.backOff(route);
assertTrue(connPerRoute.getMaxForRoute(route) < max);
assertTrue(connPerRoute.getMaxPerRoute(route) < max);
}
@Test
public void backOffFactorIsConfigurable() {
connPerRoute.setMaxForRoute(route, 10);
connPerRoute.setMaxPerRoute(route, 10);
impl.setBackoffFactor(0.9);
impl.backOff(route);
assertEquals(9, connPerRoute.getMaxForRoute(route));
assertEquals(9, connPerRoute.getMaxPerRoute(route));
}
@Test
@ -165,12 +163,12 @@ public class TestAIMDBackoffManager {
impl.setCooldownMillis(cd);
clock.setCurrentTime(now);
impl.probe(route);
int max0 = connPerRoute.getMaxForRoute(route);
int max0 = connPerRoute.getMaxPerRoute(route);
clock.setCurrentTime(now);
impl.probe(route);
assertEquals(max0, connPerRoute.getMaxForRoute(route));
assertEquals(max0, connPerRoute.getMaxPerRoute(route));
clock.setCurrentTime(now + cd + 1);
impl.probe(route);
assertTrue(max0 < connPerRoute.getMaxForRoute(route));
assertTrue(max0 < connPerRoute.getMaxPerRoute(route));
}
}

View File

@ -49,6 +49,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@SuppressWarnings("deprecation")
@RunWith(MockitoJUnitRunner.class)
@Deprecated
public class TestConnPoolByRoute extends ServerTestBase {