Packages
snakepit
0.8.5
0.13.0
0.12.0
0.11.1
0.11.0
0.10.1
0.10.0
0.9.1
0.9.0
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.11
0.6.10
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
High-performance pooler and session manager for external language integrations. Supports Python, Node.js, Ruby, and more with gRPC streaming, session management, and production-ready process cleanup.
Current section
Files
Jump to
Current section
Files
lib/snakepit/worker_profile.ex
defmodule Snakepit.WorkerProfile do
@moduledoc """
Behaviour for worker profiles (process vs thread).
A worker profile defines how workers are created, managed, and utilized.
Snakepit v0.6.0 introduces dual-mode parallelism:
## Process Profile (`:process`)
- Many single-threaded Python processes
- Process isolation and GIL compatibility
- Optimal for: I/O-bound workloads, high concurrency, legacy Python
## Thread Profile (`:thread`)
- Few multi-threaded Python processes
- Shared memory and CPU parallelism
- Optimal for: CPU-bound workloads, Python 3.13+, large data
## Implementing a Profile
Profiles control the full worker lifecycle:
defmodule MyProfile do
@behaviour Snakepit.WorkerProfile
def start_worker(config) do
# Start worker according to profile
{:ok, worker_handle}
end
def get_capacity(worker_handle) do
# Return concurrent request capacity
1 # or N for multi-threaded
end
end
See `Snakepit.WorkerProfile.Process` and `Snakepit.WorkerProfile.Thread` for reference implementations.
"""
@type worker_handle :: pid() | reference()
@type config :: map()
@type capacity :: pos_integer()
@doc """
Start a worker with the given configuration.
Returns `{:ok, worker_handle}` where worker_handle is typically a GenServer PID,
or `{:error, reason}` if startup fails.
The config map contains all pool and adapter configuration for this worker.
"""
@callback start_worker(config) :: {:ok, worker_handle} | {:error, term()}
@doc """
Stop a worker gracefully.
Should perform cleanup and shutdown the worker process.
"""
@callback stop_worker(worker_handle) :: :ok
@doc """
Execute a request on a worker.
For process-based workers, this typically blocks until the request completes.
For thread-based workers, this may execute concurrently with other requests
on the same worker.
The timeout is in milliseconds.
"""
@callback execute_request(worker_handle, request :: map(), timeout :: timeout()) ::
{:ok, term()} | {:error, term()}
@doc """
Get the maximum capacity of a worker (how many concurrent requests it can handle).
- Process profile: returns 1 (single-threaded)
- Thread profile: returns N (thread pool size)
This is used by the pool for load balancing decisions.
"""
@callback get_capacity(worker_handle) :: capacity()
@doc """
Get the current load of a worker (how many requests are currently in-flight).
Returns 0 if no requests are active, up to the worker's capacity.
"""
@callback get_load(worker_handle) :: non_neg_integer()
@doc """
Check if a worker is healthy and responsive.
Returns `:ok` if healthy, `{:error, reason}` if unhealthy.
"""
@callback health_check(worker_handle) :: :ok | {:error, term()}
@doc """
Get profile-specific metadata about a worker.
Optional callback. Returns a map with profile-specific information.
"""
@callback get_metadata(worker_handle) :: {:ok, map()} | {:error, term()}
@optional_callbacks [get_metadata: 1]
end