Skip to content

savonarola/token_bucket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

Sponsored by FunBox

TokenBucket

Rate limiter implementing Token Bucket algorithm https://en.wikipedia.org/wiki/Token_bucket

Installation

The package can be installed by adding token_bucket to your list of dependencies in mix.exs:

def deps do
  [
    {:token_bucket, "~> 0.1.0"}
  ]
end

Usage

    # Bucket size 10000, add 1000 tokens to bucket each second.
    # This allows peaks of load in 10000 tokens (requests) in a moment and still keeps average load
    # below 1000 tokens per second.
    {:ok, pid} = TokenBucket.start_link(10_000, 1_000)

    true = TokenBucket.consume(pid, 1)

    # Equivalently
    true = TokenBucket.consume(pid)

More precise tuning:

    # Bucket size 2, add 1 token to bucket each 100 ms.
    {:ok, pid} = TokenBucket.start_link(2, {1, 100})

    true = TokenBucket.consume(pid)
    true = TokenBucket.consume(pid)
    false = TokenBucket.consume(pid)

    :timer.sleep(120)

    true = TokenBucket.consume(pid)
    false = TokenBucket.consume(pid)

Using arbitrary GenServer options:

    # Register locally as `:my_limiter`.
    {:ok, pid} = TokenBucket.start_link(10_000, 1_000, name: :my_limiter)

LICENSE

This software is licensed under MIT License.

About

Rate limiter implementing Token Bucket algorithm

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages