ex_limiter v1.3.1 ExLimiter.Plug View Source

Plug for enforcing rate limits. The usage should be something like

plug ExLimiter.Plug, scale: 1000, limit: 5

Additionally, you can pass the following options:

  • :bucket, a 1-arity function of a Plug.Conn.t which determines the bucket for the rate limit. Defaults to the phoenix controller, action and remote_ip.

  • :consumes, a 1-arity function of a Plug.Conn.t which determines the amount to consume. Defaults to 1 respectively.

  • :decorate, a 2-arity function which can return an updated conn based on the outcome of the limiter call. The first argument is the Plug.Conn.t, and the second can be:

    • {:ok, Bucket.t}
    • {:rate_limited, binary} Where the second element is the bucket name that triggered the rate limit.

Additionally, you can configure a custom limiter with

config :ex_limiter, ExLimiter.Plug, limiter: MyLimiter

and you can also configure the rate limited response with

config :ex_limiter, ExLimiter.Plug, fallback: MyFallback

MyFallback needs to implement a function render_error(conn, :rate_limited)

Link to this section Summary

Link to this section Functions

Link to this function

decorate(conn, _) View Source
decorate(
  Plug.Conn.t(),
  {:ok, Bucket.t()} | {:rate_limited, bucket_name :: binary()}
) :: Plug.Conn.t()

Link to this function

render_error(conn, atom) View Source