Package com.tccc.kos.commons.util
Class Backoff
java.lang.Object
com.tccc.kos.commons.util.Backoff
General purpose backoff algorithm class. This is used to convert
a count to delay for various delay based backoff use cases.
This class supports multiple backoff algorithms based on the
values in the
BackoffConfig
bean. This bean can be stored
in the kOS config system allowing external system to adjust backoff
parameters and even algorithms without needing to change code.
This class can be used multiple ways:
- Internal count: Create an instance using
BackoffConfig
and let the bean track the counter. Useinc()
,clear()
and associated methods to manage the count and simply callgetDelay()
at any time to get the associated delay based on the count. - External count: Create an instance using
BackoffConfig
and callgetDelay(count)
with an externally managed count to return the associated delay. Useful in multi-threaded environments where there are multiple counts but a single config. - Static accessor: Call the static
getDelay(config, count)
method to get the associated delay. Avoids the need to create aBackoff
object if a config and count are already available.
The following types use config properties as described below:
- FIXED: A fixed delay that doesn't depend on the count
provided. Simply returns
baseDelayMs
. - LINEAR: A delay that grows linearly based on the count.
Returns
baseDelayMs
pluscount * unitDelayMs
where count is first limited tomaxCount
and the final delay is limited tomaxDelayMs
. IfmaxCount
is zero, the default limit of 25 will be used. Max delay is only applied if greater than zero. - EXPONENTIAL: A delay that grows exponentially based on
the count. Returns
baseDelayMs
plusunitDelayMs * Math.pow(unitMultipilier, count)
where count is limited tomaxCount
and the final delay is limited tomaxDelayMs
. IfmaxCount
is zero, the default limit of 10 will be used. A hard limit of 40 will be enforced. The power of the multiplier will stop if the value exceeds 30 bits ormaxDelayMs
to prevent overflow. IfunitMultiplier
is less than 1.0 then the default value of 1.5 will be used to avoid accelerating timeouts. Max delay is only applied if greater than zero.
- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final double
static final int
-
Constructor Summary
ConstructorsConstructorDescriptionBackoff()
Create a backoff using a default config.Backoff
(BackoffConfig config) Create a backoff using the specified config. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear the internal count.int
getCount()
Return the value of the internal count.long
getDelay()
Return the delay for the internal count.long
getDelay
(int count) Return the delay for the specified count.static long
getDelay
(BackoffConfig config, int count) Return the delay for the specified config and count.void
inc()
Increment the internal count.void
setConfig
(BackoffConfig config) Set theBackoffConfig
to use.void
setCount
(int count) Set the internal count to a specific value.
-
Field Details
-
DEFAULT_LINEAR_MAX_COUNT
public static final int DEFAULT_LINEAR_MAX_COUNT- See Also:
-
DEFAULT_EXPONENTIAL_MAX_COUNT
public static final int DEFAULT_EXPONENTIAL_MAX_COUNT- See Also:
-
DEFAULT_EXPONENTIAL_MULTIPLIER
public static final double DEFAULT_EXPONENTIAL_MULTIPLIER- See Also:
-
-
Constructor Details
-
Backoff
public Backoff()Create a backoff using a default config. -
Backoff
Create a backoff using the specified config.- Parameters:
config
- the config used to compute the delay
-
-
Method Details
-
setConfig
Set theBackoffConfig
to use. This can be set at any time. -
getCount
public int getCount()Return the value of the internal count. -
inc
public void inc()Increment the internal count. -
setCount
public void setCount(int count) Set the internal count to a specific value. -
clear
public void clear()Clear the internal count. -
getDelay
public long getDelay()Return the delay for the internal count. -
getDelay
public long getDelay(int count) Return the delay for the specified count. -
getDelay
Return the delay for the specified config and count.- Parameters:
config
- the backoff configurationcount
- the failure count
-
getConfig
-