rays/retry

How long to wait before trying again something that failed, such as a Durable Object alarm registered with durable_object.register_alarm.

import gleam/time/duration
import rays/retry

// 2s, 4s, 8s, ... and then 10 minutes between every attempt.
retry.Exponential(first: duration.seconds(2), max: duration.minutes(10))

// A minute between every attempt.
retry.Fixed(every: duration.minutes(1))

Types

How long to wait between attempts of work that failed.

pub type Backoff {
  Exponential(first: duration.Duration, max: duration.Duration)
  Fixed(every: duration.Duration)
}

Constructors

Values

pub fn delay(
  backoff: Backoff,
  after attempt: Int,
) -> duration.Duration

How long to wait after the given attempt failed, counting the first attempt as 1, before making the next one.

Examples

let backoff =
  retry.Exponential(first: duration.seconds(2), max: duration.seconds(5))
retry.delay(backoff, after: 1)
// -> duration.seconds(2)
retry.delay(backoff, after: 2)
// -> duration.seconds(4)
retry.delay(backoff, after: 3)
// -> duration.seconds(5)
pub fn validate(backoff: Backoff) -> Result(Nil, String)

Check that a backoff’s durations make sense: none is negative, and an Exponential one’s first is above zero and no more than its max.

Returns an error saying what is wrong.

✨ Search Document