# `Tink.Retry`
[🔗](https://github.com/iamkanishka/tink.ex/blob/v0.1.1/lib/tink/retry.ex#L1)

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
    )

# `retry_option`

```elixir
@type retry_option() ::
  {:max_attempts, pos_integer()}
  | {:base_delay, pos_integer()}
  | {:max_delay, pos_integer()}
  | {:jitter_factor, float()}
  | {:retry_fn, (Tink.Error.t() -&gt; boolean())}
```

# `retry_options`

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

# `calculate_delay`

```elixir
@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?`

```elixir
@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`

```elixir
@spec with_retry((-&gt; 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
    )

---

*Consult [api-reference.md](api-reference.md) for complete listing*
