AdaptiveRetry

ox.resilience.AdaptiveRetry
See theAdaptiveRetry companion object
case class AdaptiveRetry(tokenBucket: TokenBucket, failureCost: Int, successReward: Int)

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.

An instance with default parameters can be created using AdaptiveRetry.default.

Inspired by:

Value parameters

failureCost

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.

Attributes

Companion
object
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def retry[T](config: RetryConfig[Throwable, T], shouldPayFailureCost: (Either[Throwable, T]) => Boolean)(operation: => T): T

Retries an operation returning a direct result until it succeeds or the config decides to stop.

Retries an operation returning a direct result until it succeeds or the config decides to stop.

Value parameters

config

The retry config. Includes a Schedule which determines the intervals between invocations and number of attempts to execute the operation.

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

The result of the function when it eventually succeeds.

Throws
anything

The exception thrown by the last attempt if the config decides to stop retrying.

def retry[T](config: RetryConfig[Throwable, T])(operation: => T): T

Retries an operation returning a direct result until it succeeds or the config decides to stop.

Retries an operation returning a direct result until it succeeds or the config decides to stop.

Value parameters

config

The retry config. Includes a Schedule which determines the intervals between invocations and number of attempts to execute the operation.

operation

The operation to retry.

Attributes

Returns

The result of the function when it eventually succeeds.

Throws
anything

The exception thrown by the last attempt if the config decides to stop retrying.

def retry[T](schedule: Schedule, shouldPayFailureCost: (Either[Throwable, T]) => Boolean)(operation: => T): T

Retries an operation returning a direct result until it succeeds or the config decides to stop. Uses the default RetryConfig, with the given Schedule.

Retries an operation returning a direct result until it succeeds or the config decides to stop. Uses the default RetryConfig, with the given Schedule.

Value parameters

operation

The operation to retry.

schedule

The schedule which determines the intervals between invocations and number of attempts to execute the operation.

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

The result of the function when it eventually succeeds.

Throws
anything

The exception thrown by the last attempt if the config decides to stop retrying.

def retry[T](schedule: Schedule)(operation: => T): T

Retries an operation returning a direct result until it succeeds or the config decides to stop. Uses the default RetryConfig, with the given Schedule.

Retries an operation returning a direct result until it succeeds or the config decides to stop. Uses the default RetryConfig, with the given Schedule.

Value parameters

operation

The operation to retry.

schedule

The schedule which determines the intervals between invocations and number of attempts to execute the operation.

Attributes

Returns

The result of the function when it eventually succeeds.

Throws
anything

The exception thrown by the last attempt if the config decides to stop retrying.

def retryEither[E, T](config: RetryConfig[E, T], shouldPayFailureCost: (Either[E, T]) => Boolean)(operation: => Either[E, T]): Either[E, T]

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.

Type parameters

E

type of error.

T

type of result of an operation.

Value parameters

config

The retry config. Includes a Schedule which determines the intervals between invocations and number of attempts to execute the operation.

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

A scala.util.Right if the function eventually succeeds, or, otherwise, a scala.util.Left with the error from the last attempt.

def retryEither[E, T](config: RetryConfig[E, T])(operation: => Either[E, T]): Either[E, T]

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.

Type parameters

E

type of error.

T

type of result of an operation.

Value parameters

config

The retry config. Includes a Schedule which determines the intervals between invocations and number of attempts to execute the operation.

operation

The operation to retry.

Attributes

Returns

A scala.util.Right if the function eventually succeeds, or, otherwise, a scala.util.Left with the error from the last attempt.

def retryEither[E, T](schedule: Schedule, shouldPayFailureCost: (Either[E, T]) => Boolean)(operation: => Either[E, T]): Either[E, T]

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. Uses the default RetryConfig, with the given Schedule.

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. Uses the default RetryConfig, with the given Schedule.

Type parameters

E

type of error.

T

type of result of an operation.

Value parameters

operation

The operation to retry.

schedule

The schedule which determines the intervals between invocations and number of attempts to execute the operation.

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

A scala.util.Right if the function eventually succeeds, or, otherwise, a scala.util.Left with the error from the last attempt.

def retryEither[E, T](schedule: Schedule)(operation: => Either[E, T]): Either[E, T]

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. Uses the default RetryConfig, with the given Schedule.

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. Uses the default RetryConfig, with the given Schedule.

Type parameters

E

type of error.

T

type of result of an operation.

Value parameters

operation

The operation to retry.

schedule

The schedule which determines the intervals between invocations and number of attempts to execute the operation.

Attributes

Returns

A scala.util.Right if the function eventually succeeds, or, otherwise, a scala.util.Left with the error from the last attempt.

def retryWithErrorMode[E, T, F[_]](errorMode: ErrorMode[E, F])(config: RetryConfig[E, T], shouldPayFailureCost: (Either[E, T]) => Boolean)(operation: => F[T]): F[T]

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.

Type parameters

E

type of error.

F

the context inside which E or T are returned.

T

type of result of an operation.

Value parameters

config

The retry config. Includes a Schedule which determines the intervals between invocations and number of attempts to execute the operation.

errorMode

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.
def retryWithErrorMode[E, T, F[_]](errorMode: ErrorMode[E, F])(config: RetryConfig[E, T])(operation: => F[T]): F[T]

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.

Type parameters

E

type of error.

F

the context inside which E or T are returned.

T

type of result of an operation.

Value parameters

config

The retry config. Includes a Schedule which determines the intervals between invocations and number of attempts to execute the operation.

errorMode

The error mode to use, which specifies when a result value is considered success, and when a failure.

operation

The operation to retry.

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.
def retryWithErrorMode[E, T, F[_]](errorMode: ErrorMode[E, F])(schedule: Schedule, shouldPayFailureCost: (Either[E, T]) => Boolean)(operation: => F[T]): F[T]

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. Uses the default RetryConfig, with the given Schedule.

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. Uses the default RetryConfig, with the given Schedule.

Type parameters

E

type of error.

F

the context inside which E or T are returned.

T

type of result of an operation.

Value parameters

errorMode

The error mode to use, which specifies when a result value is considered success, and when a failure.

operation

The operation to retry.

schedule

The schedule which determines the intervals between invocations and number of attempts to execute the operation.

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.
def retryWithErrorMode[E, T, F[_]](errorMode: ErrorMode[E, F])(schedule: Schedule)(operation: => F[T]): F[T]

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. Uses the default RetryConfig, with the given Schedule.

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. Uses the default RetryConfig, with the given Schedule.

Type parameters

E

type of error.

F

the context inside which E or T are returned.

T

type of result of an operation.

Value parameters

errorMode

The error mode to use, which specifies when a result value is considered success, and when a failure.

operation

The operation to retry.

schedule

The schedule which determines the intervals between invocations and number of attempts to execute the operation.

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.

Inherited methods

def productElementNames: Iterator[String]

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

Attributes

Inherited from:
Product