Kurlyk
Loading...
Searching...
No Matches
kurlyk::HttpRequestManager Class Referencefinal

Manages and processes HTTP requests using a singleton pattern. More...

#include <HttpRequestManager.hpp>

Inheritance diagram for kurlyk::HttpRequestManager:
kurlyk::core::INetworkTaskManager

Public Member Functions

bool add_request (std::unique_ptr< HttpRequest > request_ptr, HttpResponseCallback callback)
 Adds a new HTTP request to the manager.
SubmitResult submit_request (std::unique_ptr< HttpRequest > request_ptr, HttpResponseCallback callback)
 Attempts to enqueue a new HTTP request and reports the admission result.
HttpRateLimitHandlePtr create_rate_limit (long requests_per_period, long period_ms, bool sequential=false)
 Creates a rate-limit handle with specified parameters.
HttpRateLimitHandlePtr get_rate_limit (long limit_id)
 Returns a handle for a registered rate limit ID.
bool remove_limit (long limit_id)
 Removes an existing rate limit with the specified identifier.
bool remove_limit (const HttpRateLimitHandlePtr &limit)
 Releases manager-owned handle for the specified rate-limit handle.
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.
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.
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)
 Calculates delay until request is allowed by two handles.
uint64_t generate_request_id ()
 Generates a new unique request ID.
uint64_t generate_group_id ()
 Generates a new group ID.
void set_max_pending_requests (std::size_t max_pending_requests)
 Sets the maximum number of pending requests accepted into the global queue.
std::size_t max_pending_requests () const
 Returns the current maximum pending request count.
bool has_requests_by_group_id (uint64_t group_id) const
 Checks whether pending, failed, or active requests exist for a group.
std::size_t group_request_count (uint64_t group_id) const
 Counts pending, failed, and active requests for a group.
void wait_requests_by_group_id (uint64_t group_id, std::function< void()> callback)
 Registers a callback invoked after all requests from a group finish.
void cancel_request_by_id (uint64_t request_id, std::function< void()> callback)
 Cancels one request by request ID.
void cancel_requests_by_group_id (uint64_t group_id, std::function< void()> callback)
 Cancels all requests with the specified group ID.
void process () override
 Processes all requests in the manager.
void shutdown () override
 Shuts down the request manager, clearing all active and pending requests.
const bool is_loaded () const override
 Checks if there are active, pending, or failed requests.
Public Member Functions inherited from kurlyk::core::INetworkTaskManager
virtual ~INetworkTaskManager ()=default

Static Public Member Functions

static HttpRequestManagerget_instance ()
 Get the singleton instance of HttpRequestManager.

Private Types

using callback_list_t = std::list<std::function<void()>>
using cancel_map_t = std::unordered_map<uint64_t, callback_list_t>

Private Member Functions

std::size_t group_request_count_unlocked (uint64_t group_id) const
void notify_group_waiters_if_idle ()
void notify_all_group_waiters ()
void process_pending_requests ()
 Processes all pending requests, moving valid requests to active batches or marking them as failed.
void process_active_requests ()
 Processes active requests, moving failed ones to the failed requests list for retrying.
void process_retry_failed_requests ()
 Attempts to retry failed requests if their retry delay has passed.
void process_cancel_requests ()
 Processes and cancels HTTP requests based on their request or group IDs.
void cleanup_pending_requests ()
 Cleans up pending requests, marking each as failed and invoking its callback.
 HttpRequestManager ()
 Private constructor to initialize global resources (e.g., cURL).
virtual ~HttpRequestManager ()
 Private destructor to clean up global resources.
 HttpRequestManager (const HttpRequestManager &)=delete
 Deleted copy constructor to enforce the singleton pattern.
HttpRequestManageroperator= (const HttpRequestManager &)=delete
 Deleted copy assignment operator to enforce the singleton pattern.

Static Private Member Functions

static bool matches_cancel (const std::unique_ptr< HttpRequestContext > &ctx, const cancel_map_t &requests_to_cancel, const cancel_map_t &groups_to_cancel)
static HttpResponsePtr make_cancelled_response ()
static void invoke_cancel_callbacks (const cancel_map_t &requests_to_cancel)

Private Attributes

std::mutex m_mutex
 Mutex to protect access to manager state shared with public entry points.
std::list< std::unique_ptr< HttpRequestContext > > m_pending_requests
 List of pending HTTP requests awaiting processing.
