/ex_limiter

Rate Limiter written in elixir with configurable backends

Primary LanguageElixirMIT LicenseMIT

ex_limiter

Rate Limiter written in elixir with configurable backends

Implements leaky bucket rate limiting (wiki), which is superior to most naive approaches by handling bursts even around time windows. You can define your own storage backend by implementing the ExLimiter.Storage behaviour, and configuring it with

config :ex_limiter, :storage, MyStorage

If you want to use Memcache as Storage. Add memcachir into your extra_applications in mix.exs

def application do
  [
    extra_applications: [:memcachir]
  ]
end

usage once configured is:

case ExLimiter.consume(bucket, 1, scale: 1000, limit: 5) do
  {:ok, bucket} -> #do some work
  {:error, :rate_limited} -> #fail
end

Additionally, if you want to have multiple rate limiters with diverse backend implementations you can use the ExLimiter.Base macro, like so:

defmodule MyLimiter do
  use ExLimiter.Base, storage: MyStorage
end

ExLimiter.Plug

ExLimiter also ships with a simple plug implementation. Usage is

plug ExLimiter.Plug, scale: 5000, limit: 20

You can also configure how the bucket is inferred from the given conn, how many tokens to consume and what limiter to use.