glimit

Package Version Hex Docs test

A simple, framework-agnostic rate limiter for Gleam with pluggable storage. ๐Ÿ’ซ

Features

Usage

A very minimalistic example of how to use glimit would be the following snippet:

import glimit

let limiter =
  glimit.new()
  |> glimit.per_second(2)
  |> glimit.identifier(fn(x) { x })
  |> glimit.on_limit_exceeded(fn(_req) { "Too many requests" })

let handler =
  fn(_req) { "Hello, world!" }
  |> glimit.apply(limiter)

handler("๐Ÿš€") // "Hello, world!"
handler("๐Ÿ’ซ") // "Hello, world!"
handler("๐Ÿ’ซ") // "Hello, world!"
handler("๐Ÿ’ซ") // "Too many requests"
handler("๐Ÿš€") // "Hello, world!"
handler("๐Ÿš€") // "Too many requests"

You can also use glimit.build and glimit.hit for direct rate limit checks without wrapping a function:

import glimit

let assert Ok(limiter) =
  glimit.new()
  |> glimit.per_second(10)
  |> glimit.identifier(fn(x) { x })
  |> glimit.on_limit_exceeded(fn(_) { "Stop!" })
  |> glimit.build

case glimit.hit(limiter, "user_123") {
  Ok(Nil) -> // allowed
  Error(glimit.RateLimited) -> // rejected
  Error(_) -> // store unavailable, fails open
}

More practical examples can be found in the examples/ directory, such as Wisp or Mist servers, or a Redis backend.

Pluggable Store Backend

By default, rate limit state is stored in ETS (Erlang Term Storage). For distributed rate limiting across multiple nodes, you can provide a custom Store that persists bucket state in an external service like Redis or Postgres.

All token bucket logic stays in glimit โ€” adapters only implement lock_and_get / set_and_unlock / unlock operations. The glimit/bucket module provides to_pairs/from_pairs helpers for serialization.

See examples/redis/ for a complete Redis adapter using valkyrie.

Performance

Every hit goes through the pluggable Store interface (lock_and_get / set_and_unlock).

Documentation

Further documentation can be found at https://hexdocs.pm/glimit/glimit.html.

Contributing

Contributions like PRโ€™s, bug reports or suggestions are more than welcome! โ™ฅ๏ธ

โœจ Search Document