Raxol.Agent.Policy.Retry (Raxol Agent v2.6.0)

Copy Markdown View Source

Retry policy: wrap an operation in a bounded-attempt loop with configurable backoff and error-matching.

Three constructors:

  • exponential/1 -- exponential backoff (base_ms * 2^(attempt - 1), capped at max_ms)
  • linear/1 -- fixed base_ms between attempts
  • always/1 -- exponential backoff, retry on any {:error, _}

Fields

  • max_attempts -- total attempts including the first; must be >= 1
  • base_ms -- base delay in ms between attempts
  • max_ms -- cap on the computed backoff (base_ms * 2^(attempt - 1) for exponential; ignored for linear); default :infinity
  • mode -- :exponential | :linear; affects backoff computation

  • on -- error matcher:
    • :any -- retry on any {:error, _} (the default for always/1)
    • list of atoms -- retry on {:error, atom} when atom is in the list
    • list of {Module, atom} tuples -- retry on {:error, {Module, atom}}
    • a (reason -> boolean) predicate -- arbitrary matching

Summary

Functions

Construct an "always retry" policy: exponential backoff with :on set to :any.

Backoff in ms before the given attempt (1-indexed: backoff/1 is the delay before the second attempt).

Construct an exponential-backoff retry policy.

Construct a linear-backoff retry policy. :base_ms is the delay between attempts (no exponential growth).

Check whether reason matches the policy's :on matcher.

Types

matcher()

@type matcher() :: :any | [atom() | {module(), atom()}] | (term() -> boolean())

mode()

@type mode() :: :exponential | :linear

t()

@type t() :: %Raxol.Agent.Policy.Retry{
  base_ms: non_neg_integer(),
  max_attempts: pos_integer(),
  max_ms: non_neg_integer() | :infinity,
  mode: mode(),
  on: matcher()
}

Functions

always(opts)

@spec always(keyword()) :: t()

Construct an "always retry" policy: exponential backoff with :on set to :any.

backoff(retry, attempt)

@spec backoff(t(), pos_integer()) :: non_neg_integer()

Backoff in ms before the given attempt (1-indexed: backoff/1 is the delay before the second attempt).

exponential(opts)

@spec exponential(keyword()) :: t()

Construct an exponential-backoff retry policy.

Required: :max_attempts, :base_ms. Optional: :max_ms, :on.

linear(opts)

@spec linear(keyword()) :: t()

Construct a linear-backoff retry policy. :base_ms is the delay between attempts (no exponential growth).

retriable?(retry, reason)

@spec retriable?(t(), term()) :: boolean()

Check whether reason matches the policy's :on matcher.