gleam/retry

retry provides a flexible mechanism for executing an operation n times after an initial failure. Various aspects can be configured: the number of retry attempts, the duration between attempts, the strategy for adjusting wait times, and the types of errors that should trigger a retry.

Types

pub opaque type Config(a, b)

Represents errors that can occur during a retry operation.

pub type RetryError(a) {
  RetriesExhausted(errors: List(a))
  UnallowedError(error: a)
}

Constructors

  • RetriesExhausted(errors: List(a))

    Indicates that all retry attempts have been exhausted. Contains a list of all errors encountered during the execution attempts.

  • UnallowedError(error: a)

    Indicates that an error occurred which was not in the list of allowed errors. Contains the specific error that caused the retry to stop.

Functions

pub fn allow(
  config: Config(a, b),
  allow allow: fn(b) -> Bool,
) -> Config(a, b)

Sets the logic for determining whether an error should trigger a retry. Expects a function that takes an error and returns a boolean. Use this function to match on your error types and return True for errors that should trigger a retry, and False for errors that should not.

pub fn backoff(
  config: Config(a, b),
  next_wait_time next_wait_time: fn(Int) -> Int,
) -> Config(a, b)

Sets the backoff strategy for increasing wait times between retry attempts. Expects a function that takes the previous wait time and returns a new wait time.

pub fn execute(
  config: Config(a, b),
  operation operation: fn() -> Result(a, b),
) -> Result(a, RetryError(b))

Initiates the retry operation with the provided configuration and operation.

Returns Ok(a) if the operation succeeds, or Error(RetryError(b)) if all attempts fail. The Error will be either RetriesExhausted containing a list of all encountered errors, or UnallowedError containing the first unallowed error encountered.

pub fn new(
  max_attempts max_attempts: Int,
  wait_time wait_time: Int,
) -> Config(a, b)

Creates a new configuration with the specified max attempts and wait time. Configuration defaults:

  • next_wait_time: constant
  • allow: all errors
Search Document