|
Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
|
Fixed-window rate limiter shared across instances through Redis. More...
#include <DistributedRateLimiter.hpp>
Public Types | |
| using | KeyFunction = std::function< std::string(const http::HttpRequest &)> |
| Identifies the client; defaults to req.client_ip (see RateLimitOptions::key). | |
Public Member Functions | |
| DistributedRateLimiter (const std::string &redis_host, int redis_port, size_t max_requests, std::chrono::seconds window, KeyFunction key=nullptr, bool allow_when_unavailable=false) | |
| Constructs a new Distributed Rate Limiter. | |
| bool | operator() (http::HttpRequest &req, std::shared_ptr< http::ResponseWriter > writer) |
| Middleware execution operator. | |
Private Attributes | |
| size_t | max_requests_ |
| std::chrono::seconds | window_ |
| KeyFunction | key_ |
| bool | allow_when_unavailable_ |
| std::unique_ptr< database::RedisClient > | redis_ |
Fixed-window rate limiter shared across instances through Redis.
Each client may make max_requests requests per window. The counter and its expiry are created atomically, so a client can never be locked out by a counter that never expires. Rejected requests receive 429 with Retry-After.
| using orbit::middleware::DistributedRateLimiter::KeyFunction = std::function<std::string(const http::HttpRequest&)> |
Identifies the client; defaults to req.client_ip (see RateLimitOptions::key).
| orbit::middleware::DistributedRateLimiter::DistributedRateLimiter | ( | const std::string & | redis_host, |
| int | redis_port, | ||
| size_t | max_requests, | ||
| std::chrono::seconds | window, | ||
| KeyFunction | key = nullptr, |
||
| bool | allow_when_unavailable = false |
||
| ) |
Constructs a new Distributed Rate Limiter.
| redis_host | The Redis server host. |
| redis_port | The Redis server port. |
| max_requests | Maximum number of requests allowed in the time window. |
| window | The time window for rate limiting. |
| key | Maps a request to the client identity; defaults to req.client_ip. |
| allow_when_unavailable | If Redis cannot be reached, let requests through (fail open) instead of rejecting them with 503 (fail closed). |
| bool orbit::middleware::DistributedRateLimiter::operator() | ( | http::HttpRequest & | req, |
| std::shared_ptr< http::ResponseWriter > | writer | ||
| ) |
Middleware execution operator.
|
private |
|
private |
|
private |
|
private |
|
private |