Poolboy is an Erlang library that provides a mature process-pool implementation for Elixir systems. Add it through Mix, fetch it with Hex, and commit mix.lock for reproducible builds. section 11.2.1
Poolboy coordinates workers through a managed lifecycle:
- Start a pool manager under the supervision tree with a configured worker module and pool size. section 11.2.2
- Check out a worker, perform the operation, and check it in when finished; callers wait when every worker is busy. section 11.2.2
- Recover from failures by returning workers after crashed clients and replacing crashed workers. section 11.2.2
Database operations through a pool
Define a child specification with a locally registered pool, three Todo.DatabaseWorker processes, and the database folder as worker arguments. Wrap store and get operations in :poolboy.transaction, which supplies a worker PID to the operation function. section 11.2.2
Listing 11.5 Starting a Poolboy-powered pool (todo_poolboy/lib/todo/database.ex)
defmodule Todo.Database do
@db_folder "./persist"
def child_spec(_) do
File.mkdir_p!(@db_folder)
:poolboy.child_spec(
__MODULE__, #1
[ #2
name: {:local, __MODULE__}, #2
worker_module: Todo.DatabaseWorker, #2
size: 3 #2
], #2
[@db_folder] #3
)
end
...
end
Inspect the child specification that registers the pool, selects its worker module, sets its size, and supplies startup arguments. section 11.2.2