std::list< std::unique_ptr< HttpRequestContext > > m_failed_requests
 List of failed HTTP requests for retrying.
std::list< std::unique_ptr< HttpBatchRequestHandler > > m_active_request_batches
 List of currently active HTTP request batches.
cancel_map_t m_requests_to_cancel_by_id
 Map of request IDs to their associated cancellation callbacks.
cancel_map_t m_groups_to_cancel
 Map of group IDs to their associated cancellation callbacks.
cancel_map_t m_group_waiters
 Map of group IDs to callbacks waiting until a group becomes idle.
HttpRateLimiter m_rate_limiter
 Rate limiter for controlling request frequency.
std::atomic< uint64_t > m_next_in_flight_token {1}
 Atomic counter for sequential rate-limit tokens.
std::atomic< uint64_t > m_request_id_counter = ATOMIC_VAR_INIT(1)
 Atomic counter for unique request IDs.
std::atomic< uint64_t > m_group_id_counter = ATOMIC_VAR_INIT(1)
 Atomic counter for group IDs.
std::atomic< bool > m_shutdown = ATOMIC_VAR_INIT(false)
 Flag indicating if shutdown has been requested.
std::atomic< std::size_t > m_max_pending_requests = ATOMIC_VAR_INIT(0)
 Maximum number of requests accepted into the pending queue, or zero if unbounded.

Detailed Description

Manages and processes HTTP requests using a singleton pattern.

Note
All process_*(), is_loaded(), and shutdown() must be called exclusively from the NetworkWorker thread. m_mutex guards public entry points and group waiters.

Definition at line 23 of file HttpRequestManager.hpp.

Member Typedef Documentation

◆ callback_list_t

using kurlyk::HttpRequestManager::callback_list_t = std::list<std::function<void()>>
private

Definition at line 301 of file HttpRequestManager.hpp.

◆ cancel_map_t

using kurlyk::HttpRequestManager::cancel_map_t = std::unordered_map<uint64_t, callback_list_t>
private

Definition at line 302 of file HttpRequestManager.hpp.

Constructor & Destructor Documentation

◆ HttpRequestManager() [1/2]

kurlyk::HttpRequestManager::HttpRequestManager ( )
inlineprivate

Private constructor to initialize global resources (e.g., cURL).

Definition at line 627 of file HttpRequestManager.hpp.

◆ HttpRequestManager() [2/2]

kurlyk::HttpRequestManager::HttpRequestManager ( const HttpRequestManager & )
privatedelete

Deleted copy constructor to enforce the singleton pattern.

Member Function Documentation

◆ add_request()

bool kurlyk::HttpRequestManager::add_request ( std::unique_ptr< HttpRequest > request_ptr,
HttpResponseCallback callback )
inline

Adds a new HTTP request to the manager.

Parameters
request_ptrUnique pointer to the HTTP request object containing request details.
callbackCallback function invoked when the request completes.
Returns
True if the request was successfully added, false if admission was rejected.

Definition at line 37 of file HttpRequestManager.hpp.

◆ allow_request()

bool kurlyk::HttpRequestManager::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 )
inline

Checks if a request is allowed by two optional rate-limit handles.

Parameters
general_limitGeneral rate-limit handle (may be empty).
specific_limitSpecific rate-limit handle (may be empty).
in_flight_tokenToken identifying the in-flight request; 0 skips sequential checks.
general_keyPartition key for the general limit; empty means default shared state.
specific_keyPartition key for the specific limit; empty means default shared state.
Returns
True if the request is allowed, false otherwise.

Definition at line 107 of file HttpRequestManager.hpp.

◆ cancel_request_by_id()

void kurlyk::HttpRequestManager::cancel_request_by_id ( uint64_t request_id,
std::function< void()> callback )
inline

Cancels one request by request ID.

Parameters
request_idThe unique identifier of the request to cancel.
callbackAn optional callback function to execute after cancellation.

Definition at line 225 of file HttpRequestManager.hpp.

◆ cancel_requests_by_group_id()

void kurlyk::HttpRequestManager::cancel_requests_by_group_id ( uint64_t group_id,
std::function< void()> callback )
inline

Cancels all requests with the specified group ID.

Parameters
group_idThe group identifier of requests to cancel.
callbackAn optional callback function to execute after cancellation.

Definition at line 237 of file HttpRequestManager.hpp.

◆ cleanup_pending_requests()

