# v1.6.0 Design: Webmention Rate Limiting > Reconstructed on 2026-11-03 from the implementation; the original v1.6.0 document was lost. Later changes: v2.6.8–v2.7.2 (see CHANGELOG). > The rate limiter itself has not changed since v1.6.0; v2.10.0 reused `RateLimiter` for the WebSub hub. Describes `starpunk/rate_limiter.py` and `starpunk/routes/webmention.py` as of v2.10.1. Related: ADR-063, [api-contract.md](api-contract.md). **Note on ADR numbering:** the code cites "ADR-064: Webmention Rate Limiting". In this repository ADR-064 is the OIDC admin login decision (v2.0.0); the webmention rate-limiting ADR from v1.6.0 was lost with the other v1.6.0 documents and its number was reused. This document is the surviving description of that decision. ## Goal Stop `POST /webmention` from being used to fill the database or the validation queue (each accepted webmention costs one outbound fetch). ## Algorithm `RateLimiter(limit, window_seconds)`: sliding-window log, in memory. - Per key, a list of request timestamps (`time.time()`), guarded by one `threading.Lock`. - `is_allowed(key)`: drop timestamps older than the window; if the count is `>= limit` refuse (not recorded); otherwise record and allow. - `get_retry_after(key)`: seconds until the oldest timestamp leaves the window, clamped to 1..window. - `cleanup(max_idle_seconds=300)`: drop keys with no recent requests. - `get_request_count(key)`: current count (tests/logging). In-memory state was chosen to match the other in-process caches (the v1.6.0 code cites the worker's endpoint `_discovery_cache`, removed in v2.7.0); no new table, no Redis. ## Webmention limits Two module-level limiters, created by `init_rate_limiters(app)` in `create_app()`: | Setting | Default | Key | |---|---|---| | `WEBMENTION_RATE_LIMIT_ENABLED` | true | Off: both limiters are `None`, everything allowed | | `WEBMENTION_RATE_LIMIT_IP` | 60 | Client IP | | `WEBMENTION_RATE_LIMIT_DOMAIN` | 10 | Host (`netloc`) of the `source` URL | | `WEBMENTION_RATE_LIMIT_WINDOW` | 60 s | Both | `check_rate_limit(ip, source_domain)` checks the IP first, then the source domain (if one could be parsed). Only POST is limited, in the webmention blueprint's `before_request`, before any validation, reading `source` from the form or JSON body. Client IP (`get_client_ip`): the first `X-Forwarded-For` entry when `TRUSTED_PROXY=true`, else `request.remote_addr`. Behind Caddy without `TRUSTED_PROXY` every client shares the proxy's address (documented in `.env.example` since v2.10.1). ## Response ``` HTTP/1.1 429 Retry-After: {"error": "rate_limit_exceeded", "error_description": "Too many requests from your IP. Please retry after N seconds."} ``` The description carries the computed wait; the `Retry-After` header carries the full window. ## Other users `routes/websub.py` (v2.10.0) has its own `RateLimiter(30, 60)` per client IP for subscription requests, calling `cleanup()` every 500 requests. ## Known limits - Counters are per gunicorn process, so the effective limit is up to limit × processes, and they reset on restart. - `cleanup_rate_limiters()` exists but nothing calls it, so the webmention limiters never drop a key (old timestamps are trimmed when the key is checked again, but the key stays); memory grows slowly with distinct client IPs and source domains until the process restarts. - Requests refused by the IP limit are not recorded, and a request refused by the domain limit has already been counted against the IP.