HTTPCLIENT-2037: AIMDBackoffManager should use TimeValue

This commit is contained in:
Michael Osipov 2019-12-15 12:21:59 +01:00
parent ebccd5d225
commit 667fc9e218
2 changed files with 14 additions and 12 deletions

View File

@ -34,6 +34,7 @@ import org.apache.hc.client5.http.classic.BackoffManager;
import org.apache.hc.core5.annotation.Experimental;
import org.apache.hc.core5.pool.ConnPoolControl;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.TimeValue;
/**
* <p>The {@code AIMDBackoffManager} applies an additive increase,
@ -62,7 +63,7 @@ public class AIMDBackoffManager implements BackoffManager {
private final Clock clock;
private final Map<HttpRoute, Long> lastRouteProbes;
private final Map<HttpRoute, Long> lastRouteBackoffs;
private long coolDown = 5 * 1000L;
private TimeValue coolDown = TimeValue.ofSeconds(5L);
private double backoffFactor = 0.5;
private int cap = 2; // Per RFC 2616 sec 8.1.4
@ -90,7 +91,7 @@ public class AIMDBackoffManager implements BackoffManager {
final int curr = connPerRoute.getMaxPerRoute(route);
final Long lastUpdate = getLastUpdate(lastRouteBackoffs, route);
final long now = clock.getCurrentTime();
if (now - lastUpdate.longValue() < coolDown) {
if (now - lastUpdate.longValue() < coolDown.toMillis()) {
return;
}
connPerRoute.setMaxPerRoute(route, getBackedOffPoolSize(curr));
@ -113,7 +114,8 @@ public class AIMDBackoffManager implements BackoffManager {
final Long lastProbe = getLastUpdate(lastRouteProbes, route);
final Long lastBackoff = getLastUpdate(lastRouteBackoffs, route);
final long now = clock.getCurrentTime();
if (now - lastProbe.longValue() < coolDown || now - lastBackoff.longValue() < coolDown) {
if (now - lastProbe.longValue() < coolDown.toMillis()
|| now - lastBackoff.longValue() < coolDown.toMillis()) {
return;
}
connPerRoute.setMaxPerRoute(route, max);
@ -144,15 +146,14 @@ public class AIMDBackoffManager implements BackoffManager {
}
/**
* Sets the amount of time, in milliseconds, to wait between
* adjustments in pool sizes for a given host, to allow
* enough time for the adjustments to take effect. Defaults
* to 5000L (5 seconds).
* @param l must be positive
* Sets the amount of time to wait between adjustments in
* pool sizes for a given host, to allow enough time for
* the adjustments to take effect. Defaults to 5 seconds.
* @param coolDown must be positive
*/
public void setCooldownMillis(final long l) {
Args.positive(coolDown, "Cool down");
coolDown = l;
public void setCoolDown(final TimeValue coolDown) {
Args.positive(coolDown.getDuration(), "coolDown");
this.coolDown = coolDown;
}
/**

View File

@ -34,6 +34,7 @@ import java.util.Random;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.classic.BackoffManager;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.util.TimeValue;
import org.junit.Before;
import org.junit.Test;
@ -166,7 +167,7 @@ public class TestAIMDBackoffManager {
cd++;
}
final long now = System.currentTimeMillis();
impl.setCooldownMillis(cd);
impl.setCoolDown(TimeValue.ofMilliseconds(cd));
clock.setCurrentTime(now);
impl.probe(route);
final int max0 = connPerRoute.getMaxPerRoute(route);