Package com.gmt2001.ratelimiters
Class ExponentialBackoff
java.lang.Object
com.gmt2001.ratelimiters.ExponentialBackoff
Handles backoff timing using an exponentially increasing duration strategy.
- Author:
- gmt2001
-
Constructor Summary
ConstructorsConstructorDescriptionExponentialBackoff
(long minIntervalMS, long maxIntervalMS) ConstructorExponentialBackoff
(long minIntervalMS, long maxIntervalMS, long resetIntervalMS) ConstructorExponentialBackoff
(Duration minInterval, Duration maxInterval) ConstructorExponentialBackoff
(Duration minInterval, Duration maxInterval, Duration resetInterval) Constructor -
Method Summary
Modifier and TypeMethodDescriptionvoid
Backoff()
Blocks with Thread.sleep until the next intervalvoid
BackoffAsync
(Runnable command) Calls the specified Runnable once the next interval expiresvoid
Blocks with Thread.sleep until the next interval, if not already backing offvoid
BackoffOnceAsync
(Runnable command) Calls the specified Runnable once the next interval expires, if not already backing offvoid
Cancels a scheduled reset initiated by a call to ResetInboolean
Returns whether a backoff is currently in progressReturns the last timestamp when a backoff was completedlong
Determines and returns the next interval to backoff forint
Returns the total number of times the backoff has been used since the last resetboolean
Returns true if the previous interval was equal to the max intervalvoid
Reset()
Resets the backoff to use the minimum values on the next callvoid
Resets the backoff to use the minimum values on the next call, but only if duration expires without CancelReset being called
-
Constructor Details
-
ExponentialBackoff
Constructor- Parameters:
minInterval
- Minimum backoff intervalmaxInterval
- Maximum backoff interval
-
ExponentialBackoff
Constructor- Parameters:
minInterval
- Minimum backoff intervalmaxInterval
- Maximum backoff intervalresetInterval
- Time since last backoff until an auto-reset occurs;null
to disable
-
ExponentialBackoff
public ExponentialBackoff(long minIntervalMS, long maxIntervalMS) Constructor- Parameters:
minIntervalMS
- Minimum backoff interval, in MSmaxIntervalMS
- Maximum backoff interval, in MS
-
ExponentialBackoff
public ExponentialBackoff(long minIntervalMS, long maxIntervalMS, long resetIntervalMS) Constructor- Parameters:
minIntervalMS
- Minimum backoff interval, in MSmaxIntervalMS
- Maximum backoff interval, in MSresetIntervalMS
- Time since last backoff until an auto-reset occurs;-1
to disable
-
-
Method Details
-
BackoffOnce
public void BackoffOnce()Blocks with Thread.sleep until the next interval, if not already backing off -
Backoff
public void Backoff()Blocks with Thread.sleep until the next interval -
BackoffOnceAsync
Calls the specified Runnable once the next interval expires, if not already backing off- Parameters:
command
- The Runnable to callback
-
BackoffAsync
Calls the specified Runnable once the next interval expires- Parameters:
command
- The Runnable to callback
-
Reset
public void Reset()Resets the backoff to use the minimum values on the next call -
ResetIn
Resets the backoff to use the minimum values on the next call, but only if duration expires without CancelReset being called- Parameters:
duration
- the duration to wait before resetting this ExponentialBackoff
-
CancelReset
public void CancelReset()Cancels a scheduled reset initiated by a call to ResetIn -
GetIsBackingOff
public boolean GetIsBackingOff()Returns whether a backoff is currently in progress- Returns:
- true if a backoff is executing; false otherwise
-
GetLastBackoff
Returns the last timestamp when a backoff was completed- Returns:
- The timestamp
-
GetNextInterval
public long GetNextInterval()Determines and returns the next interval to backoff for- Returns:
- The next interval that will be used when Backoff() or BackoffAsync(command) is called, in milliseconds
-
GetTotalIterations
public int GetTotalIterations()Returns the total number of times the backoff has been used since the last reset- Returns:
- The total
-
IsAtMaxInterval
public boolean IsAtMaxInterval()Returns true if the previous interval was equal to the max interval- Returns:
- true if the previous interval was equal to the max interval
-