Class BackoffThrottler
java.lang.Object
io.temporal.internal.BackoffThrottler
Used to throttle code execution in presence of failures using exponential backoff logic.
The formula used to calculate the next sleep interval is:
jitter = random number in the range [-maxJitterCoefficient, +maxJitterCoefficient]; sleepTime = min(pow(backoffCoefficient, failureCount - 1) * initialSleep * (1 + jitter), maxSleep);where
initialSleep is either set to regularInitialSleep or
congestionInitialSleep based on the most recent failure. Note that it means that
attempt X can possibly get a shorter throttle than attempt X-1, if a non-congestion failure
occurs after a congestion failure. This is the expected behaviour for all SDK.
Example usage:
BackoffThrottler throttler = new BackoffThrottler(50, 1000, 60000, 2, 0.1);
while(!stopped) {
try {
long throttleMs = throttler.getSleepTime();
if (throttleMs > 0) {
Thread.sleep(throttleMs);
}
// some code that can fail and should be throttled
...
throttler.success();
}
catch (Exception e) {
throttler.failure(
(e instanceof StatusRuntimeException)
? ((StatusRuntimeException) e).getStatus().getCode()
: Status.Code.UNKNOWN);
}
}
-
Constructor Summary
ConstructorsConstructorDescriptionBackoffThrottler(Duration regularInitialSleep, Duration congestionInitialSleep, Duration maxSleep, double backoffCoefficient, double maxJitterCoefficient) Construct an instance of the throttler. -
Method Summary
-
Constructor Details
-
BackoffThrottler
public BackoffThrottler(Duration regularInitialSleep, Duration congestionInitialSleep, @Nullable Duration maxSleep, double backoffCoefficient, double maxJitterCoefficient) Construct an instance of the throttler.- Parameters:
regularInitialSleep- time to sleep on the first failure (assuming regular failures)congestionInitialSleep- time to sleep on the first failure (for congestion failures)maxSleep- maximum time to sleep independently of number of failuresbackoffCoefficient- coefficient used to calculate the next time to sleepmaxJitterCoefficient- maximum jitter coefficient (in the range [0.0, 1.0[) to randomly add or subtract to sleep time
-
-
Method Details
-
getSleepTime
public long getSleepTime() -
getAttemptCount
public int getAttemptCount() -
success
public void success()Reset failure count to 0 and clear last failure code. -
failure
public void failure(io.grpc.Status.Code failureCode) Increment failure count and set last failure code.
-