### Poolboy Example Application Module (Erlang) Source: https://github.com/devinus/poolboy/blob/master/README.md This Erlang module (`example.erl`) defines the main application logic, acting as both an application and supervisor behavior. It handles application startup, shutdown, and initializes Poolboy pools based on environment variables. It also provides functions (`squery`, `equery`) to interact with workers via `poolboy:transaction`. ```Erlang -module(example). -behaviour(application). -behaviour(supervisor). -export([start/0, stop/0, squery/2, equery/3]). -export([start/2, stop/1]). -export([init/1]). start() -> application:start(?MODULE). stop() -> application:stop(?MODULE). start(_Type, _Args) -> supervisor:start_link({local, example_sup}, ?MODULE, []). stop(_State) -> ok. init([]) -> {ok, Pools} = application:get_env(example, pools), PoolSpecs = lists:map(fun({Name, SizeArgs, WorkerArgs}) -> PoolArgs = [{name, {local, Name}}, {worker_module, example_worker}] ++ SizeArgs, poolboy:child_spec(Name, PoolArgs, WorkerArgs) end, Pools), {ok, {{one_for_one, 10, 10}, PoolSpecs}}. squery(PoolName, Sql) -> poolboy:transaction(PoolName, fun(Worker) -> gen_server:call(Worker, {squery, Sql}) end). equery(PoolName, Stmt, Params) -> poolboy:transaction(PoolName, fun(Worker) -> gen_server:call(Worker, {equery, Stmt, Params}) end). ``` -------------------------------- ### Poolboy Example Worker Module (Erlang) Source: https://github.com/devinus/poolboy/blob/master/README.md This Erlang module (`example_worker.erl`) implements the `gen_server` and `poolboy_worker` behaviors, serving as the actual worker process for the Poolboy pools. It handles connection establishment (e.g., to a PostgreSQL database using `epgsql`), processes incoming calls (`squery`, `equery`), and manages the worker's state and termination. ```Erlang -module(example_worker). -behaviour(gen_server). -behaviour(poolboy_worker). -export([start_link/1]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -record(state, {conn}). start_link(Args) -> gen_server:start_link(?MODULE, Args, []). init(Args) -> process_flag(trap_exit, true), Hostname = proplists:get_value(hostname, Args), Database = proplists:get_value(database, Args), Username = proplists:get_value(username, Args), Password = proplists:get_value(password, Args), {ok, Conn} = epgsql:connect(Hostname, Username, Password, [ {database, Database} ]), {ok, #state{conn=Conn}}. handle_call({squery, Sql}, _From, #state{conn=Conn}=State) -> {reply, epgsql:squery(Conn, Sql), State}; handle_call({equery, Stmt, Params}, _From, #state{conn=Conn}=State) -> {reply, epgsql:equery(Conn, Stmt, Params), State}; handle_call(_Request, _From, State) -> {reply, ok, State}. handle_cast(_Msg, State) -> {noreply, State}. handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, #state{conn=Conn}) -> ok = epgsql:close(Conn), ok. code_change(_OldVsn, State, _Extra) -> {ok, State}. ``` -------------------------------- ### Poolboy Application Configuration (Erlang) Source: https://github.com/devinus/poolboy/blob/master/README.md This Erlang application configuration file (`example.app`) defines the structure and dependencies for an application using Poolboy. It specifies multiple named pools (`pool1`, `pool2`) with their respective sizes, overflow limits, and worker-specific arguments for database connections. ```Erlang {application, example, [ {description, "An example application"}, {vsn, "0.1"}, {applications, [kernel, stdlib, sasl, crypto, ssl]}, {modules, [example, example_worker]}, {registered, [example]}, {mod, {example, []}}, {env, [ {pools, [ {pool1, [ {size, 10}, {max_overflow, 20} ], [ {hostname, "127.0.0.1"}, {database, "db1"}, {username, "db1"}, {password, "abc123"} ]}, {pool2, [ {size, 5}, {max_overflow, 10} ], [ {hostname, "127.0.0.1"}, {database, "db2"}, {username, "db2"}, {password, "abc123"} ]} ]} ]} ]}. ``` -------------------------------- ### Basic Poolboy Worker Checkout and Checkin (Erlang Shell) Source: https://github.com/devinus/poolboy/blob/master/README.md This snippet demonstrates the fundamental operations of checking out a worker from a named pool, performing an action with it (e.g., a gen_server:call), and then checking the worker back into the pool. It illustrates the basic lifecycle of a worker within Poolboy. ```erl-sh 1> Worker = poolboy:checkout(PoolName). <0.9001.0> 2> gen_server:call(Worker, Request). ok 3> poolboy:checkin(PoolName, Worker). ok ``` -------------------------------- ### Poolboy Configuration Options Source: https://github.com/devinus/poolboy/blob/master/README.md This section details the available configuration options for Poolboy pools. These options control aspects such as the pool's name, the worker module to be used, the maximum number of workers, overflow capacity, and the strategy for managing checked-in workers (LIFO or FIFO). ```APIDOC Poolboy Options: name: string - The pool name. worker_module: module - The module that represents the workers. size: integer - Maximum pool size. max_overflow: integer - Maximum number of workers created if pool is empty. strategy: string - 'lifo' or 'fifo', determines whether checked in workers should be placed first or last in the line of available workers. 'lifo' operates like a traditional stack; 'fifo' like a queue. Default is 'lifo'. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.