Snakepit.Executor (Snakepit v0.8.7)

View Source

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

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)