Tinkex.API.Retry (Tinkex v0.3.4)

View Source

Retry logic with exponential backoff for Tinkex API requests.

Implements Python SDK parity for retry behavior:

  • Exponential backoff with jitter
  • Configurable max retries
  • Status-based retry decisions (429, 408, 409, 5xx)
  • Retry-After header support
  • x-should-retry header override

Summary

Functions

Calculates retry delay with exponential backoff and jitter.

Executes a request with automatic retry logic.

Functions

calculate_delay(attempt)

@spec calculate_delay(non_neg_integer()) :: non_neg_integer()

Calculates retry delay with exponential backoff and jitter.

Python parity implementation:

  • Base delay: INITIAL_RETRY_DELAY * 2^attempt
  • Capped at: MAX_RETRY_DELAY
  • Jitter: random value in range [0.75, 1.0]

Examples

iex> delay = calculate_delay(0)
iex> delay >= 375 and delay <= 500
true

iex> delay = calculate_delay(5)
iex> delay >= 7500 and delay <= 10000
true

execute(request, pool, timeout, max_retries, dump_headers?)

@spec execute(Finch.Request.t(), atom(), timeout(), non_neg_integer(), boolean()) ::
  retry_result()

Executes a request with automatic retry logic.

Retries are governed by:

  1. max_retries configuration (no wall-clock timeout)
  2. Status codes (429, 408, 409, 5xx)
  3. Connection errors (transport, HTTP)
  4. x-should-retry header override

Options

  • request - Finch request to execute
  • pool - Connection pool name
  • timeout - Per-request timeout in milliseconds
  • max_retries - Maximum number of retry attempts
  • dump_headers? - Whether to log request details

Returns

{result, retry_count} where result is {:ok, response} or {:error, reason} and retry_count is the number of retries performed.