ProtoRune.HTTPClient.Backoff (proto_rune v0.5.2)

Copy Markdown

Pure helpers to compute retry delays for rate limited (HTTP 429) responses.

The delay grows exponentially per attempt and is capped by a maximum. When the server sends a Retry-After header, its value takes precedence over the computed backoff.

Summary

Functions

Computes the delay in milliseconds to wait before the given retry attempt.

Parses a Retry-After header value into milliseconds.

Extracts the Retry-After header of an HTTP response as milliseconds.

Functions

delay(attempt, opts \\ [])

Computes the delay in milliseconds to wait before the given retry attempt.

The first retry attempt is 1. The delay doubles per attempt starting from :base_delay and never exceeds :max_delay. When :retry_after (in milliseconds) is present, it is returned as-is.

Options

  • :base_delay - delay for the first retry, defaults to 500ms
  • :max_delay - ceiling for the computed delay, defaults to 10_000ms
  • :retry_after - delay in milliseconds parsed from a Retry-After header

Examples

iex> Backoff.delay(1, base_delay: 500, max_delay: 10_000)
500

iex> Backoff.delay(3, base_delay: 500, max_delay: 10_000)
2_000

iex> Backoff.delay(1, retry_after: 30_000)
30_000

parse_retry_after(value)

Parses a Retry-After header value into milliseconds.

Only the delta-seconds form is supported. Returns nil for anything else.

retry_after_ms(arg1)

Extracts the Retry-After header of an HTTP response as milliseconds.

Returns nil when the header is absent or cannot be parsed. Handles both map headers (as returned by Req) and lists of header tuples.