Manages rate limits for HTTP requests, ensuring compliance with set limits.
More...
#include <HttpRateLimiter.hpp>
|
long | create_limit (long requests_per_period, long period_ms) |
| Creates a new rate limit with specified parameters.
|
|
bool | remove_limit (long limit_id) |
| Removes a rate limit with the specified ID.
|
|
bool | allow_request (long general_rate_limit_id, long specific_rate_limit_id) |
| Checks if a request is allowed under the specified general and specific rate limits.
|
|
template<typename Duration = std::chrono::milliseconds> |
Duration | time_until_next_allowed (long general_rate_limit_id, long specific_rate_limit_id) |
| Calculates the delay until the next request is allowed under the specified rate limits.
|
|
template<typename Duration = std::chrono::milliseconds> |
Duration | time_until_any_limit_allows () |
| Finds the shortest delay among all active rate limits.
|
|
|
std::mutex | m_mutex |
| Mutex to protect shared data.
|
|
long | m_next_id = 1 |
| Next available unique ID for rate limits.
|
|
std::unordered_map< long, LimitData > | m_limits |
| Map storing rate limit configurations.
|
|
Manages rate limits for HTTP requests, ensuring compliance with set limits.
Each rate limit is assigned a unique identifier, allowing specific limits to be applied to different request streams. This class controls the rate of requests by monitoring the number of requests within a specified time window.
Definition at line 16 of file HttpRateLimiter.hpp.
◆ time_point_t
◆ allow_request()
bool kurlyk::HttpRateLimiter::allow_request |
( |
long | general_rate_limit_id, |
|
|
long | specific_rate_limit_id ) |
|
inline |
Checks if a request is allowed under the specified general and specific rate limits.
This method checks both the general and specific rate limits without updating their counters unless the request is allowed under both limits. If the request is allowed, it updates the counters accordingly.
- Parameters
-
general_rate_limit_id | Unique identifier for the general rate limit. |
specific_rate_limit_id | Unique identifier for the specific rate limit. |
- Returns
- True if the request is allowed under both limits, false otherwise.
Definition at line 52 of file HttpRateLimiter.hpp.
◆ check_limit()
Checks if a request is allowed under the specified rate limit without updating the count.
- Parameters
-
limit_data | Reference to the LimitData for the rate limit. |
now | The current time point. |
- Returns
- True if the request is allowed, false otherwise.
Definition at line 163 of file HttpRateLimiter.hpp.
◆ create_limit()
long kurlyk::HttpRateLimiter::create_limit |
( |
long | requests_per_period, |
|
|
long | period_ms ) |
|
inline |
Creates a new rate limit with specified parameters.
This method initializes a new rate limit and returns its unique identifier.
- Parameters
-
requests_per_period | Maximum number of requests allowed within the time period. A value of 0 means no limit is applied. |
period_ms | Duration of the time period in milliseconds. |
- Returns
- Unique identifier for the created rate limit.
Definition at line 24 of file HttpRateLimiter.hpp.
◆ remove_limit()
bool kurlyk::HttpRateLimiter::remove_limit |
( |
long | limit_id | ) |
|
|
inline |
Removes a rate limit with the specified ID.
This method allows the removal of an existing rate limit by its unique identifier.
- Parameters
-
limit_id | The unique identifier of the rate limit to remove. |
- Returns
- True if the rate limit was removed successfully, false if the ID was not found.
Definition at line 40 of file HttpRateLimiter.hpp.
◆ time_until_any_limit_allows()
template<typename Duration = std::chrono::milliseconds>
Duration kurlyk::HttpRateLimiter::time_until_any_limit_allows |
( |
| ) |
|
|
inline |
Finds the shortest delay among all active rate limits.
Returns the minimum duration until any of the rate limits allows a request. If all limits are currently available, returns zero.
- Template Parameters
-
Duration | The chrono duration type (e.g., std::chrono::milliseconds, std::chrono::microseconds). |
- Returns
- The shortest wait duration among all limits. Zero if no wait is required.
Definition at line 126 of file HttpRateLimiter.hpp.
◆ time_until_limit_allows()
template<typename Duration>
Duration kurlyk::HttpRateLimiter::time_until_limit_allows |
( |
const LimitData & | limit_data, |
|
|
const time_point_t & | now ) const |
|
inlineprivate |
Calculates the delay required before a request is allowed under a single rate limit.
If the request exceeds the allowed number within the configured period, returns the remaining time until the rate limit resets. Otherwise returns zero. The time unit is determined by the template argument.
- Template Parameters
-
Duration | Duration type (e.g., std::chrono::milliseconds, std::chrono::microseconds). |
- Parameters
-
limit_data | Internal data for the rate limit being evaluated. |
now | Current timestamp to compare against the rate period start time. |
- Returns
- Duration to wait until the rate limit allows a request. Zero if already allowed.
Definition at line 204 of file HttpRateLimiter.hpp.
◆ time_until_next_allowed()
template<typename Duration = std::chrono::milliseconds>
Duration kurlyk::HttpRateLimiter::time_until_next_allowed |
( |
long | general_rate_limit_id, |
|
|
long | specific_rate_limit_id ) |
|
inline |
Calculates the delay until the next request is allowed under the specified rate limits.
Computes the maximum wait time between the general and specific limits. Ensures that a request is only allowed when all applicable limits permit it.
- Template Parameters
-
Duration | Duration type (e.g., std::chrono::milliseconds, std::chrono::microseconds). |
- Parameters
-
general_rate_limit_id | ID of the general rate limit. |
specific_rate_limit_id | ID of the specific rate limit. |
- Returns
- Duration to wait before the request is safe to perform. Zero if already allowed by both limits.
Definition at line 100 of file HttpRateLimiter.hpp.
◆ update_limit()
Updates the request count for the specified rate limit.
- Parameters
-
limit_data | Reference to the LimitData for the rate limit. |
now | The current time point. |
Definition at line 182 of file HttpRateLimiter.hpp.
◆ m_limits
std::unordered_map<long, LimitData> kurlyk::HttpRateLimiter::m_limits |
|
private |
◆ m_mutex
std::mutex kurlyk::HttpRateLimiter::m_mutex |
|
private |
◆ m_next_id
long kurlyk::HttpRateLimiter::m_next_id = 1 |
|
private |
The documentation for this class was generated from the following file: