TokenBucket (TokenBucket v1.0.0) View Source
TokenBucket is a simple implementation of the Token Bucket Algorithm;
A new bucket is initialized with a cadence and a capacity; in the
background the GenServer populates the bucket with tokens by calling
itself with :tick
based on the cadence
The capacity of the bucket represents the size of the burst; meaning that if the bucket were at capacity this is the number of near parallel token removals that could happen.
If you want a bucket with no burst simply define a capacity of (1)
Link to this section Summary
Functions
Blocking call to remove one token from the bucket. Returns :ok
when the operation completes
Returns a specification to start this module under a supervisor.
Callback implementation for GenServer.init/1
.
Starts a TokenBucket with linking under a supervision tree; the options must include :capacity
and :cadence
; also accepts options for the GenServer
Non-blocking call to remove one token from the bucket. Returns :ok
if the operation were succesful and :empty
if there were no tokens
available
Link to this section Functions
Blocking call to remove one token from the bucket. Returns :ok
when the operation completes
Examples
iex> TokenBucket.await(MyBucket)
:ok
Returns a specification to start this module under a supervisor.
See Supervisor
.
Callback implementation for GenServer.init/1
.
Starts a TokenBucket with linking under a supervision tree; the options must include :capacity
and :cadence
; also accepts options for the GenServer
Non-blocking call to remove one token from the bucket. Returns :ok
if the operation were succesful and :empty
if there were no tokens
available
Examples
iex> TokenBucket.take(MyBucket)
:ok
iex> TokenBucket.take(MyBucket)
:empty