void kurlyk::HttpRequestManager::cleanup_pending_requests ( )
inlineprivate

Cleans up pending requests, marking each as failed and invoking its callback.

Definition at line 590 of file HttpRequestManager.hpp.

◆ create_rate_limit()

HttpRateLimitHandlePtr kurlyk::HttpRequestManager::create_rate_limit ( long requests_per_period,
long period_ms,
bool sequential = false )
inline

Creates a rate-limit handle with specified parameters.

Parameters
requests_per_periodMaximum number of requests allowed in the specified period.
period_msTime period in milliseconds during which the rate limit applies.
sequentialWhen true, no other request sharing this limit may start until the current request (including all its retries) has finished.
Returns
RAII handle for the created rate limit.

Definition at line 77 of file HttpRequestManager.hpp.

◆ generate_group_id()

uint64_t kurlyk::HttpRequestManager::generate_group_id ( )
inline

Generates a new group ID.

Returns
A new group ID.

Definition at line 167 of file HttpRequestManager.hpp.

◆ generate_request_id()

uint64_t kurlyk::HttpRequestManager::generate_request_id ( )
inline

Generates a new unique request ID.

Returns
A new unique request ID.

Definition at line 161 of file HttpRequestManager.hpp.

◆ get_instance()

HttpRequestManager & kurlyk::HttpRequestManager::get_instance ( )
inlinestatic

Get the singleton instance of HttpRequestManager.

Returns
Reference to the singleton instance.

Definition at line 28 of file HttpRequestManager.hpp.

◆ get_rate_limit()

HttpRateLimitHandlePtr kurlyk::HttpRequestManager::get_rate_limit ( long limit_id)
inline

Returns a handle for a registered rate limit ID.

Note
Returns empty if the manager-owned handle was already released.

Definition at line 83 of file HttpRequestManager.hpp.

◆ group_request_count()

std::size_t kurlyk::HttpRequestManager::group_request_count ( uint64_t group_id) const
inline

Counts pending, failed, and active requests for a group.

Parameters
group_idGroup ID to inspect.
Returns
Number of managed requests that belong to this group.

Definition at line 193 of file HttpRequestManager.hpp.

◆ group_request_count_unlocked()

std::size_t kurlyk::HttpRequestManager::group_request_count_unlocked ( uint64_t group_id) const
inlineprivate

Definition at line 313 of file HttpRequestManager.hpp.

◆ has_requests_by_group_id()

bool kurlyk::HttpRequestManager::has_requests_by_group_id ( uint64_t group_id) const
inline

Checks whether pending, failed, or active requests exist for a group.

Parameters
group_idGroup ID to inspect.
Returns
True if at least one managed request belongs to this group.

Definition at line 186 of file HttpRequestManager.hpp.

◆ invoke_cancel_callbacks()

void kurlyk::HttpRequestManager::invoke_cancel_callbacks ( const cancel_map_t & requests_to_cancel)
inlinestaticprivate

Definition at line 581 of file HttpRequestManager.hpp.

◆ is_loaded()

const bool kurlyk::HttpRequestManager::is_loaded ( ) const
inlineoverridevirtual

Checks if there are active, pending, or failed requests.

Returns
True if there are requests still being managed, otherwise false.

Implements kurlyk::core::INetworkTaskManager.

Definition at line 286 of file HttpRequestManager.hpp.

◆ make_cancelled_response()

HttpResponsePtr kurlyk::HttpRequestManager::make_cancelled_response ( )
inlinestaticprivate

Definition at line 569 of file HttpRequestManager.hpp.

◆ matches_cancel()

bool kurlyk::HttpRequestManager::matches_cancel ( const std::unique_ptr< HttpRequestContext > & ctx,
const cancel_map_t & requests_to_cancel,
const cancel_map_t & groups_to_cancel )
inlinestaticprivate

Definition at line 557 of file HttpRequestManager.hpp.

◆ max_pending_requests()

std::size_t kurlyk::HttpRequestManager::max_pending_requests ( ) const
inline

Returns the current maximum pending request count.

Returns
Configured queue limit, or 0 if the queue is unbounded.

Definition at line 179 of file HttpRequestManager.hpp.

◆ notify_all_group_waiters()

void kurlyk::HttpRequestManager::notify_all_group_waiters ( )
inlineprivate

Definition at line 357 of file HttpRequestManager.hpp.

◆ notify_group_waiters_if_idle()

