Usage control
Usage control limits how many requests a caller can make in a period of time. A caller that goes over a limit gets 429 Too many requests.
Usage control rules are defined under Rules.UsageControlRules and bound by name with UsageControlRules on GlobalConfig, a server or a route. Each scope accepts a list, so several rules can be bound at once, for example one rule for bursts and another for a longer period.
Rules: UsageControlRules: per-minute: RequestCount: 60 WindowTime: 1m per-hour: RequestCount: 1000 WindowTime: 1h ClientIDRules: by-ip: Modes: [IP] # who is counted: here the caller's IP
ServerConfigs: svc-a: URL: "http://svc-a:9090" ClientIDRule: by-ip UsageControlRules: [per-minute, per-hour]Rule options
Section titled “Rule options”| Key | Required | Description |
|---|---|---|
RequestCount |
yes | Number of requests allowed in the window. Must be greater than zero. |
WindowTime |
yes | Length of the window: a positive whole number followed by a unit. |
BurstValue |
no | How many requests a caller can send back to back. From 1 to RequestCount, default RequestCount. See burst. |
State |
no | Where the counters live: InMemory (default) or Database. See sharing limits between instances. |
TargetDatabase |
with State: Database |
Name of an entry under ExternalDependencies. Not allowed with InMemory. |
Units for WindowTime:
| Unit | Meaning | Example |
|---|---|---|
m |
minutes | 1m, 30m |
h |
hours | 1h, 5h |
w |
weeks | 1w |
Mo |
months (30 days) | 1Mo |
The count is kept per caller, see client ID rules for how callers are identified. A request that carries none of the identifiers its client ID rule asks for is refused with 401 and is not counted. On a route with an authentication method the caller is the authenticated identity, and a request without a credential allowed by AllowAnonymous is counted by client IP (see anonymous requests). Callers must be identifiable for a limit to run: a route with usage control rules needs an authentication method or a client ID rule (there is no default), otherwise the config is rejected. A rule with RequestCount: 20 and WindowTime: 1m allows each caller 20 requests per minute.
BurstValue is the number of requests a caller can send at once. After that the caller gets RequestCount per WindowTime back at a steady pace, so a lower BurstValue smooths out spikes.
Rules: UsageControlRules: smooth: RequestCount: 60 WindowTime: 1m BurstValue: 10With this rule a caller can send 10 requests at once, then one more every second (60 per minute). Without BurstValue the caller could send all 60 at once, and the 61st right after would get 429.
BurstValue must be a whole number from 1 to RequestCount. It is ignored by rules without a WindowTime.
Where rules apply
Section titled “Where rules apply”A request is checked against the rules bound to GlobalConfig, to the server of the route and to the route itself. The request is rejected as soon as one rule is exceeded. Within a scope, rules with the shortest window are checked first.
Usage control mode
Section titled “Usage control mode”UsageControlMode on a server or a route chooses which scopes are checked.
| Mode | Behavior |
|---|---|
Cascade (default) |
Global, server and route rules all apply. The request must pass every one. |
Strict |
The scope stops the cascade, rules of the scopes above it are ignored. |
With Strict:
| Set on | Rules checked |
|---|---|
| a route | only the route’s rules, server and global rules are ignored |
| a server | the global rules are ignored |
GlobalConfig: ClientIDRule: by-ip UsageControlRules: [global-limit]
ServerConfigs: svc-a: URL: "http://svc-a:9090" UsageControlRules: [server-limit]
ServerRoutes: checkout-status: TargetServer: svc-a MatchOptions: PathPattern: /api/status/checkout UsageControlMode: Strict # only this route's rules apply here UsageControlRules: [strict-2-per-min]
checkout: TargetServer: svc-a MatchOptions: PathPattern: /api/checkout # Cascade: global-limit + server-limitSharing limits between instances
Section titled “Sharing limits between instances”By default each gateway instance counts on its own and forgets everything when it restarts or reloads its config. With State: Database the counters live in a Redis-protocol server (Redis, Valkey, KeyDB, Dragonfly, ElastiCache, …), so every instance pointing at it shares the same limit and the counts survive a restart. This feature is in beta.
ExternalDependencies: shared-redis: Type: Database SubType: RedisLike Addresses: ["redis-1:6379"] Auth: Username: env:REDIS_USER # optional, omit for password only Password: file:redis_pass # env: or file: references, never inline TLS: # presence enables TLS CACertFile: redis-ca.pem ServerName: redis.internal ClientCertFile: redis.crt # with ClientKeyFile: mTLS ClientKeyFile: redis.key TimeoutMs: 20 FailMode: closed
Rules: UsageControlRules: login-limit: RequestCount: 60 WindowTime: 1m State: Database TargetDatabase: shared-redis| Key | Required | Description |
|---|---|---|
Type |
yes | Database. |
SubType |
yes | RedisLike. |
Addresses |
yes | host:port list. Only the first is used for now (single node). Sentinel and Cluster are not supported yet. |
Auth.Username / Auth.Password |
no | env:NAME reads an environment variable, file:name reads a file inside /etc/nocleye-beta/gateway/auths/. Inline values are rejected. Username needs a Password. Omit Auth for a server without authentication. |
TLS |
no | Its presence enables TLS. CACertFile, ClientCertFile and ClientKeyFile are file names inside /etc/nocleye-beta/gateway/certs/. Setting both client files is mTLS. InsecureSkipVerify: true disables verification and is a warning. |
TimeoutMs |
no | Time limit of each call to the server. Default 20. |
FailMode |
no | What happens when the server errors or times out: closed (default) rejects the request, open lets it through. |
The gateway checks the connection (PING) when it loads the config: an unreachable server, a wrong password or an unreadable certificate makes the load fail, and the previous config keeps serving.
When the database is down
Section titled “When the database is down”With FailMode: closed every request that hits a database backed rule is rejected with 429 while the server cannot answer. With FailMode: open they are let through and the limit is not enforced, which nocleye check reports as a warning. After a few consecutive failures the gateway stops calling the server for a short time, so an outage does not add the timeout to every request. Failures are counted in gw_dependency_errors_total, and the access log line of each affected request has dependency_state set to error or breaker_open. Note that with FailMode: closed the answer is still 429, so use dependency_state to tell a database failure from a caller who went over the limit.
Beta behavior
Section titled “Beta behavior”- Keys are stored as
nocleye:gw-<gateway version>:<kind>:<rule>:<caller>. The prefix is not configurable and includes the gateway version: different versions never share counters, so limits start from zero after an upgrade. This is expected to change after the beta.
Response
Section titled “Response”A rejected request gets status 429 with the body Too many requests. It appears in the access log with response_status 429, and counts in gw_requests_total with reason="rate_limited" (or quota_exceeded) when Prometheus is enabled.
The access log line says which limit refused the request: limit_reason (rate_limited or quota_exceeded), limit_rule (the rule name) and limit_scope (global, server or route). With Prometheus, gw_limit_rejections_total counts the same refusals per rule, scope and kind. See Logs and metrics.
Validation
Section titled “Validation”RequestCountof zero or less is an error.- A
BurstValuebelow1or aboveRequestCountis an error. - A
WindowTimethat is not a positive whole number followed bym,h,worMois an error. - Binding a usage control rule that does not exist is an error.
- A
Stateother thanInMemoryorDatabaseis an error. TargetDatabaseis required withState: Database, not allowed otherwise, and must name anExternalDependenciesentry withType: DatabaseandSubType: RedisLike.- Having no usage control rules at all is a warning: callers are not limited.
