![]() |
Kurlyk
|
Manages rate limits for HTTP requests. More...
#include <HttpRateLimiter.hpp>
Classes | |
| struct | KeyState |
| Per-key mutable runtime state inside a rate limit. More... | |
| struct | LimitData |
| Immutable limit parameters plus per-key mutable state. More... | |
Public Member Functions | |
| HttpRateLimitHandlePtr | create_limit_handle (long requests_per_period, long period_ms, bool sequential=false) |
| Creates a new rate limit and returns its RAII handle. | |
| long | create_limit (long requests_per_period, long period_ms) |
| Legacy API: creates a new rate limit and returns only its ID. | |
| bool | remove_limit (long limit_id) |
| Releases manager-owned handle for the specified limit ID. | |
| bool | remove_limit (const HttpRateLimitHandlePtr &handle) |
| Releases manager-owned handle for the specified limit handle. | |
| HttpRateLimitHandlePtr | get_limit (long limit_id) |
| Returns manager-owned handle by ID. | |
| bool | allow_request (const HttpRateLimitHandlePtr &general_limit, const HttpRateLimitHandlePtr &specific_limit, uint64_t in_flight_token, const std::string &general_key, const std::string &specific_key) |
| Checks if a request is allowed by two optional rate-limit handles with in-flight token tracking for sequential limits. | |
| bool | allow_request (const HttpRateLimitHandlePtr &general_limit, const HttpRateLimitHandlePtr &specific_limit) |
| Handle-based overload without explicit token or keys (token = 0, keys empty). | |
| bool | allow_request (long general_rate_limit_id, long specific_rate_limit_id) |
| Legacy API: checks if request is allowed by two limit IDs. | |
| void | release_request (const HttpRateLimitHandlePtr &general_limit, const HttpRateLimitHandlePtr &specific_limit, uint64_t in_flight_token, const std::string &general_key, const std::string &specific_key) |
| Releases in-flight tokens for sequential rate limits. | |
| void | release_request (const HttpRateLimitHandlePtr &general_limit, const HttpRateLimitHandlePtr &specific_limit, uint64_t in_flight_token) |
| Legacy overload without keys. | |
| template<typename Duration = std::chrono::milliseconds> | |
| RateLimitDelay< Duration > | time_until_next_allowed (const HttpRateLimitHandlePtr &general_limit, const HttpRateLimitHandlePtr &specific_limit, const std::string &general_key, const std::string &specific_key) |
| template<typename Duration = std::chrono::milliseconds> | |
| RateLimitDelay< Duration > | time_until_next_allowed (const HttpRateLimitHandlePtr &general_limit, const HttpRateLimitHandlePtr &specific_limit) |
| Legacy overload without explicit partition keys (uses default shared state). | |
| template<typename Duration = std::chrono::milliseconds> | |
| RateLimitDelay< Duration > | time_until_next_allowed (long general_rate_limit_id, long specific_rate_limit_id) |
| Legacy API: calculates delay by limit IDs. | |
| template<typename Duration = std::chrono::milliseconds> | |
| RateLimitDelay< Duration > | time_until_any_limit_allows () |
| Finds the shortest delay among all physically alive limits. | |
Private Types | |
| using | time_point_t = std::chrono::steady_clock::time_point |
Private Member Functions | |
| bool | remove_limit_internal (long limit_id) |
| Marks a limit as removed and erases it only when no key state remains. | |
| KeyState & | get_key_state (LimitData &limit, const std::string &key) |
| Looks up a KeyState by key, creating it lazily if necessary. | |
| const KeyState * | find_key_state (const LimitData &limit, const std::string &key) const |
| Looks up a KeyState by key for read-only access (no insertion). | |
| bool | can_pass (const LimitData &limit, const std::string &key, uint64_t token, const time_point_t &now) const |
| Checks if a key within a limit can pass both sequential and count/period constraints. | |
| bool | check_key (const LimitData &limit_data, const KeyState &state, const time_point_t &now) const |
| void | commit_limit (LimitData &limit, const std::string &key, uint64_t token, const time_point_t &now) |
| Commits in-flight token and count/period state after a successful can_pass. | |
| void | update_key (LimitData &limit_data, KeyState &state, const time_point_t &now) |
| void | release_key (LimitData &limit, const std::string &key, uint64_t token) |
| Releases an in-flight token from a specific key and erases the key if it has no runtime state. | |
| template<typename Duration> | |
| Duration | time_until_limit_allows (const LimitData &limit, const std::string &key, const time_point_t &now) const |
| template<typename Duration> | |
| Duration | time_until_key_allows (const LimitData &limit, const KeyState &state, const time_point_t &now) const |
| void | gc_stale_keys (const time_point_t &now) |
| Erases keys that are empty or whose period has expired. | |
Private Attributes | |
| std::mutex | m_mutex |
| long | m_next_id = 1 |
| std::unordered_map< long, LimitData > | m_limits |
| Physically alive limit data. | |
| std::unordered_map< long, HttpRateLimitHandlePtr > | m_owned_handles |
| Manager-owned handles for limits created through create_limit_handle(). | |
| size_t | m_gc_counter = 0 |
Manages rate limits for HTTP requests.
Rate limits are referenced by RAII handles. A limit remains physically present while at least one HttpRateLimitHandlePtr exists. Calling remove_limit(id) only releases the manager-owned handle. If pending or active requests still hold copied handles, the limit remains active until those requests are destroyed.
Each limit can be partitioned by string keys. Requests sharing the same key within the same limit ID share rate-limit state; different keys are independent. An empty key maps to the default shared state.
Definition at line 22 of file HttpRateLimiter.hpp.
|
private |
Definition at line 376 of file HttpRateLimiter.hpp.
|
inline |
Handle-based overload without explicit token or keys (token = 0, keys empty).
Definition at line 194 of file HttpRateLimiter.hpp.
|
inline |
Checks if a request is allowed by two optional rate-limit handles with in-flight token tracking for sequential limits.
Counters and in-flight sets are updated only if both limits allow the request.
| general_limit | General rate-limit handle (may be empty). |
| specific_limit | Specific rate-limit handle (may be empty). |
| in_flight_token | Token identifying the in-flight request; 0 skips sequential checks. |
| general_key | Partition key for the general limit; empty means default state. |
| specific_key | Partition key for the specific limit; empty means default state. |
Definition at line 128 of file HttpRateLimiter.hpp.
|
inline |
Legacy API: checks if request is allowed by two limit IDs.
Definition at line 205 of file HttpRateLimiter.hpp.
|
inlineprivate |
Checks if a key within a limit can pass both sequential and count/period constraints.
Definition at line 434 of file HttpRateLimiter.hpp.
|
inlineprivate |
Definition at line 452 of file HttpRateLimiter.hpp.
|
inlineprivate |
Commits in-flight token and count/period state after a successful can_pass.
Definition at line 470 of file HttpRateLimiter.hpp.
|
inline |
Legacy API: creates a new rate limit and returns only its ID.
Definition at line 63 of file HttpRateLimiter.hpp.
|
inline |
Creates a new rate limit and returns its RAII handle.
| requests_per_period | Maximum requests allowed within the period. 0 means unlimited. |
| period_ms | Period duration in milliseconds. |
| sequential | When `true`, no other request sharing this limit may start until the current request (including all its retries) has finished. |
Definition at line 30 of file HttpRateLimiter.hpp.
|
inlineprivate |
Looks up a KeyState by key for read-only access (no insertion).
Definition at line 425 of file HttpRateLimiter.hpp.
|
inlineprivate |
Erases keys that are empty or whose period has expired.
Definition at line 571 of file HttpRateLimiter.hpp.
|
inlineprivate |
Looks up a KeyState by key, creating it lazily if necessary.
Definition at line 420 of file HttpRateLimiter.hpp.
|
inline |
Returns manager-owned handle by ID.
Definition at line 107 of file HttpRateLimiter.hpp.
|
inlineprivate |
Releases an in-flight token from a specific key and erases the key if it has no runtime state.
Definition at line 501 of file HttpRateLimiter.hpp.
|
inline |
Legacy overload without keys.
Definition at line 254 of file HttpRateLimiter.hpp.
|
inline |
Releases in-flight tokens for sequential rate limits.
Must be called when a request finishes (including after cancellation or destruction).
| general_limit | General rate-limit handle (may be empty). |
| specific_limit | Specific rate-limit handle (may be empty). |
| in_flight_token | Token identifying the in-flight request; 0 is a no-op. |
| general_key | Partition key for the general limit. |
| specific_key | Partition key for the specific limit. |
Definition at line 223 of file HttpRateLimiter.hpp.
|
inline |
Releases manager-owned handle for the specified limit handle.
| handle | Rate-limit handle. |
Definition at line 100 of file HttpRateLimiter.hpp.
|
inline |
Releases manager-owned handle for the specified limit ID.
This does not necessarily erase LimitData immediately. If any pending or active request still holds a copied handle, the limit stays alive. Physical erase happens from HttpRateLimitHandle destructor.
| limit_id | Rate-limit ID. |
Definition at line 76 of file HttpRateLimiter.hpp.
|
inlineprivate |
Marks a limit as removed and erases it only when no key state remains.
This method is intentionally private. It must be called only from HttpRateLimitHandle destruction path.
Definition at line 404 of file HttpRateLimiter.hpp.
|
inline |
Finds the shortest delay among all physically alive limits.
| Duration | Duration type; defaults to std::chrono::milliseconds. |
Definition at line 341 of file HttpRateLimiter.hpp.
|
inlineprivate |
Definition at line 540 of file HttpRateLimiter.hpp.
|
inlineprivate |
Definition at line 520 of file HttpRateLimiter.hpp.
|
inline |
Legacy overload without explicit partition keys (uses default shared state).
Definition at line 310 of file HttpRateLimiter.hpp.
|
inline |
| Duration | Duration type; defaults to std::chrono::milliseconds. |
| general_limit | General rate-limit handle (may be empty). |
| specific_limit | Specific rate-limit handle (may be empty). |
| general_key | Partition key for the general limit; empty means default shared state. |
| specific_key | Partition key for the specific limit; empty means default shared state. |
Definition at line 271 of file HttpRateLimiter.hpp.
|
inline |
Legacy API: calculates delay by limit IDs.
| Duration | Duration type; defaults to std::chrono::milliseconds. |
| general_rate_limit_id | General rate-limit ID. |
| specific_rate_limit_id | Specific rate-limit ID. |
Definition at line 324 of file HttpRateLimiter.hpp.
|
inlineprivate |
Definition at line 482 of file HttpRateLimiter.hpp.
|
private |
Definition at line 617 of file HttpRateLimiter.hpp.
|
private |
Physically alive limit data.
A record remains here while at least one HttpRateLimitHandlePtr exists.
Definition at line 609 of file HttpRateLimiter.hpp.
|
mutableprivate |
Definition at line 602 of file HttpRateLimiter.hpp.
|
private |
Definition at line 604 of file HttpRateLimiter.hpp.
|
private |
Manager-owned handles for limits created through create_limit_handle().
remove_limit(id) releases the handle from this map. If requests still hold copies, physical data stays alive until the last copy is destroyed.
Definition at line 615 of file HttpRateLimiter.hpp.