Pristine.Adapters.Retry.Foundation (Pristine v0.4.0)

Copy Markdown View Source

Retry adapter backed by Foundation retry policies.

This adapter provides:

  • Full retry orchestration via with_retry/2
  • Opt-in :retry_budget_ms stops before a delay would reach the total call budget and returns the last result, including time spent in the initial attempt.
  • HTTP-specific retry determination via should_retry?/1
  • Retry-After header parsing via parse_retry_after/1
  • Optional Pristine.Cancellation integration that interrupts the default retry wait and prevents later attempts once cancellation is terminal.

When a caller supplies a custom :sleep_fun, Pristine preserves that hook. Cancellation is checked immediately before and after the custom sleeper; a sleeper that blocks internally must cooperate with the same cancellation token if it needs mid-sleep interruption.

Summary

Functions

Create a retry policy with HTTP-aware retry-after support.

Parse retry delay from HTTP response headers.

Determine if an HTTP response should be retried.

Functions

http_aware_policy(opts \\ [])

@spec http_aware_policy(keyword()) :: Foundation.Retry.Policy.t()

Create a retry policy with HTTP-aware retry-after support.

This function creates a retry policy that will respect Retry-After headers from HTTP responses. Use this when you need the retry delays to be driven by server-specified delays.

Options

All standard Foundation.Retry.Policy options, plus:

  • :response_headers_fun - Function to extract headers from the result

Examples

policy = Foundation.http_aware_policy(
  max_attempts: 5,
  response_headers_fun: fn
    {:ok, %{headers: headers}} -> headers
    _ -> %{}
  end
)

parse_retry_after(response_or_headers, opts \\ [])

Parse retry delay from HTTP response headers.

Delegates to Foundation.Retry.HTTP.parse_retry_after/1.

Examples

iex> Pristine.Adapters.Retry.Foundation.parse_retry_after(%{"retry-after" => "5"})
5000

should_retry?(response)

Determine if an HTTP response should be retried.

Delegates to Foundation.Retry.HTTP.should_retry?/1.

Examples

iex> Pristine.Adapters.Retry.Foundation.should_retry?(%{status: 429})
true

iex> Pristine.Adapters.Retry.Foundation.should_retry?(%{
...>   status: 400,
...>   headers: %{"x-should-retry" => "true"}
...> })
true