Rate Limits & Logging
Estimated reading time: 6 minutes 预计阅读时间: 6 分钟Rate limits protect high-traffic endpoints so one user, IP, or caller cannot exhaust service resources in a short time. Logs record key admin actions so login, create/update/delete, resource access, errors, and latency can be audited.
Summerrs Admin provides two groups of capabilities:
Declarative Rate Limits
Use #[rate_limit] on HTTP handlers. It checks the limit before business logic runs. When the request is limited, it returns 429 Too Many Requests; when the Redis backend is unavailable and the policy is fail_closed, it returns 503 Service Unavailable.
The common case is rate limiting by IP or user:
Usage notes:
- Only async free functions are supported.
- Methods with
selfare not supported. - Put
#[rate_limit]outside the route macro, for example above#[get_api].
Integration Prerequisites
The macro depends on summer_common::rate_limit::RateLimitEngine. Before enabling rate limits on business endpoints, provide this component in the app.
Register it through the plugin:
RateLimitPlugin reads summer_redis::Redis from app components, then registers:
For custom routers or tests, you can also inject RateLimitEngine directly as an axum extension or Summer component.
To add response headers automatically, mount:
Parameters
key = "user" prefers the logged-in user ID. When the user is anonymous, it falls back to ip:<client_ip> and does not share buckets with key = "ip". key = "header:X-Tenant-Id" buckets by the header value; missing headers use unknown.
Algorithms
Only token_bucket and gcra support cost-based limits and reservations.
Redis Failure Policies
When backend = "redis", Redis failures are handled by failure_policy:
In multi-instance deployments, fallback_memory only counts within one process, so semantics degrade. External APIs usually prefer fail_open for availability; strict quotas may prefer fail_closed.
Shadow Mode
mode = "shadow" records "this would have been rejected" without actually rejecting the request:
Shadow and Enforce share the same bucket state. If shadow runs for a long time and you switch directly to enforce, requests may be rejected immediately for a while. Call RateLimitEngine::reset_key before switching when needed.
Cost And Reservation
RateLimitContext also supports cost-based quota consumption:
For "reserve first, settle later" workloads, use reserve:
This fits AI tokens, bulk imports, file processing, and similar cost-based tasks. Non-GCRA algorithms treat cost > 1 as one request and warn on first occurrence.
Response Headers
If rate_limit_headers_middleware is mounted, responses automatically include IETF draft-style rate-limit headers:
When limited, the business response uses the unified error format:
Operation Log Macro
System endpoints can use #[log] for operation audit:
The macro injects OperationLogContext and records:
Disable parameter logging for sensitive endpoints:
Disable response logging for large responses or file responses:
Batched Log Writes
The main app registers:
LogBatchCollectorPlugin provides OperationLogCollector and LoginLogCollector. Services do not write to the database synchronously. They try_send into a bounded channel, and a background worker performs batched insert_many when thresholds are reached.
Default config comes from summer-plugins/src/log_batch_collector/config.rs:
When the channel is full or closed, logs are dropped and a warning is recorded. The main request path is not blocked.
Query Logs
System routes provide log queries:
Log-query endpoints also use #[log], so an admin viewing logs is itself recorded in operation logs.
Practical Advice
- Normal admin CRUD usually only needs
#[log]and permission macros. - Disable parameter or response logging for login, passwords, tokens, file downloads, and large responses.
- Register
RateLimitEnginebefore enabling rate limits, then start with a small set of endpoints. - Use Redis backend for external APIs or high-cost endpoints.
- Observe with
shadowmode before switching toenforce; reset buckets before switching when needed. - Mount
rate_limit_headers_middlewarewhen clients need automatic backoff hints.
