Package com.github.twitch4j.common.util
Class ExponentialBackoffStrategy
java.lang.Object
com.github.twitch4j.common.util.ExponentialBackoffStrategy
- All Implemented Interfaces:
IBackoffStrategy
Thread-safe, configurable helper for applying the exponential backoff algorithm with optional jitter and/or truncation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class -
Method Summary
Modifier and TypeMethodDescriptionbuilder()copy()booleanlongget()Increments the failure count and computes the appropriate exponential backoff.longThe milliseconds value for the first non-zero backoff.intReturns the current failure countlongThe range of initial jitter amounts (in milliseconds) for when bothisImmediateFirst()andisJitter()are true.longThe maximum backoff value (on average), in milliseconds.intThe maximum number of retries that should be allowed.doubleThe multiplier on back-offs that is in the base of the exponent.inthashCode()booleanWhether 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) toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.twitch4j.util.IBackoffStrategy
sleep
-
Method Details
-
get
public long get()Description copied from interface:IBackoffStrategyIncrements the failure count and computes the appropriate exponential backoff.- Specified by:
getin interfaceIBackoffStrategy- Returns:
- the amount of milliseconds to delay before retrying.
-
reset
public void reset()Description copied from interface:IBackoffStrategyResets the failure count for exponential backoff calculations.- Specified by:
resetin interfaceIBackoffStrategy
-
setFailures
public void setFailures(int failures) -
getFailures
public int getFailures()Description copied from interface:IBackoffStrategyReturns the current failure count- Specified by:
getFailuresin interfaceIBackoffStrategy- Returns:
- failure count
-
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
-
hashCode
public int hashCode() -
toString
-