Tink.Retry (Tink v0.1.1)

Copy Markdown View Source

Retry logic with exponential backoff for Tink.

Automatically retries failed requests with configurable retry strategies.

Features

  • Exponential backoff with jitter
  • Configurable max retries
  • Only retries transient errors
  • Telemetry events

Configuration

config :tink,
  max_retries: 3,
  retry_base_delay: 1000,
  retry_max_delay: 30000

Examples

# Retry a function
result = Tink.Retry.with_retry(fn ->
  make_api_call()
end)

# Custom retry options
result = Tink.Retry.with_retry(
  fn -> make_api_call() end,
  max_attempts: 5,
  base_delay: 500
)

Summary

Functions

Calculates delay for a specific retry attempt with exponential backoff and jitter.

Checks if an error should be retried using default logic.

Executes a function with retry logic.

Types

retry_option()

@type retry_option() ::
  {:max_attempts, pos_integer()}
  | {:base_delay, pos_integer()}
  | {:max_delay, pos_integer()}
  | {:jitter_factor, float()}
  | {:retry_fn, (Tink.Error.t() -> boolean())}

retry_options()

@type retry_options() :: [retry_option()]

Functions

calculate_delay(attempt, base_delay, max_delay, jitter_factor)

@spec calculate_delay(pos_integer(), pos_integer(), pos_integer(), float()) ::
  pos_integer()

Calculates delay for a specific retry attempt with exponential backoff and jitter.

Examples

iex> Tink.Retry.calculate_delay(1, 1000, 30000, 0.1)
# Returns value around 1000ms with ±10% jitter

iex> Tink.Retry.calculate_delay(3, 1000, 30000, 0.1)
# Returns value around 4000ms with ±10% jitter

should_retry?(error)

@spec should_retry?(Tink.Error.t() | term()) :: boolean()

Checks if an error should be retried using default logic.

Examples

iex> error = %Tink.Error{type: :network_error, message: ""}
iex> Tink.Retry.should_retry?(error)
true

iex> error = %Tink.Error{type: :validation_error, message: ""}
iex> Tink.Retry.should_retry?(error)
false

with_retry(fun, opts \\ [])

@spec with_retry((-> term()), retry_options()) :: term()

Executes a function with retry logic.

Options

  • :max_attempts - Maximum number of attempts (default: 3)
  • :base_delay - Base delay between retries in ms (default: 1000)
  • :max_delay - Maximum delay between retries in ms (default: 30000)
  • :jitter_factor - Jitter factor for randomization (default: 0.1)
  • :retry_fn - Custom function to determine if error is retryable

Examples

# Simple retry
{:ok, result} = Tink.Retry.with_retry(fn ->
  Tink.Accounts.list(client)
end)

# Custom retry logic
{:ok, result} = Tink.Retry.with_retry(
  fn -> make_request() end,
  max_attempts: 5,
  base_delay: 2000,
  retry_fn: &custom_retry?/1
)