Buckets.TokenBucket (plug_ratelimit v0.21.37)
A Token Bucket fills with tokens at a regular rate, up until a preset limit. Another process may ask if the bucket is empty or not. Each empty request drains a token from the bucket.
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor.
Decrement n, minimum value is zero.
Returns true if the bucket is empty, otherwise false. Removes a token from the bucket after the test.
Each call to this function removes a token from the bucket. Returns true if the bucket is not empty before the call is made, otherwise false if empty.
Add tokens to the bucket, and schedule the next refill.
Callback implementation for GenServer.init/1
.
Create a Token Bucket process that allows 10 requests per second
Link to this section Functions
child_spec(init_arg)
Returns a specification to start this module under a supervisor.
See Supervisor
.
dec_to_zero(n)
Decrement n, minimum value is zero.
empty?(pid)
Returns true if the bucket is empty, otherwise false. Removes a token from the bucket after the test.
handle_call(msg, from, state)
Each call to this function removes a token from the bucket. Returns true if the bucket is not empty before the call is made, otherwise false if empty.
handle_info(msg, state)
Add tokens to the bucket, and schedule the next refill.
init(list)
Callback implementation for GenServer.init/1
.
start(args, opts \\ [])
Create a Token Bucket process that allows 10 requests per second:
{:ok, pid} = Buckets.TokenBucket.start([requests_per_second: 10])
Buckets.TokenBucket.empty?(pid)