View Source Getting Started

Starting pool of workers

To start a pool you can use either start/1 or start_link/1.

Poolex.start_link(pool_id: :my_pool, worker_module: SomeWorker, workers_count: 10)

In general, you should place it into your Supervision tree for fault tolerance.

children = [
  {Poolex,
  pool_id: :my_pool,
  worker_module: SomeWorker,
  workers_count: 10,
  max_overflow: 10}
]

Supervisor.start_link(children, strategy: :one_for_one)

The second argument should contain a set of options for starting the pool.

Poolex configuration options

OptionDescriptionExampleDefault value
pool_idIdentifier by which you will access the pool:my_pooloption is required
worker_moduleName of module that implements our workerMyApp.Workeroption is required
workers_countHow many workers should be running in the pool5option is required
max_overflowHow many workers can be created over the limit20
worker_argsList of arguments passed to the start function[:gg, "wp"][]
worker_start_funName of the function that starts the worker:run:start_link
busy_workers_implModule that describes how to work with busy workersSomeBusyWorkersImplPoolex.Workers.Impl.List
idle_workers_implModule that describes how to work with idle workersSomeIdleWorkersImplPoolex.Workers.Impl.List
waiting_callers_implModule that describes how to work with callers queueWaitingCallersImplPoolex.Callers.Impl.ErlangQueue
pool_size_metricsWhether to dispatch pool size metricstruefalse

Working with the pool

After the pool is initialized, you can get a free worker and perform any operations on it. This is done through the main interface run/3.

The first argument is the name of the pool mentioned above.

The second argument is the function that takes the pid of the worker as the only parameter and performs the necessary actions.

The third argument contains run options. Currently, there is only one checkout_timeout option that tells Poolex how long we can wait for a worker on the call site.

iex> Poolex.start_link(pool_id: :agent_pool, worker_module: Agent, worker_args: [fn -> 5 end], workers_count: 1)
iex> Poolex.run(:agent_pool, fn pid -> Agent.get(pid, &(&1)) end)
{:ok, 5}

If you would like to see examples of using Poolex, then check out Example of Use.