Skip to content

Commit

Permalink
Merge pull request #114 from elandau/feature/aimd_min_max
Browse files Browse the repository at this point in the history
AIMD Bounds
  • Loading branch information
elandau authored Mar 23, 2019
2 parents 3d7a960 + afbbed4 commit 5ae2be4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public final class AIMDLimit extends AbstractLimit {
private static final long DEFAULT_TIMEOUT = TimeUnit.SECONDS.toNanos(5);

public static class Builder {
private int initialLimit = 10;
private int minLimit = 20;
private int initialLimit = 20;
private int maxLimit = 200;
private double backoffRatio = 0.9;
private long timeout = DEFAULT_TIMEOUT;

Expand All @@ -37,6 +39,16 @@ public Builder initialLimit(int initialLimit) {
return this;
}

public Builder minLimit(int minLimit) {
this.minLimit = minLimit;
return this;
}

public Builder maxLimit(int maxLimit) {
this.maxLimit = maxLimit;
return this;
}

public Builder backoffRatio(double backoffRatio) {
Preconditions.checkArgument(backoffRatio < 1.0 && backoffRatio >= 0.5, "Backoff ratio must be in the range [0.5, 1.0)");
this.backoffRatio = backoffRatio;
Expand Down Expand Up @@ -66,24 +78,32 @@ public static Builder newBuilder() {

private final double backoffRatio;
private final long timeout;
private final int minLimit;
private final int maxLimit;

private AIMDLimit(Builder builder) {
super(builder.initialLimit);
this.backoffRatio = builder.backoffRatio;
this.timeout = builder.timeout;
this.maxLimit = builder.maxLimit;
this.minLimit = builder.minLimit;
}

@Override
protected int _update(long startTime, long rtt, int inflight, boolean didDrop) {
final int currentLimit = getLimit();
int currentLimit = getLimit();

if (didDrop || rtt > timeout) {
return Math.max(1, Math.min(currentLimit - 1, (int) (currentLimit * backoffRatio)));
currentLimit = (int) (currentLimit * backoffRatio);
} else if (inflight * 2 >= currentLimit) {
return currentLimit + 1;
} else {
return currentLimit;
currentLimit = currentLimit + 1;
}

if (currentLimit >= maxLimit) {
currentLimit = currentLimit / 2;
}

return Math.min(maxLimit, Math.max(minLimit, currentLimit));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public void testDefault() {

@Test
public void increaseOnSuccess() {
AIMDLimit limiter = AIMDLimit.newBuilder().initialLimit(10).build();
AIMDLimit limiter = AIMDLimit.newBuilder().initialLimit(20).build();
limiter.onSample(0, TimeUnit.MILLISECONDS.toNanos(1), 10, false);
Assert.assertEquals(11, limiter.getLimit());
Assert.assertEquals(21, limiter.getLimit());
}

@Test
public void decreaseOnDrops() {
AIMDLimit limiter = AIMDLimit.newBuilder().initialLimit(10).build();
AIMDLimit limiter = AIMDLimit.newBuilder().initialLimit(30).build();
limiter.onSample(0, 0, 0, true);
Assert.assertEquals(9, limiter.getLimit());
Assert.assertEquals(27, limiter.getLimit());
}
}

0 comments on commit 5ae2be4

Please sign in to comment.