# `Snakepit.Executor`
[🔗](https://github.com/nshkrdotcom/snakepit/blob/v0.13.0/lib/snakepit/executor.ex#L1)

Execution helpers with retry, circuit breaker, and timeout support.

> #### Legacy Optional Module {: .warning}
>
> `Snakepit` does not call this module internally. It remains available for
> compatibility and may be removed in `v0.16.0` or later.
>
> Prefer explicit use of `Snakepit.RetryPolicy`, `Snakepit.CircuitBreaker`,
> and the internal `TimeoutRunner` module in new code.

Provides various execution strategies for running operations
with fault tolerance.

## Usage

    # Simple execution with retry
    result = Executor.execute_with_retry(
      fn -> risky_operation() end,
      max_attempts: 3,
      backoff_ms: [100, 200, 400]
    )

    # With circuit breaker
    result = Executor.execute_with_circuit_breaker(cb, fn ->
      external_call()
    end)

    # With timeout
    result = Executor.execute_with_timeout(
      fn -> slow_operation() end,
      timeout_ms: 5000
    )

# `execute`

```elixir
@spec execute(
  (-&gt; any()),
  keyword()
) :: any()
```

Executes a function directly.

# `execute_async`

```elixir
@spec execute_async(
  (-&gt; any()),
  keyword()
) :: Task.t()
```

Executes a function asynchronously.

Returns a Task that can be awaited.

# `execute_batch`

```elixir
@spec execute_batch(
  [(-&gt; any())],
  keyword()
) :: [any()]
```

Executes multiple functions in parallel.

Returns results in the same order as the input functions.

## Options

- `:timeout_ms` - Timeout for all operations (default: 30000)
- `:max_concurrency` - Maximum concurrent operations (default: unlimited)

# `execute_with_circuit_breaker`

```elixir
@spec execute_with_circuit_breaker(GenServer.server(), (-&gt; any()), keyword()) :: any()
```

Executes a function through a circuit breaker.

# `execute_with_protection`

```elixir
@spec execute_with_protection(GenServer.server(), (-&gt; any()), keyword()) :: any()
```

Executes with retry and circuit breaker.

Combines retry logic with circuit breaker protection.

# `execute_with_retry`

```elixir
@spec execute_with_retry(
  (-&gt; any()),
  keyword()
) :: any()
```

Executes a function with retry on transient failures.

## Options

- `:max_attempts` - Maximum attempts (default: 3)
- `:backoff_ms` - List of backoff delays (default: [100, 200, 400])
- `:retriable_errors` - Errors to retry (default: [:timeout, :unavailable])
- `:jitter` - Add random jitter (default: false)

# `execute_with_timeout`

```elixir
@spec execute_with_timeout(
  (-&gt; any()),
  keyword()
) :: any()
```

Executes a function with a timeout.

Returns `{:error, :timeout}` if the function doesn't complete in time.

## Options

- `:timeout_ms` - Timeout in milliseconds (required)

---

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