Snakepit.Executor (Snakepit v0.13.0)

Copy Markdown View Source

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

Legacy Optional Module

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
)

Summary

Functions

Executes a function directly.

Executes a function asynchronously.

Executes multiple functions in parallel.

Executes a function through a circuit breaker.

Executes with retry and circuit breaker.

Executes a function with retry on transient failures.

Executes a function with a timeout.

Functions

execute(fun, opts \\ [])

@spec execute(
  (-> any()),
  keyword()
) :: any()

Executes a function directly.

execute_async(fun, opts \\ [])

@spec execute_async(
  (-> any()),
  keyword()
) :: Task.t()

Executes a function asynchronously.

Returns a Task that can be awaited.

execute_batch(functions, opts \\ [])

@spec execute_batch(
  [(-> 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(circuit_breaker, fun, opts \\ [])

@spec execute_with_circuit_breaker(GenServer.server(), (-> any()), keyword()) :: any()

Executes a function through a circuit breaker.

execute_with_protection(circuit_breaker, fun, opts \\ [])

@spec execute_with_protection(GenServer.server(), (-> any()), keyword()) :: any()

Executes with retry and circuit breaker.

Combines retry logic with circuit breaker protection.

execute_with_retry(fun, opts \\ [])

@spec execute_with_retry(
  (-> 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(fun, opts)

@spec execute_with_timeout(
  (-> 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)