-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CappedExponential Delay (#416)
- Loading branch information
Showing
2 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/software/amazon/cloudformation/proxy/delay/CappedExponential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package software.amazon.cloudformation.proxy.delay; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class CappedExponential extends MinDelayAbstractBase { | ||
|
||
final double powerBy; | ||
|
||
final long startTimeInMillis; | ||
|
||
final Duration maxDelay; | ||
|
||
CappedExponential(Duration timeout, Duration minDelay, Double powerBy, Duration maxDelay) { | ||
super(timeout, minDelay); | ||
Preconditions.checkArgument(powerBy >= 1.0, "powerBy >= 1.0"); | ||
Preconditions.checkArgument(maxDelay != null && maxDelay.toMillis() >= 0, "maxDelay must be > 0"); | ||
Preconditions.checkArgument(maxDelay.compareTo(minDelay) >= 0, "maxDelay.compareTo(minDelay) >= 0"); | ||
this.powerBy = powerBy; | ||
this.maxDelay = maxDelay; | ||
this.startTimeInMillis = System.currentTimeMillis(); | ||
} | ||
|
||
public static Builder of() { | ||
return new Builder(); | ||
} | ||
|
||
public static final class Builder extends MinDelayBasedBuilder<CappedExponential, Builder> { | ||
private double powerBy = 2; | ||
private Duration maxDelay =Duration.ofSeconds(20); | ||
|
||
public CappedExponential.Builder powerBy(Double powerBy) { | ||
this.powerBy = powerBy; | ||
return this; | ||
} | ||
|
||
public CappedExponential.Builder maxDelay(Duration maxDelay) { | ||
this.maxDelay = maxDelay; | ||
return this; | ||
} | ||
|
||
@Override | ||
public CappedExponential build() { | ||
return new CappedExponential(timeout, minDelay, powerBy, maxDelay); | ||
} | ||
} | ||
|
||
@Override | ||
public Duration nextDelay(int attempt) { | ||
if (attempt == 0) { | ||
return minDelay; | ||
} | ||
if (System.currentTimeMillis() - startTimeInMillis > timeout.toMillis()) { | ||
return Duration.ZERO; | ||
} | ||
long next = Math.round(minDelay.toMillis() * Math.pow(powerBy, attempt - 1)); | ||
return Duration.ofSeconds(Math.min(maxDelay.getSeconds(), TimeUnit.MILLISECONDS.toSeconds(next))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters