Package com.github.twitch4j.common.util
Class ExponentialBackoffStrategy
java.lang.Object
com.github.twitch4j.common.util.ExponentialBackoffStrategy
public final class ExponentialBackoffStrategy
extends java.lang.Object
Thread-safe, configurable helper for applying the exponential backoff algorithm with optional jitter and/or truncation.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classExponentialBackoffStrategy.ExponentialBackoffStrategyBuilder -
Method Summary
Modifier and Type Method Description static ExponentialBackoffStrategy.ExponentialBackoffStrategyBuilderbuilder()ExponentialBackoffStrategycopy()booleanequals(java.lang.Object o)longget()Increments the failure count and computes the appropriate exponential backoff.longgetBaseMillis()The milliseconds value for the first non-zero backoff.intgetFailures()longgetInitialJitterRange()The range of initial jitter amounts (in milliseconds) for when bothisImmediateFirst()andisJitter()are true.longgetMaximumBackoff()The maximum backoff value (on average), in milliseconds.intgetMaxRetries()The maximum number of retries that should be allowed.doublegetMultiplier()The multiplier on back-offs that is in the base of the exponent.inthashCode()booleanisImmediateFirst()Whether the first attempt after a failure should take place without delay.booleanisJitter()Whether (pseudo-)randomness should be applied when computing the exponential backoff.voidreset()Resets the failure count for exponential backoff calculations.voidsetFailures(int failures)booleansleep()Sleeps for the delay suggested byget().ExponentialBackoffStrategy.ExponentialBackoffStrategyBuildertoBuilder()java.lang.StringtoString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Method Details
-
sleep
public boolean sleep()Sleeps for the delay suggested byget().- Returns:
- whether the sleep was successful (could be false if maximum attempts have been hit, for example).
-
get
public long get()Increments the failure count and computes the appropriate exponential backoff.- Returns:
- the amount of milliseconds to delay before retrying.
-
reset
public void reset()Resets the failure count for exponential backoff calculations. -
setFailures
public void setFailures(int failures) -
getFailures
public int getFailures() -
copy
- Returns:
- a new
ExponentialBackoffStrategyinstance with the same configuration settings (and no failures).
-
builder
-
toBuilder
-
getMaximumBackoff
public long getMaximumBackoff()The maximum backoff value (on average), in milliseconds. If set to a negative value, the algorithm will not be of the truncated variety. -
getMultiplier
public double getMultiplier()The multiplier on back-offs that is in the base of the exponent.The default is 2, which results in doubling of average delays with additional failures. This generally should be set to a value greater than 1 so that delays tend to increase with more failures.
-
isImmediateFirst
public boolean isImmediateFirst()Whether the first attempt after a failure should take place without delay.To avoid a "stampeding herd" of reconnecting clients,
ExponentialBackoffStrategy.ExponentialBackoffStrategyBuilder.jitter(boolean)can be enabled andExponentialBackoffStrategy.ExponentialBackoffStrategyBuilder.initialJitterRange(long)can optionally be configured. -
isJitter
public boolean isJitter()Whether (pseudo-)randomness should be applied when computing the exponential backoff.Highly useful for avoiding the thundering herd problem.
-
getInitialJitterRange
public long getInitialJitterRange()The range of initial jitter amounts (in milliseconds) for when bothisImmediateFirst()andisJitter()are true. -
getBaseMillis
public long getBaseMillis()The milliseconds value for the first non-zero backoff. WhenisJitter()is true, this becomes an average targeted value rather than a strictly enforced constant. -
getMaxRetries
public int getMaxRetries()The maximum number of retries that should be allowed.A negative value corresponds to no limit. A zero value corresponds to no retries allowed. A positive value enforces a specific maximum.
-
equals
public boolean equals(java.lang.Object o)- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-