Getting Started

View Source

Starting pool of workers

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

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

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

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

Supervisor.start_link(children, strategy: :one_for_one)

Poolex configuration options

OptionDescriptionExampleDefault value
busy_workers_implModule that describes how to work with busy workersSomeBusyWorkersImplPoolex.Workers.Impl.List
failed_workers_retry_intervalInterval in milliseconds between retry attempts for failed workers5_0001_000
idle_workers_implModule that describes how to work with idle workersSomeIdleWorkersImplPoolex.Workers.Impl.List
idle_overflowed_workers_implModule that describes how to work with idle overflowed workersSomeIdleOverflowedWorkersImplPoolex.Workers.Impl.List
max_overflowHow many workers can be created over the limit20
worker_shutdown_delayDelay (ms) before shutting down overflow worker after release50000
pool_idIdentifier by which you will access the pool:my_poolworker_module value
pool_size_metricsWhether to dispatch pool size metricstruefalse
waiting_callers_implModule that describes how to work with callers queueWaitingCallersImplPoolex.Callers.Impl.ErlangQueue
worker_argsList of arguments passed to the start function[:gg, "wp"][]
worker_moduleName of module that implements our workerMyApp.Workeroption is required
worker_start_funName of the function that starts the worker:run:start_link
workers_countHow many workers should be running in the pool5option is required

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 pool name.

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, checkout_timeout: 5_000)
{:ok, 5}

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