sbetter (sbroker v1.1.1)
This modules provides utility functions for load balancing using the best of two random choices. It is designed for use with sbroker
and sregulator
processes using the sbetter_meter
meter. However any OTP process can use this module to do load balancing using the via
naming format if the process is registered with and updates the sbetter_server
.
To use sbetter
with via
use names of the form {via, sbetter, {{Broker, ...}, ask | ask_r}}
. Where {Broker, ...}
is a tuple containing pid() | atom() | {global, any()} | {via, module(), any()} | {atom(), node()}
. The process with the small value/shortest sojourn time of two random processes for the ask
(or ask_r
) queue will be called. The sojourn time includes the message queue delay and the time spent waiting in the internal queue.
Comparing values/sojourn times requires ets
lookups. However it is not required to carry out the lookups for every request to get well balanced queues. To only compare two random choices 20% of the time and use sscheduler
the remaining 80% use scheduler_ask
and scheduler_ask_r
, or to only compare two random choices 20% of the time and choose a random process the reamining 80% use rand_ask
and rand_ask_r
. This ratio is chosen as the majority of the gain in choosing two random choices can be captured by giving 20% of requests a choice. See section 4.5 of the reference for more information.
It is not possible to locally look up the pid of a process with name {atom(), node()}
if the node is not the local node. Therefore a registered name on another node is not supported for use with this module.
If a chosen process is not local the call may exit with {badnode, node()}
.
sbetter_server
the call may exit with {nobetter, pid()}
. The sbetter_meter
will register with the server. However other methods can be used to register and update the sbetter_server
. Registering with the sbetter_server
must be done with sbetter_server:register/3
and not using start_link({via, sbetter, ...}, ...)
.See also: sbetter_meter, sbetter_server.
Link to this section Summary
Functions
ok
if a process could be chosen otherwise exits.undefined
.Link to this section Types
method/0
Specs
method() :: ask | ask_r | scheduler_ask | scheduler_ask_r | rand_ask | rand_ask_r.
Link to this section Functions
send(Name, Msg)
Specs
send({Processes, Method}, Msg) -> ok when Processes :: tuple(), Method :: method(), Msg :: any().
ok
if a process could be chosen otherwise exits.
whereis_name(_)
Specs
whereis_name({Processes, Method}) -> Process | undefined when Processes :: tuple(), Method :: method(), Process :: pid().
undefined
.