Kurlyk
Loading...
Searching...
No Matches
kurlyk::HttpRateLimiter Class Reference

Manages rate limits for HTTP requests, ensuring compliance with set limits. More...

#include <HttpRateLimiter.hpp>

Classes

struct  LimitData
 Stores data for an individual rate limit. More...
 

Public Member Functions

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.
 

Private Types

using time_point_t = std::chrono::steady_clock::time_point
 

Private Member Functions

bool check_limit (LimitData &limit_data, const time_point_t &now)
 Checks if a request is allowed under the specified rate limit without updating the count.
 
void update_limit (LimitData &limit_data, const time_point_t &now)
 Updates the request count for the specified rate limit.
 
template<typename Duration>
Duration time_until_limit_allows (const LimitData &limit_data, const time_point_t &now) const
 Calculates the delay required before a request is allowed under a single rate limit.
 

Private Attributes

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, LimitDatam_limits
 Map storing rate limit configurations.
 

Detailed Description

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.

Member Typedef Documentation

◆ time_point_t

using kurlyk::HttpRateLimiter::time_point_t = std::chrono::steady_clock::time_point
private

Definition at line 144 of file HttpRateLimiter.hpp.

Member Function Documentation

◆ 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_idUnique identifier for the general rate limit.
specific_rate_limit_idUnique 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()

bool kurlyk::HttpRateLimiter::check_limit ( LimitData & limit_data,
const time_point_t & now )
inlineprivate

Checks if a request is allowed under the specified rate limit without updating the count.

Parameters
limit_dataReference to the LimitData for the rate limit.
nowThe 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_periodMaximum number of requests allowed within the time period. A value of 0 means no limit is applied.
period_msDuration 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_idThe 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
DurationThe 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
DurationDuration type (e.g., std::chrono::milliseconds, std::chrono::microseconds).
Parameters
limit_dataInternal data for the rate limit being evaluated.
nowCurrent 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
DurationDuration type (e.g., std::chrono::milliseconds, std::chrono::microseconds).
Parameters
general_rate_limit_idID of the general rate limit.
specific_rate_limit_idID 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()

void kurlyk::HttpRateLimiter::update_limit ( LimitData & limit_data,
const time_point_t & now )
inlineprivate

Updates the request count for the specified rate limit.

Parameters
limit_dataReference to the LimitData for the rate limit.
nowThe current time point.

Definition at line 182 of file HttpRateLimiter.hpp.

Member Data Documentation

◆ m_limits

std::unordered_map<long, LimitData> kurlyk::HttpRateLimiter::m_limits
private

Map storing rate limit configurations.

Definition at line 157 of file HttpRateLimiter.hpp.

◆ m_mutex

std::mutex kurlyk::HttpRateLimiter::m_mutex
private

Mutex to protect shared data.

Definition at line 155 of file HttpRateLimiter.hpp.

◆ m_next_id

long kurlyk::HttpRateLimiter::m_next_id = 1
private

Next available unique ID for rate limits.

Definition at line 156 of file HttpRateLimiter.hpp.


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