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
Modifier and TypeClassDescriptionstatic class
-
Method Summary
Modifier and TypeMethodDescriptionbuilder()
copy()
boolean
long
get()
Increments the failure count and computes the appropriate exponential backoff.long
The milliseconds value for the first non-zero backoff.int
Returns the current failure countlong
The range of initial jitter amounts (in milliseconds) for when bothisImmediateFirst()
andisJitter()
are true.long
The maximum backoff value (on average), in milliseconds.int
The maximum number of retries that should be allowed.double
The multiplier on back-offs that is in the base of the exponent.int
hashCode()
boolean
Whether the first attempt after a failure should take place without delay.boolean
isJitter()
Whether (pseudo-)randomness should be applied when computing the exponential backoff.void
reset()
Resets the failure count for exponential backoff calculations.void
setFailures
(int failures) toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.github.twitch4j.util.IBackoffStrategy
sleep
-
Method Details
-
get
public long get()Description copied from interface:IBackoffStrategy
Increments the failure count and computes the appropriate exponential backoff.- Specified by:
get
in interfaceIBackoffStrategy
- Returns:
- the amount of milliseconds to delay before retrying.
-
reset
public void reset()Description copied from interface:IBackoffStrategy
Resets the failure count for exponential backoff calculations.- Specified by:
reset
in interfaceIBackoffStrategy
-
setFailures
public void setFailures(int failures) -
getFailures
public int getFailures()Description copied from interface:IBackoffStrategy
Returns the current failure count- Specified by:
getFailures
in interfaceIBackoffStrategy
- Returns:
- failure count
-
copy
- Returns:
- a new
ExponentialBackoffStrategy
instance 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
-