View Source Getting Started

starting-pool-of-workers

Starting pool of workers

To start a cache you can use either start/2 or start_link/2. The first argument is the name of the pool and defines how you will communicate with it.

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

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

pool_config = [
  worker_module: SomeWorker,
  workers_count: 10,
  max_overflow: 10
]

children = [
  %{
    id: :my_pool,
    start: {Poolex, :start_link, [:my_pool, pool_config]}
  }
]

Supervisor.start_link(children, strategy: :one_for_one)

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

poolex-configuration-options

Poolex configuration options

OptionDescriptionExampleDefault value
worker_moduleName of module that implements our workerMyApp.Workeroption is required
worker_start_funName of the function that starts the worker:run:start
worker_argsList of arguments passed to the start function[:gg, "wp"][]
workers_countHow many workers should be running in the pool5option is required
max_overflowHow many workers can be created over the limit20

working-with-the-pool

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 interfaces run/3 and run!/3. The functions work the same and the only difference between them is that run/3 takes care of the runtime error handling.

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 timeout option that tells to Poolex how long we can to wait for a worker on the call side.

iex> Poolex.start_link(: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}
iex> Poolex.run!(:agent_pool, fn pid -> Agent.get(pid, &(&1)) end)
5

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