void kurlyk::HttpRequestManager::notify_group_waiters_if_idle ( )
inlineprivate

Definition at line 335 of file HttpRequestManager.hpp.

◆ operator=()

HttpRequestManager & kurlyk::HttpRequestManager::operator= ( const HttpRequestManager & )
privatedelete

Deleted copy assignment operator to enforce the singleton pattern.

◆ process()

void kurlyk::HttpRequestManager::process ( )
inlineoverridevirtual

Processes all requests in the manager.

Executes pending, active, and retry-eligible failed requests.

Implements kurlyk::core::INetworkTaskManager.

Definition at line 259 of file HttpRequestManager.hpp.

◆ process_active_requests()

void kurlyk::HttpRequestManager::process_active_requests ( )
inlineprivate

Processes active requests, moving failed ones to the failed requests list for retrying.

Definition at line 469 of file HttpRequestManager.hpp.

◆ process_cancel_requests()

void kurlyk::HttpRequestManager::process_cancel_requests ( )
inlineprivate

Processes and cancels HTTP requests based on their request or group IDs.

Definition at line 510 of file HttpRequestManager.hpp.

◆ process_pending_requests()

void kurlyk::HttpRequestManager::process_pending_requests ( )
inlineprivate

Processes all pending requests, moving valid requests to active batches or marking them as failed.

Definition at line 373 of file HttpRequestManager.hpp.

◆ process_retry_failed_requests()

void kurlyk::HttpRequestManager::process_retry_failed_requests ( )
inlineprivate

Attempts to retry failed requests if their retry delay has passed.

Definition at line 486 of file HttpRequestManager.hpp.

◆ release_request()

void kurlyk::HttpRequestManager::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 )
inline

Releases in-flight tokens for sequential rate limits.

Parameters
general_limitGeneral rate-limit handle (may be empty).
specific_limitSpecific rate-limit handle (may be empty).
in_flight_tokenToken identifying the in-flight request; 0 is a no-op.
general_keyPartition key for the general limit.
specific_keyPartition key for the specific limit.

Definition at line 123 of file HttpRequestManager.hpp.

◆ remove_limit() [1/2]

bool kurlyk::HttpRequestManager::remove_limit ( const HttpRateLimitHandlePtr & limit)
inline

Releases manager-owned handle for the specified rate-limit handle.

Note
Physical limit data may remain alive while requests still hold handles.

Definition at line 96 of file HttpRequestManager.hpp.

◆ remove_limit() [2/2]

bool kurlyk::HttpRequestManager::remove_limit ( long limit_id)
inline

Removes an existing rate limit with the specified identifier.

Parameters
limit_idThe unique identifier of the rate limit to be removed.
Returns
True if the rate limit was successfully removed, or false if the rate limit ID was not found.

Definition at line 90 of file HttpRequestManager.hpp.

◆ set_max_pending_requests()

void kurlyk::HttpRequestManager::set_max_pending_requests ( std::size_t max_pending_requests)
inline

Sets the maximum number of pending requests accepted into the global queue.

Parameters
max_pending_requestsQueue limit, or 0 to keep the queue unbounded.

Definition at line 173 of file HttpRequestManager.hpp.

◆ shutdown()

void kurlyk::HttpRequestManager::shutdown ( )
inlineoverridevirtual

Shuts down the request manager, clearing all active and pending requests.

Stops request processing and releases all resources tied to active and pending requests.

Implements kurlyk::core::INetworkTaskManager.

Definition at line 273 of file HttpRequestManager.hpp.

◆ submit_request()

SubmitResult kurlyk::HttpRequestManager::submit_request ( std::unique_ptr< HttpRequest > request_ptr,
HttpResponseCallback callback )
inline

Attempts to enqueue a new HTTP request and reports the admission result.

Parameters
request_ptrUnique pointer to the HTTP request object containing request details.
callbackCallback function invoked when the request completes.
Returns
SubmitResult describing whether the request was accepted into the pending queue.

Definition at line 47 of file HttpRequestManager.hpp.

◆ time_until_next_allowed()

template<typename Duration = std::chrono::milliseconds>
RateLimitDelay< Duration > kurlyk::HttpRequestManager::time_until_next_allowed ( const HttpRateLimitHandlePtr & general_limit,
const HttpRateLimitHandlePtr & specific_limit,
const std::string & general_key,
const std::string & specific_key )
inline

Calculates delay until request is allowed by two handles.

