Implements "adaptive" retries: every retry costs failureCost tokens from the bucket, and every success causes successReward tokens to be added to the bucket. If there are not enough tokens, retry is not attempted.
This way retries don't overload a system that is down due to a systemic failure (such as a bug in the code, excessive load etc.): retries will be attempted only as long as there are enough tokens in the bucket, then the load on the downstream system will be reduced so that it can recover. For transient failures (component failure, infrastructure issues etc.), retries still work as expected, as the bucket has enough tokens to cover the cost of multiple retries.
Instances of this class are thread-safe and are designed to be shared. Typically, a single instance should be used to proxy access to a single constrained resource.
Number of tokens to take from tokenBucket when retrying.
successReward
Number of tokens to add back to tokenBucket after a successful operation.
tokenBucket
Instance of TokenBucket. As a token bucket is thread-safe, it can be shared between different instances of AdaptiveRetry, e.g. with a different failureCost.
Function to decide if returned result Either[E, T] should be considered failure in terms of paying cost for retry. Penalty is paid only if it is decided to retry operation, the penalty will not be paid for successful operation. Defaults to true.
Attributes
Returns
The result of the function if it eventually succeeds.
Throws
anything
The exception thrown by the last attempt if the config decides to stop.
Retries an operation returning an scala.util.Either until it succeeds or the config decides to stop. Note that any exceptions thrown by the operation aren't caught and don't cause a retry to happen.
Retries an operation returning an scala.util.Either until it succeeds or the config decides to stop. Note that any exceptions thrown by the operation aren't caught and don't cause a retry to happen.
Function to decide if returned result Either[E, T] should be considered failure in terms of paying cost for retry. Penalty is paid only if it is decided to retry operation, the penalty will not be paid for successful operation. Defaults to true.
Attributes
Returns
A scala.util.Right if the function eventually succeeds, or, otherwise, a scala.util.Left with the error from the last attempt.
Retries an operation using the given error mode until it succeeds or the config decides to stop. Note that any exceptions thrown by the operation aren't caught (unless the operation catches them as part of its implementation) and don't cause a retry to happen.
Retries an operation using the given error mode until it succeeds or the config decides to stop. Note that any exceptions thrown by the operation aren't caught (unless the operation catches them as part of its implementation) and don't cause a retry to happen.
The error mode to use, which specifies when a result value is considered success, and when a failure.
operation
The operation to retry.
shouldPayFailureCost
Function to decide if returned result Either[E, T] should be considered failure in terms of paying cost for retry. Penalty is paid only if it is decided to retry operation, the penalty will not be paid for successful operation. Defaults to true.
Attributes
Returns
Either:
the result of the function if it eventually succeeds, in the context of F, as dictated by the error mode.
the error E in context F as returned by the last attempt if the config decides to stop.