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.

See Token Bucket Algorithm

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

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See 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.

Link to this function

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.

Link to this function

handle_info(msg, state)

Add tokens to the bucket, and schedule the next refill.

Callback implementation for GenServer.init/1.

Link to this function

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)