fast-twitch.middlewares.rate-limit
Complete generated reference for fast-twitch.middlewares.rate-limit
Usage and execution boundary
Consumes tokens per key before the handler. Allowed requests receive rate-limit metadata. A rejected request returns 429 with Retry-After by default. Stores are process-local atoms, not a distributed limiter.
(ns application.middleware
(:require [fast-twitch.middlewares.rate-limit :as middleware]))
(defn handler [_]
{:status 200 :headers {"content-type" "text/plain"} :body "Hello"})
(def app (middleware/wrap-rate-limit handler {:requests-per-second 10 :burst 20}))
This defines the Ring handler portion. Adapt app through routing/ft-handler or pass it to server/listen!; startup instructions and a complete walkthrough show the surrounding project.
Settings and defaults
| Setting | Meaning |
|---|---|
:requests-per-second / :rate |
Default 10 |
:burst |
Default ceiling(rate) |
:store |
Atom-backed token map; created for the wrapped handler by default |
:key-fn |
Default real-ip, remote-addr or unknown |
:error-response |
Default 429 with Retry-After 1 |
:error-handler |
Function [request result] |
Wrapper order and all middleware families. The function reference below preserves the upstream signatures and source links; helper functions do not all share the wrapper's arity.
Upstream function reference
Applies token-bucket rate limiting to requests.
client-key
(client-key request)
Function.
Returns the default rate limit key for a request. Source
default-error-response
The default response returned when a client exceeds its rate limit. Source
memory-store
(memory-store)
Function.
Creates an atom-backed in-memory rate limit store. Source
rate-limit-request
(rate-limit-request request result)
Function.
Associates rate limit metadata with a request. Source
rate-limit-response
(rate-limit-response request result options)
Function.
Builds the response returned when a request exceeds its rate limit. Source
rate-limit-result
(rate-limit-result store key rate burst)
Function.
Returns a map describing whether the request is allowed by the rate limit. Source
wrap-rate-limit
(wrap-rate-limit handler)
(wrap-rate-limit handler options)
Function.
Wraps a handler with per-key token-bucket rate limiting. Source