Worker Pool Build Statuscodecov

A pool of gen servers.

Abstract

The goal of worker pool is pretty straightforward: To provide a transparent way to manage a pool of workers and do the best effort in balancing the load among them distributing the tasks requested to the pool.

Documentation

The documentation can be generated from code using rebar3_ex_doc with rebar3 ex_doc. It is also available online here

Usage

All user functions are exposed through the wpool module.

Starting the Application

Worker Pool is an erlang application that can be started using the functions in the application module. For convenience, wpool:start/0 and wpool:stop/0 are also provided.

Starting a Pool

To start a new worker pool, you can either use wpool:start_pool (if you want to supervise it yourself) or wpool:start_sup_pool (if you want the pool to live under wpool's supervision tree). You can provide several options on any of those calls:

Using the Workers

Since the workers are gen_servers, messages can be called or casted to them. To do that you can use wpool:call and wpool:cast as you would use the equivalent functions on gen_server.

Choosing a Strategy

Beyond the regular parameters for gen_server, wpool also provides an extra optional parameter: Strategy. The strategy used to pick up the worker to perform the task. If not provided, the result of wpool:default_strategy/0 is used. The available strategies are defined in the t:wpool:strategy/0 type and also described below:

best_worker

Picks the worker with the smaller queue of messages. Loosely based on this article. This strategy is usually useful when your workers always perform the same task, or tasks with expectedly similar runtimes.

random_worker

Just picks a random worker. This strategy is the fastest one when to select a worker. It's ideal if your workers will perform many short tasks.

next_worker

Picks the next worker in a round-robin fashion. That ensures evenly distribution of tasks.

available_worker

Instead of just picking one of the workers in the queue and sending the request to it, this strategy queues the request and waits until a worker is available to perform it. That may render the worker selection part of the process much slower (thus generating the need for an additional parameter: Worker_Timeout that controls how many milliseconds is the client willing to spend in that, regardless of the global Timeout for the call). This strategy ensures that, if a worker crashes, no messages are lost in its message queue. It also ensures that, if a task takes too long, that doesn't block other tasks since, as soon as other worker is free it can pick up the next task in the list.

next_available_worker

In a way, this strategy behaves like available_worker in the sense that it will pick the first worker that it can find which is not running any task at the moment, but the difference is that it will fail if all workers are busy.

hash_worker

This strategy takes a key and selects a worker using erlang:phash2/2. This ensures that tasks classified under the same key will be delivered to the same worker, which is useful to classify events by key and work on them sequentially on the worker, distributing different keys across different workers.

Broadcasting a Pool

Wpool provides a way to broadcast a message to every worker within the given Pool.

1> wpool:start().
ok
2> wpool:start_pool(my_pool, [{workers, 3}]).
{ok,<0.299.0>}
3> wpool:broadcast(my_pool, {io, format, ["I got a message.~n"]}).
I got a message.
I got a message.
I got a message.
ok

NOTE: This messages don't get queued, they go straight to the worker's message queues, so if you're using available_worker strategy to balance the charge and you have some tasks queued up waiting for the next available worker, the broadcast will reach all the workers before the queued up tasks.

Watching a Pool

Wpool provides a way to get live statistics about a pool. To do that, you can use wpool:stats/1.

Stopping a Pool

To stop a pool, just use wpool:stop_pool/1.

Examples

To see how wpool is used you can check the test folder where you'll find many different scenarios exercised in the different suites.

If you want to see worker_pool in a real life project, I recommend you to check sumo_db, another open-source library from Inaka that uses wpool intensively.

Benchmarks

wpool comes with a very basic benchmarker that let's you compare different strategies against the default wpool_worker. If you want to do the same in your project, you can use wpool_bench as a template and replace the worker and the tasks by your own ones.

Contact Us

If you find any bugs or have a problem while using this library, please open an issue in this repo (or a pull request :)).

On Hex.pm

Worker Pool is available on Hex.pm.

Requirements

Required OTP version 25 or higher. We only provide guarantees that the system runs on OTP25+ since that's what we're testing it in, but the minimum_otp_vsn is "21" because some systems where worker_pool is integrated do require it.