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

Token-bucket rate limiter keyed by client. More...

#include <RateLimiter.hpp>

Collaboration diagram for orbit::middleware::RateLimiter:

Classes

struct  Bucket
 

Public Member Functions

 RateLimiter (RateLimitOptions options)
 
 RateLimiter (size_t max_requests, std::chrono::seconds window)
 
bool operator() (http::HttpRequest &req, std::shared_ptr< http::ResponseWriter > writer)
 
size_t tracked_clients ()
 Number of clients currently tracked (for tests and metrics).
 

Private Types

using Clock = std::chrono::steady_clock
 

Private Member Functions

void evict_refilled (Clock::time_point now)
 

Private Attributes

RateLimitOptions options_
 
double refill_per_second_
 
std::unordered_map< std::string, Bucket > buckets_
 
Clock::time_point last_sweep_
 
std::mutex mutex_
 

Detailed Description

Token-bucket rate limiter keyed by client.

Each client gets max_requests tokens that refill continuously over window. Rejected requests receive 429 with Retry-After. Clients whose bucket has refilled are forgotten, and at most max_tracked_clients are kept, so the memory used is bounded no matter how many addresses a client rotates through.

Member Typedef Documentation

◆ Clock

using orbit::middleware::RateLimiter::Clock = std::chrono::steady_clock
private

Constructor & Destructor Documentation

◆ RateLimiter() [1/2]

orbit::middleware::RateLimiter::RateLimiter ( RateLimitOptions  options)
explicit

◆ RateLimiter() [2/2]

orbit::middleware::RateLimiter::RateLimiter ( size_t  max_requests,
std::chrono::seconds  window 
)

Member Function Documentation

◆ evict_refilled()

void orbit::middleware::RateLimiter::evict_refilled ( Clock::time_point  now)
private

◆ operator()()

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

◆ tracked_clients()

size_t orbit::middleware::RateLimiter::tracked_clients ( )

Number of clients currently tracked (for tests and metrics).

Member Data Documentation

◆ buckets_

std::unordered_map<std::string, Bucket> orbit::middleware::RateLimiter::buckets_
private

◆ last_sweep_

Clock::time_point orbit::middleware::RateLimiter::last_sweep_
private

◆ mutex_

std::mutex orbit::middleware::RateLimiter::mutex_
private

◆ options_

RateLimitOptions orbit::middleware::RateLimiter::options_
private

◆ refill_per_second_

double orbit::middleware::RateLimiter::refill_per_second_
private

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