ExResilience.Retry (ex_resilience v0.4.0)

Copy Markdown View Source

Retry logic with configurable backoff strategies.

Wraps a function call and retries on failure according to the configured strategy. Does not use a GenServer -- retry is stateless per-call.

Options

  • :name -- optional atom used in telemetry metadata. Default :retry.
  • :max_attempts -- total attempts (including the first). Default 3.
  • :backoff -- backoff strategy (:fixed, :linear, :exponential). Default :exponential.
  • :base_delay -- base delay in ms. Default 100.
  • :max_delay -- cap on delay in ms. Default 10_000.
  • :jitter -- jitter setting. Can be true (full jitter, delay randomized in [0, delay]), false (no jitter), or a float between 0.0 and 1.0 for proportional jitter (delay adjusted by +/- delay * fraction). Default true.
  • :retry_on -- 1-arity predicate that returns true if the result should be retried. Default: retries {:error, _} and :error. Takes precedence over :error_classifier when both are provided.
  • :error_classifier -- module implementing ExResilience.ErrorClassifier. When provided (and :retry_on is not), retries results classified as :retriable. Ignored if :retry_on is also set.

Examples

iex> ExResilience.Retry.call(fn -> {:ok, 42} end, max_attempts: 3)
{:ok, 42}

iex> ExResilience.Retry.call(fn -> {:error, :fail} end, max_attempts: 2)
{:error, :fail}

Summary

Functions

Executes fun with retry logic.

Types

option()

@type option() ::
  {:name, atom()}
  | {:max_attempts, pos_integer()}
  | {:backoff, ExResilience.Backoff.strategy()}
  | {:base_delay, ExResilience.Backoff.milliseconds()}
  | {:max_delay, ExResilience.Backoff.milliseconds()}
  | {:jitter, boolean() | float()}
  | {:retry_on, (term() -> boolean())}
  | {:error_classifier, module()}

Functions

call(fun, opts \\ [])

@spec call((-> term()), [option()]) :: term()

Executes fun with retry logic.

Returns the result of the first successful call, or the last result after all attempts are exhausted.