Snakepit.RetryPolicy (Snakepit v0.8.7)

View Source

Retry policy with exponential backoff.

Configures retry behavior including max attempts, backoff timing, and which errors are retriable.

Usage

policy = RetryPolicy.new(
  max_attempts: 3,
  backoff_ms: [100, 200, 400],
  jitter: true
)

if RetryPolicy.should_retry?(policy, attempt) do
  delay = RetryPolicy.backoff_for_attempt(policy, attempt)
  Process.sleep(delay)
  # retry...
end

Summary

Functions

Returns the backoff delay for a given attempt.

Creates a new retry policy.

Checks if an error is retriable according to the policy.

Checks if another retry attempt should be made.

Types

t()

@type t() :: %Snakepit.RetryPolicy{
  backoff_ms: [non_neg_integer()],
  backoff_multiplier: float(),
  base_backoff_ms: non_neg_integer(),
  jitter: boolean(),
  jitter_factor: float(),
  max_attempts: pos_integer(),
  max_backoff_ms: non_neg_integer(),
  retriable_errors: [atom()] | :all
}

Functions

backoff_for_attempt(policy, attempt)

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

Returns the backoff delay for a given attempt.

new(opts)

@spec new(keyword()) :: t()

Creates a new retry policy.

Options

  • :max_attempts - Maximum retry attempts (default: 3)
  • :backoff_ms - List of backoff delays in ms (default: [100, 200, 400, 800, 1600])
  • :base_backoff_ms - Base for exponential backoff (default: 100)
  • :backoff_multiplier - Multiplier for exponential backoff (default: 2.0)
  • :max_backoff_ms - Maximum backoff delay (default: 30000)
  • :jitter - Add random jitter to delays (default: false)
  • :jitter_factor - Jitter range as fraction of delay (default: 0.25)
  • :retriable_errors - List of error atoms to retry, or :all (default: common errors)

retry_for_error?(arg1, arg2)

@spec retry_for_error?(t(), {:error, atom()} | term()) :: boolean()

Checks if an error is retriable according to the policy.

should_retry?(retry_policy, attempt)

@spec should_retry?(t(), non_neg_integer()) :: boolean()

Checks if another retry attempt should be made.