Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::middleware::DistributedRateLimiter Class Reference

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_
 

Detailed Description

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.

Member Typedef Documentation

◆ KeyFunction

Identifies the client; defaults to req.client_ip (see RateLimitOptions::key).

Constructor & Destructor Documentation

◆ DistributedRateLimiter()

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.

Parameters
redis_hostThe Redis server host.
redis_portThe Redis server port.
max_requestsMaximum number of requests allowed in the time window.
windowThe time window for rate limiting.
keyMaps a request to the client identity; defaults to req.client_ip.
allow_when_unavailableIf Redis cannot be reached, let requests through (fail open) instead of rejecting them with 503 (fail closed).

Member Function Documentation

◆ operator()()

bool orbit::middleware::DistributedRateLimiter::operator() ( http::HttpRequest &  req,
std::shared_ptr< http::ResponseWriter >  writer 
)

Middleware execution operator.

Returns
true If the request is allowed.
false If the request was rejected (429 or 503).

Member Data Documentation

◆ allow_when_unavailable_

bool orbit::middleware::DistributedRateLimiter::allow_when_unavailable_
private

◆ key_

KeyFunction orbit::middleware::DistributedRateLimiter::key_
private

◆ max_requests_

size_t orbit::middleware::DistributedRateLimiter::max_requests_
private

◆ redis_

std::unique_ptr<database::RedisClient> orbit::middleware::DistributedRateLimiter::redis_
private

◆ window_

std::chrono::seconds orbit::middleware::DistributedRateLimiter::window_
private

The documentation for this class was generated from the following file: