Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
RateLimiter.hpp
Go to the documentation of this file.
1#pragma once
3#include <string>
4#include <unordered_map>
5#include <mutex>
6#include <chrono>
7#include <functional>
8#include <memory>
11
12namespace orbit::middleware {
13
18 size_t max_requests = 100;
19 std::chrono::seconds window{60};
23 std::function<std::string(const http::HttpRequest&)> key;
24 size_t max_tracked_clients = 100000;
25};
26
36public:
37 explicit RateLimiter(RateLimitOptions options);
38 RateLimiter(size_t max_requests, std::chrono::seconds window);
39
40 bool operator()(http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> writer);
41
44
45private:
46 using Clock = std::chrono::steady_clock;
47
48 struct Bucket {
49 double tokens;
50 Clock::time_point updated;
51 };
52
53 void evict_refilled(Clock::time_point now);
54
57 std::unordered_map<std::string, Bucket> buckets_;
58 Clock::time_point last_sweep_;
59 std::mutex mutex_;
60};
61
62inline auto rate_limit(RateLimitOptions options) {
63 return [limiter = std::make_shared<RateLimiter>(std::move(options))](http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> writer) -> bool {
64 return (*limiter)(req, writer);
65 };
66}
67
68inline auto rate_limit(size_t max_requests, std::chrono::seconds window) {
69 RateLimitOptions options;
70 options.max_requests = max_requests;
71 options.window = window;
72 return rate_limit(std::move(options));
73}
74
75} // namespace middleware
Token-bucket rate limiter keyed by client.
Definition RateLimiter.hpp:35
RateLimitOptions options_
Definition RateLimiter.hpp:55
bool operator()(http::HttpRequest &req, std::shared_ptr< http::ResponseWriter > writer)
Clock::time_point last_sweep_
Definition RateLimiter.hpp:58
size_t tracked_clients()
Number of clients currently tracked (for tests and metrics).
double refill_per_second_
Definition RateLimiter.hpp:56
std::chrono::steady_clock Clock
Definition RateLimiter.hpp:46
RateLimiter(size_t max_requests, std::chrono::seconds window)
RateLimiter(RateLimitOptions options)
std::mutex mutex_
Definition RateLimiter.hpp:59
void evict_refilled(Clock::time_point now)
std::unordered_map< std::string, Bucket > buckets_
Definition RateLimiter.hpp:57
Definition HttpRequest.hpp:19
auto rate_limit(RateLimitOptions options)
Definition RateLimiter.hpp:62
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
Settings for the in-memory rate limiter.
Definition RateLimiter.hpp:17
size_t max_requests
Burst size and requests allowed per window.
Definition RateLimiter.hpp:18
std::chrono::seconds window
Definition RateLimiter.hpp:19
std::function< std::string(const http::HttpRequest &)> key
Definition RateLimiter.hpp:23
size_t max_tracked_clients
Upper bound on remembered clients.
Definition RateLimiter.hpp:24
Definition RateLimiter.hpp:48
Clock::time_point updated
Definition RateLimiter.hpp:50
double tokens
Definition RateLimiter.hpp:49