gleam/retry
retry
executes a fallible operation multiple times. 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
Represents errors that can occur during a retry attempts.
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 an ordered list of all errors encountered during the execution attempts.
-
UnallowedError(error: a)
Indicates that
retry
ran into an error that wasn’t allowed. Contains the specific error that caused the retry to stop. By default, all errors are allowed. Use theallow()
function to specify which errors should trigger a retry.
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 the next 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))
.