java.lang.Object
io.aiven.commons.timing.Backoff
Performs a delay based on the number of successive
delay() or
cleanDelay() calls without a reset(). Delay increases
exponentially but never exceeds the time remaining by more than 0.512
seconds.
There are several use cases:
Case 1: There is a hard limit to the total amount of time spent waiting and
processing.
In this case a Timer is initialized to the maximum time that the
processing may take. The timer will specify the amount of time that remains
for the process. During processing
- As long as the timer returns a value > 0 the backoff may sleep.
- If the backoff sleep time is greater than the time remaining the timer is
aborted and backoff does not sleep. In this use case the
Timer.isExpired()method should be checked after backoff returns to see if the timer has expired or been aborted.
BackoffConfig.getSupplierOfTimeRemaining() should
return X on every call and the BackoffConfig.applyTimerRule() should
return false. During processing Backoff will always sleep, except in very
early backoff calls where jitter overcomes the delay.
Case 3: An action should be taken as many times as possible within the
allowed time.
A timer is set and used to specify the time remaining as in Case 1 above.
When the action is successful the reset() method is called. This
resets the internal max delay to the current limit specified by the timer.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final AbortTriggerA function to call to abort the timer.static final intThe value subtracted from the jitter to center it.static final intThe maximum jitter random number.protected final SupplierOfLongA supplier of the time remaining (in milliseconds) on the overriding timer. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidLikedelay()but swallows theInterruptedException.voiddelay()Delay execution based on the number of times this method has been called.longCalculates the delay without jitter.intCalculates the range of jitter in milliseconds.final voidreset()Reset the backoff time so that delay is again at the minimum.voidsetMinimumDelay(Duration duration) Set the minimum wait time.toString()
-
Field Details
-
MAX_JITTER
public static final int MAX_JITTERThe maximum jitter random number. Should be a power of 2 for speed.- See Also:
-
JITTER_SUBTRAHEND
public static final int JITTER_SUBTRAHENDThe value subtracted from the jitter to center it.- See Also:
-
timeRemaining
A supplier of the time remaining (in milliseconds) on the overriding timer. -
abortTrigger
A function to call to abort the timer.
-
-
Constructor Details
-
Backoff
Constructor.- Parameters:
config- The configuration for the backoff.
-
-
Method Details
-
reset
public final void reset()Reset the backoff time so that delay is again at the minimum. -
setMinimumDelay
Set the minimum wait time. Actual delay will be the closest power of 2 such that2^x >= duration.- Parameters:
duration- the minimum wait time.
-
estimatedDelay
public long estimatedDelay()Calculates the delay without jitter.- Returns:
- the number of milliseconds the delay will be.
-
getMaxJitter
public int getMaxJitter()Calculates the range of jitter in milliseconds.- Returns:
- the maximum jitter in milliseconds. jitter is +/- maximum jitter.
-
delay
Delay execution based on the number of times this method has been called.- Throws:
InterruptedException- If any thread interrupts this thread.
-
cleanDelay
public void cleanDelay()Likedelay()but swallows theInterruptedException. -
toString
-