Karta. Limits
(Karta v1.0.0)
The limits that hold across every node: at most limit events per
window milliseconds for a key, counted in Postgres.
Karta runs on several nodes against one database. A limit counted in
memory gives each node its own count, so N nodes allow N times the limit
(Karta.RateLimiter keeps the counters that are meant to be per node).
These counters are rows of rate_limits, one per kind of limit, key and
fixed window, moved forward with one statement:
INSERT INTO rate_limits ... ON CONFLICT (kind, key, window_ms, window_index)
DO UPDATE SET count = rate_limits.count + 1 RETURNING countso two nodes incrementing together get different counts, and a refused event is decremented again in the same call and is not counted.
The window slides as Karta.RateLimiter's does: the previous window
counts for the part of it that still lies inside the sliding window
(previous * (1 - elapsed / window) + current). A row is kept until the
window after its own has ended, because the window after it weighs it.
A counter that cannot be read refuses. When the database answers with
a connection error or a query error, hit/5 returns {:error, window}:
no caller lets an event through because its limit could not be read.
Keys. A key is a credential id, a user id, a client address (an IPv4
address or an IPv6 /64, formatted), or account_key/1 of a login
address. A login address is never written: its attempts are counted under
the hash account_key/1 computes. Callers pass ids and addresses, never
free text.
Bounded. Every row expires (expires_at). Karta.Limits.Cleaner
deletes expired rows every 30 seconds and, when the table holds more than
:max_rows rows, removes the least-hit rows first, then those closest to
expiry, as Karta.DNS.Cache does with its answers. No row outlives its
window, so the table holds live counters only.
Summary
Functions
The key a login address's attempts are counted under: the SHA-256 of
"karta login account\n" and the normalised address, as lower-case hex.
The address itself is never written to the counter table.
Deletes the rows whose window has ended, then the least-hit rows while the
table holds more than :max_rows (default 1,000,000), then the rows
closest to expiry among them. Returns how many rows each step removed.
Counts an event of kind for key: :ok, or {:error, retry_after_ms}
when the limit is reached or the counter cannot be read.
Functions
The key a login address's attempts are counted under: the SHA-256 of
"karta login account\n" and the normalised address, as lower-case hex.
The address itself is never written to the counter table.
@spec expire(DateTime.t(), keyword()) :: %{ expired: non_neg_integer(), evicted: non_neg_integer() }
Deletes the rows whose window has ended, then the least-hit rows while the
table holds more than :max_rows (default 1,000,000), then the rows
closest to expiry among them. Returns how many rows each step removed.
@spec hit(atom(), term(), pos_integer(), pos_integer(), keyword()) :: :ok | {:error, pos_integer()}
Counts an event of kind for key: :ok, or {:error, retry_after_ms}
when the limit is reached or the counter cannot be read.
kind names the limit (:credential, :login_client,
:login_account, :totp, :password_check, :api_key_verification).
key is an id, an address tuple, an integer, or an account_key/1
hash. Options: now (a DateTime, default the current time), repo
(a module with query!/2, default Karta.Repo) and weight (default 1:
what the event counts for, added or refused as a whole, in one statement).