Package io.temporal.internal
Class BackoffThrottler
- java.lang.Object
-
- io.temporal.internal.BackoffThrottler
-
@NotThreadSafe public final class BackoffThrottler extends java.lang.Object
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);
whereinitialSleep
is either set toregularInitialSleep
orcongestionInitialSleep
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
Constructors Constructor Description BackoffThrottler(java.time.Duration regularInitialSleep, java.time.Duration congestionInitialSleep, java.time.Duration maxSleep, double backoffCoefficient, double maxJitterCoefficient)
Construct an instance of the throttler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
failure(io.grpc.Status.Code failureCode)
Increment failure count and set last failure code.int
getAttemptCount()
long
getSleepTime()
void
success()
Reset failure count to 0 and clear last failure code.
-
-
-
Constructor Detail
-
BackoffThrottler
public BackoffThrottler(java.time.Duration regularInitialSleep, java.time.Duration congestionInitialSleep, @Nullable java.time.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
-
-