Per-IP sliding window rate limiter with Eio support
README.md

rate-limit #

Pure per-client rate limiting for OCaml servers.

Rate_limit is an immutable sliding-window limiter: each call supplies the current time and returns a new state with an admission decision. A rejection carries the delay until the oldest accepted request leaves the window. HTTP servers can use that delay for Retry-After with HTTP 429 (RFC 6585 section 4). The RFC leaves client identity and request counting to the server; this limiter uses the caller's client key and the exact interval (now - window_seconds, now].

The separate rate-limit-eio package exposes Rate_limit_eio. Its check locks the shared state, calls the pure operation, installs the result and returns its decision. The adapter contains no expiration, admission, quota, retry-delay or cleanup policy. It supports fibers and multiple OCaml domains.

rate-limit.token-bucket exposes Token_bucket, an immutable strict token bucket (RFC 3290 Appendix A.4) for a rate and a burst. It is a different algorithm: it admits a cost against refilled credit rather than counting a client's accepted requests over a window.

Installation #

$ opam install rate-limit
$ opam install rate-limit-eio

The core package and its tests require no Eio runtime. Install the adapter when the limiter must be shared concurrently. If the packages are not yet available in the public opam repository, add the overlay:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install rate-limit rate-limit-eio

Sliding windows #

Retain the returned state. Earlier snapshots remain unchanged:

let initial = Rate_limit.v ~max_requests:1 ~window_seconds:10. ()
let full, first = Rate_limit.check initial ~ip:"client" ~now:0.
let _, blocked = Rate_limit.check full ~ip:"client" ~now:9.
let _, reopened = Rate_limit.check full ~ip:"client" ~now:10.
let () =
  assert (first = Rate_limit.Allowed {remaining = 0});
  assert (blocked = Rate_limit.Rejected {retry_after = 1.});
  assert (reopened = Rate_limit.Allowed {remaining = 0});
  assert (Rate_limit.current_count initial ~ip:"client" ~now:0. = 0)

A state keeps at most N accepted timestamps per client, with N the configured limit. Client lookup uses O(log C) key comparisons, with C retained clients: at most O(B log C) comparison work for B-byte keys. A check also walks at most N timestamps. Periodic cleanup drops expired client windows when a check crosses 300 seconds since the previous cleanup. Retaining old state snapshots also retains their history. The Eio adapter retains only its current state. The token bucket uses constant space per bucket.

Token buckets #

let bucket = Token_bucket.v ~rate:2. ~burst:3 ~now:0.
let allowed =
  let bucket, first = Token_bucket.take bucket ~now:0. ~cost:3 in
  let _, second = Token_bucket.take bucket ~now:0. ~cost:1 in
  first && not second
let () = assert allowed

Verification #

The core tests cover exact expiry, quota, isolated clients, snapshot immutability and cleanup. The fuzz driver compares arbitrary monotonic request histories with an independent accepted-request oracle. The Eio test releases two gated domains together and verifies one shared quota and unique remaining quota values across all admitted calls.

The admission benchmark runs the adapter at 100 and 1000 clients:

$ dune exec bench/admission/measure.exe

Licence #

ISC. See LICENSE.md.