wpool_pool (worker_pool v7.0.0)

View Source

Top supervisor for a worker_pool.

This supervisor supervises wpool_process_sup (which is the worker's supervisor) together with auxiliary servers that help keep the whole pool running and in order.

The strategy of this supervisor must be one_for_all but the intensity and period may be changed from their defaults by the wpool:pool_sup_intensity() and wpool:pool_sup_intensity() options respectively.

Summary

Types

A pool of workers.

API Functions

Adds a callback module. The module must implement the wpool_process_callbacks behaviour.

Picks the worker with the smaller queue of messages.

Calls all workers in the pool in parallel.

Casts a message to all the workers within the given pool.

Picks the first available worker and sends the call to it.. The timeout provided includes the time it takes to get a worker and for it to process the call.

Casts a message to the first available worker.. Since we can wait forever for a wpool:cast to be delivered but we don't want the caller to be blocked, this function just forwards the cast when it gets the worker.

Retrieves the list of worker registered names. This can be useful to manually inspect the workers or do custom work on them.

Picks a worker base on a hash result.. Note that phash2(Term, Range) returns an integer between 0 (inclusive) and Range (non-inclusive), so 1 must be added.

Set next within the worker pool record. Useful when using. a custom strategy function.

Picks the first available worker, if any.

Picks the next worker in a round robin fashion.

Picks a random worker.

Removes a callback module.

Picks the first available worker and sends the call to it.. The timeout provided includes the time it takes to get a worker and for it to process the call.

Picks the first available worker and sends the request to it. The timeout provided considers only the time it takes to get a worker.

Starts a supervisor with several wpool_processes as its children.

Retrieves the pool stats for all pools.

Retrieves a snapshot of the pool stats.

Get values from the worker pool record. Useful when using a custom. strategy function.

Storage Functions

Use this function to get the Worker pool record in a custom worker.

Types

wpool()

-opaque wpool()

A pool of workers.

API Functions

add_callback_module(Pool, Module)

-spec add_callback_module(wpool:name(), module()) -> ok | {error, term()}.

Adds a callback module. The module must implement the wpool_process_callbacks behaviour.

best_worker(Name)

-spec best_worker(wpool:name()) -> atom().

Picks the worker with the smaller queue of messages.

Throws no_workers.

broadcall(Name, Call, Timeout)

-spec broadcall(wpool:name(), term(), timeout()) -> {[Replies :: term()], [Errors :: term()]}.

Calls all workers in the pool in parallel.

Waits for responses in parallel too, and it assumes that if any response times out, all of them did too and therefore exits with reason timeout like a regular gen_server does.

broadcast(Name, Cast)

-spec broadcast(wpool:name(), term()) -> ok.

Casts a message to all the workers within the given pool.

call_available_worker(Name, Call, Timeout)

-spec call_available_worker(wpool:name(), term(), timeout()) -> term().

Picks the first available worker and sends the call to it.. The timeout provided includes the time it takes to get a worker and for it to process the call.

Throws no_workers | timeout.

cast_to_available_worker(Name, Cast)

-spec cast_to_available_worker(wpool:name(), term()) -> ok.

Casts a message to the first available worker.. Since we can wait forever for a wpool:cast to be delivered but we don't want the caller to be blocked, this function just forwards the cast when it gets the worker.

get_workers(Name)

-spec get_workers(wpool:name()) -> [atom()].

Retrieves the list of worker registered names. This can be useful to manually inspect the workers or do custom work on them.

hash_worker(Name, HashKey)

-spec hash_worker(wpool:name(), term()) -> atom().

Picks a worker base on a hash result.. Note that phash2(Term, Range) returns an integer between 0 (inclusive) and Range (non-inclusive), so 1 must be added.

Throws no_workers.

next/2

-spec next(pos_integer(), wpool()) -> wpool().

Set next within the worker pool record. Useful when using. a custom strategy function.

next_available_worker(Name)

-spec next_available_worker(wpool:name()) -> atom().

Picks the first available worker, if any.

Throws no_workers | no_available_workers.

next_worker(Name)

-spec next_worker(wpool:name()) -> atom().

Picks the next worker in a round robin fashion.

Throws no_workers.

random_worker(Name)

-spec random_worker(wpool:name()) -> atom().

Picks a random worker.

Throws no_workers.

remove_callback_module(Pool, Module)

-spec remove_callback_module(wpool:name(), module()) -> ok | {error, term()}.

Removes a callback module.

run_with_available_worker(Name, Run, Timeout)

-spec run_with_available_worker(wpool:name(), wpool:run(Result), timeout()) -> Result.

Picks the first available worker and sends the call to it.. The timeout provided includes the time it takes to get a worker and for it to process the call.

Throws no_workers | timeout.

send_request_available_worker(Name, Call, Timeout)

-spec send_request_available_worker(wpool:name(), term(), timeout()) ->
                                       noproc | timeout | gen_server:request_id().

Picks the first available worker and sends the request to it. The timeout provided considers only the time it takes to get a worker.

Throws no_workers | timeout.

start_link(Name, Options)

-spec start_link(wpool:name(), wpool:options()) -> supervisor:startlink_ret().

Starts a supervisor with several wpool_processes as its children.

stats()

-spec stats() -> [wpool:stats()].

Retrieves the pool stats for all pools.

stats(Name)

-spec stats(wpool:name()) -> wpool:stats().

Retrieves a snapshot of the pool stats.

Throws no_workers.

wpool_get/2

-spec wpool_get(atom(), wpool()) -> term();
               ([atom()], wpool()) -> term().

Get values from the worker pool record. Useful when using a custom. strategy function.

Storage Functions

find_wpool(Name)

-spec find_wpool(atom()) -> undefined | wpool().

Use this function to get the Worker pool record in a custom worker.