Template Parameters
DurationDuration type; defaults to std::chrono::milliseconds.
Parameters
general_limitGeneral rate-limit handle (may be empty).
specific_limitSpecific rate-limit handle (may be empty).
general_keyPartition key for the general limit; empty means default shared state.
specific_keyPartition key for the specific limit; empty means default shared state.
Returns
RateLimitDelay describing the maximum delay across both dimensions.

Definition at line 141 of file HttpRequestManager.hpp.

◆ wait_requests_by_group_id()

void kurlyk::HttpRequestManager::wait_requests_by_group_id ( uint64_t group_id,
std::function< void()> callback )
inline

Registers a callback invoked after all requests from a group finish.

Parameters
group_idGroup ID to wait for.
callbackCallback invoked when the group becomes idle.

Definition at line 201 of file HttpRequestManager.hpp.

◆ ~HttpRequestManager()

virtual kurlyk::HttpRequestManager::~HttpRequestManager ( )
inlineprivatevirtual

Private destructor to clean up global resources.

Definition at line 632 of file HttpRequestManager.hpp.

Member Data Documentation

◆ m_active_request_batches

std::list<std::unique_ptr<HttpBatchRequestHandler> > kurlyk::HttpRequestManager::m_active_request_batches
private

List of currently active HTTP request batches.

Definition at line 300 of file HttpRequestManager.hpp.

◆ m_failed_requests

std::list<std::unique_ptr<HttpRequestContext> > kurlyk::HttpRequestManager::m_failed_requests
private

List of failed HTTP requests for retrying.

Definition at line 299 of file HttpRequestManager.hpp.

◆ m_group_id_counter

std::atomic<uint64_t> kurlyk::HttpRequestManager::m_group_id_counter = ATOMIC_VAR_INIT(1)
private

Atomic counter for group IDs.

Definition at line 309 of file HttpRequestManager.hpp.

◆ m_group_waiters

cancel_map_t kurlyk::HttpRequestManager::m_group_waiters
private

Map of group IDs to callbacks waiting until a group becomes idle.

Definition at line 305 of file HttpRequestManager.hpp.

◆ m_groups_to_cancel

cancel_map_t kurlyk::HttpRequestManager::m_groups_to_cancel
private

Map of group IDs to their associated cancellation callbacks.

Definition at line 304 of file HttpRequestManager.hpp.

◆ m_max_pending_requests

std::atomic<std::size_t> kurlyk::HttpRequestManager::m_max_pending_requests = ATOMIC_VAR_INIT(0)
private

Maximum number of requests accepted into the pending queue, or zero if unbounded.

Definition at line 311 of file HttpRequestManager.hpp.

◆ m_mutex

std::mutex kurlyk::HttpRequestManager::m_mutex
mutableprivate

Mutex to protect access to manager state shared with public entry points.

Definition at line 297 of file HttpRequestManager.hpp.

◆ m_next_in_flight_token

std::atomic<uint64_t> kurlyk::HttpRequestManager::m_next_in_flight_token {1}
private

Atomic counter for sequential rate-limit tokens.

Definition at line 307 of file HttpRequestManager.hpp.

◆ m_pending_requests

std::list<std::unique_ptr<HttpRequestContext> > kurlyk::HttpRequestManager::m_pending_requests
private

List of pending HTTP requests awaiting processing.

Definition at line 298 of file HttpRequestManager.hpp.

◆ m_rate_limiter

HttpRateLimiter kurlyk::HttpRequestManager::m_rate_limiter
private

Rate limiter for controlling request frequency.

Definition at line 306 of file HttpRequestManager.hpp.

◆ m_request_id_counter

std::atomic<uint64_t> kurlyk::HttpRequestManager::m_request_id_counter = ATOMIC_VAR_INIT(1)
private

Atomic counter for unique request IDs.

Definition at line 308 of file HttpRequestManager.hpp.

◆ m_requests_to_cancel_by_id

cancel_map_t kurlyk::HttpRequestManager::m_requests_to_cancel_by_id
private

Map of request IDs to their associated cancellation callbacks.

Definition at line 303 of file HttpRequestManager.hpp.

◆ m_shutdown

std::atomic<bool> kurlyk::HttpRequestManager::m_shutdown = ATOMIC_VAR_INIT(false)
private

Flag indicating if shutdown has been requested.

Definition at line 310 of file HttpRequestManager.hpp.


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