poolboy Documentation

repository·master·Indexed 23 days ago

https://github.com/devinus/poolboy

A lightweight, generic Erlang library for managing worker pools, designed for simplicity, performance, and disaster recovery. It provides mechanisms for manual worker checkout/checkin, safe execution via poolboy:transaction/2, and pool configuration using poolboy:child_spec/3 for integration into Erlang supervisors.

Tokens
781
Snippets
1
Records
5
Agent score
32%

What's inside poolboy

  1. Implement a Poolboy worker

    master

    To create a worker compatible with Poolboy, your module should implement the poolboy_worker behavior (or simply be a gen_server that accepts arguments in start_link/1).

    Key requirements:

    1. start_link(Args): Must accept a list of arguments used to initialize the worker (e.g., connection strings).
    2. init(Args): Should use process_flag(trap_exit, true) to ensure the worker can handle supervisor shutdowns gracefully.
    3. Cleanup: Implement terminate/2 to close any resources (like database connections) held by the worker.
    -module(my_worker).
    -behaviour(gen_server).
    -behaviour(poolboy_worker).
    
    start_link(Args) ->
        gen_server:start_link(?MODULE, Args, []).
    
    init(Args) ->
        process_flag(trap_exit, true),
        % ... initialization logic ...
        {ok, #state{...}}.
    
    terminate(_Reason, #state{conn=Conn}) ->
        % ... cleanup logic ...
        ok.
  2. Use Poolboy to checkout and checkin workers manually

    master

    You can manually manage worker lifecycle by checking out a worker from a named pool, performing a task, and then checking the worker back in. This is useful for low-level control over worker usage.

    1. Use poolboy:checkout(PoolName) to get a worker PID.
    2. Use the worker PID (typically via gen_server:call/2) to perform your task.
    3. Use poolboy:checkin(PoolName, Worker) to return the worker to the pool.
  3. Use poolboy:transaction/2 for safe worker execution

    master
    The poolboy:transaction/2 function is a high-level way to execute a function using a worker from the pool. It automatically handles checking the worker out and checking it back in, ensuring the worker is returned to the pool even if the provided function fails or completes. This is the recommended pattern for most use cases to prevent worker leaks.
  4. Configure a Poolboy pool using poolboy:child_spec/3

    master

    To integrate Poolboy into an Erlang application (e.g., within a supervisor's init/1 function), use poolboy:child_spec/3. This function generates a child specification that can be used by a supervisor to manage the pool.

    poolboy:child_spec(Name, PoolArgs, WorkerArgs)

    • Name: The name of the pool.
    • PoolArgs: A list of arguments for the pool configuration (e.g., worker_module, size, max_overflow).
    • WorkerArgs: A list of arguments passed to the worker's start_link/1 function during initialization.
  5. Poolboy configuration options

    master

    When defining a pool via poolboy:child_spec/3, you can provide the following configuration options in the PoolArgs list:

    OptionDescription
    nameThe name of the pool (typically {local, Name}).
    worker_moduleThe module that represents the workers.
    sizeThe base/maximum pool size.
    max_overflowThe maximum number of workers created if the pool is empty.
    strategyDetermines whether checked in workers should be placed first or last in the line of available workers. Options: lifo (default, stack-like) or fifo (queue-like).