persevero

persevero executes a fallible operation multiple times.

Types

Represents errors that can occur during execution attempts.

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

Constructors

  • RetriesExhausted(errors: List(a))

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

  • UnallowedError(error: a)

    Indicates that an error that wasn’t allowed was encountered. Contains the specific error that caused execution to stop.

Functions

pub fn all_errors(: a) -> Bool

Convenience function that you can supply to execute’s allow parameter to allow all errors.

pub fn apply_cap(
  wait_stream wait_stream: Yielder(Int),
  max_wait_time max_wait_time: Int,
) -> Yielder(Int)

Caps each wait time at a maximum value.

pub fn apply_constant(
  wait_stream wait_stream: Yielder(Int),
  adjustment adjustment: Int,
) -> Yielder(Int)

Adds a constant integer to each wait time.

pub fn apply_jitter(
  wait_stream wait_stream: Yielder(Int),
  upper_bound upper_bound: Int,
) -> Yielder(Int)

Adds a random integer between [1, upper_bound] to each wait time.

pub fn apply_multiplier(
  wait_stream wait_stream: Yielder(Int),
  factor factor: Int,
) -> Yielder(Int)

Multiplies each wait time by a constant factor.

pub fn constant_backoff(wait_time wait_time: Int) -> Yielder(Int)

Produces a delay stream that waits for a constant amount of time: 500, 500, 500, …

pub fn custom_backoff(
  wait_time wait_time: Int,
  next_wait_time next_wait_time: fn(Int) -> Int,
) -> Yielder(Int)

Produces a delay stream with custom backoff logic.

pub fn execute(
  wait_stream wait_stream: Yielder(Int),
  allow allow: fn(a) -> Bool,
  max_attempts max_attempts: Int,
  operation operation: fn() -> Result(b, a),
) -> Result(b, Error(a))

Initiates the execution process with the specified operation.

allow sets the logic for determining whether an error should trigger another attempt. Expects a function that takes an error and returns a boolean. Use this function to match on the encountered error and return True for errors that should trigger another attempt, and False for errors that should not. To allow all errors, use all_errors.

pub fn exponential_backoff(
  wait_time wait_time: Int,
  factor factor: Int,
) -> Yielder(Int)

Produces a delay stream that waits for an exponentially-increasing amount of time: 500, 1000, 2000, 4000, …

pub fn linear_backoff(
  wait_time wait_time: Int,
  step step: Int,
) -> Yielder(Int)

Produces a delay stream that waits for a linearly-increasing amount of time: 500, 1000, 1500, …

pub fn no_backoff() -> Yielder(Int)

Produces a 0ms-delay stream: 0, 0, 0, …

Search Document