persevero

persevero executes a fallible operation multiple times.

import gleam/http/request
import gleam/httpc
import gleam/io
import persevero

pub fn main() {
  let assert Ok(request) = request.to("https://www.apple.com")

  let response = {
    use <- persevero.execute(
      wait_stream: persevero.exponential_backoff(50, 2),
      allow: persevero.all_errors,
      mode: persevero.MaxAttempts(3),
    )

    httpc.send(request)
  }

  case response {
    Ok(response) if response.status == 200 -> io.debug("Give me #prawducks. 😃")
    _ -> io.debug("Guess I'll dev on Linux. 😔")
  }
}

Types

Represents errors that can occur during execution attempts.

pub type Error(a) {
  RetriesExhausted(errors: List(a))
  TimeExhausted(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.

  • TimeExhausted(errors: List(a))

    Indicates that the maximum duration for execution has been reached. Contains an ordered list of all errors encountered during the execution

  • UnallowedError(error: a)

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

Configures the retry mode.

pub type Mode {
  MaxAttempts(Int)
  Expiry(Int)
}

Constructors

  • MaxAttempts(Int)

    Specifies the maximum number of attempts to make.

  • Expiry(Int)

    Specifies the maximum duration to make attempts for.

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 ms wait stream with a constant wait time. Ex: 500ms, 500ms, 500ms, …

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

Produces a custom wait stream.

pub fn execute(
  wait_stream wait_stream: Yielder(Int),
  allow allow: fn(a) -> Bool,
  mode mode: Mode,
  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 ms wait stream that increases exponentially for each attempt. time: Ex: 500ms, 1000ms, 2000ms, 4000ms, …

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

Produces a ms wait stream that increases linearly for each attempt. Ex: 500ms, 1000ms, 1500ms, …

pub fn no_backoff() -> Yielder(Int)

Produces a 0ms wait stream. Ex: 0ms, 0ms, 0ms, …

Search Document