![]() |
Kurlyk
|
Manages and processes HTTP requests using a singleton pattern. More...
#include <HttpRequestManager.hpp>
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 HttpRequestManager & | get_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. | |
| HttpRequestManager & | operator= (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. | |
Manages and processes HTTP requests using a singleton pattern.
Definition at line 23 of file HttpRequestManager.hpp.
|
private |
Definition at line 301 of file HttpRequestManager.hpp.
|
private |
Definition at line 302 of file HttpRequestManager.hpp.
|
inlineprivate |
Private constructor to initialize global resources (e.g., cURL).
Definition at line 627 of file HttpRequestManager.hpp.
|
privatedelete |
Deleted copy constructor to enforce the singleton pattern.
|
inline |
Adds a new HTTP request to the manager.
| request_ptr | Unique pointer to the HTTP request object containing request details. |
| callback | Callback function invoked when the request completes. |
Definition at line 37 of file HttpRequestManager.hpp.
|
inline |
Checks if a request is allowed by two optional rate-limit handles.
| 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 shared state. |
| specific_key | Partition key for the specific limit; empty means default shared state. |
Definition at line 107 of file HttpRequestManager.hpp.
|
inline |
Cancels one request by request ID.
| request_id | The unique identifier of the request to cancel. |
| callback | An optional callback function to execute after cancellation. |
Definition at line 225 of file HttpRequestManager.hpp.
|
inline |
Cancels all requests with the specified group ID.
| group_id | The group identifier of requests to cancel. |
| callback | An optional callback function to execute after cancellation. |
Definition at line 237 of file HttpRequestManager.hpp.
|
inlineprivate |
Cleans up pending requests, marking each as failed and invoking its callback.
Definition at line 590 of file HttpRequestManager.hpp.
|
inline |
Creates a rate-limit handle with specified parameters.
| requests_per_period | Maximum number of requests allowed in the specified period. |
| period_ms | Time period in milliseconds during which the rate limit applies. |
| sequential | When true, no other request sharing this limit may start until the current request (including all its retries) has finished. |
Definition at line 77 of file HttpRequestManager.hpp.
|
inline |
Generates a new group ID.
Definition at line 167 of file HttpRequestManager.hpp.
|
inline |
Generates a new unique request ID.
Definition at line 161 of file HttpRequestManager.hpp.
|
inlinestatic |
Get the singleton instance of HttpRequestManager.
Definition at line 28 of file HttpRequestManager.hpp.
|
inline |
Returns a handle for a registered rate limit ID.
Definition at line 83 of file HttpRequestManager.hpp.
|
inline |
Counts pending, failed, and active requests for a group.
| group_id | Group ID to inspect. |
Definition at line 193 of file HttpRequestManager.hpp.
|
inlineprivate |
Definition at line 313 of file HttpRequestManager.hpp.
|
inline |
Checks whether pending, failed, or active requests exist for a group.
| group_id | Group ID to inspect. |
Definition at line 186 of file HttpRequestManager.hpp.
|
inlinestaticprivate |
Definition at line 581 of file HttpRequestManager.hpp.
|
inlineoverridevirtual |
Checks if there are active, pending, or failed requests.
Implements kurlyk::core::INetworkTaskManager.
Definition at line 286 of file HttpRequestManager.hpp.
|
inlinestaticprivate |
Definition at line 569 of file HttpRequestManager.hpp.
|
inlinestaticprivate |
Definition at line 557 of file HttpRequestManager.hpp.
|
inline |
Returns the current maximum pending request count.
Definition at line 179 of file HttpRequestManager.hpp.
|
inlineprivate |
Definition at line 357 of file HttpRequestManager.hpp.
|
inlineprivate |
Definition at line 335 of file HttpRequestManager.hpp.
|
privatedelete |
Deleted copy assignment operator to enforce the singleton pattern.
|
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.
|
inlineprivate |
Processes active requests, moving failed ones to the failed requests list for retrying.
Definition at line 469 of file HttpRequestManager.hpp.
|
inlineprivate |
Processes and cancels HTTP requests based on their request or group IDs.
Definition at line 510 of file HttpRequestManager.hpp.
|
inlineprivate |
Processes all pending requests, moving valid requests to active batches or marking them as failed.
Definition at line 373 of file HttpRequestManager.hpp.
|
inlineprivate |
Attempts to retry failed requests if their retry delay has passed.
Definition at line 486 of file HttpRequestManager.hpp.
|
inline |
Releases in-flight tokens for sequential rate limits.
| 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 123 of file HttpRequestManager.hpp.
|
inline |
Releases manager-owned handle for the specified rate-limit handle.
Definition at line 96 of file HttpRequestManager.hpp.
|
inline |
Removes an existing rate limit with the specified identifier.
| limit_id | The unique identifier of the rate limit to be removed. |
Definition at line 90 of file HttpRequestManager.hpp.
|
inline |
Sets the maximum number of pending requests accepted into the global queue.
| max_pending_requests | Queue limit, or 0 to keep the queue unbounded. |
Definition at line 173 of file HttpRequestManager.hpp.
|
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.
|
inline |
Attempts to enqueue a new HTTP request and reports the admission result.
| request_ptr | Unique pointer to the HTTP request object containing request details. |
| callback | Callback function invoked when the request completes. |
Definition at line 47 of file HttpRequestManager.hpp.
|
inline |
Calculates delay until request is allowed by two handles.
| 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 141 of file HttpRequestManager.hpp.
|
inline |
Registers a callback invoked after all requests from a group finish.
| group_id | Group ID to wait for. |
| callback | Callback invoked when the group becomes idle. |
Definition at line 201 of file HttpRequestManager.hpp.
|
inlineprivatevirtual |
Private destructor to clean up global resources.
Definition at line 632 of file HttpRequestManager.hpp.
|
private |
List of currently active HTTP request batches.
Definition at line 300 of file HttpRequestManager.hpp.
|
private |
List of failed HTTP requests for retrying.
Definition at line 299 of file HttpRequestManager.hpp.
|
private |
Atomic counter for group IDs.
Definition at line 309 of file HttpRequestManager.hpp.
|
private |
Map of group IDs to callbacks waiting until a group becomes idle.
Definition at line 305 of file HttpRequestManager.hpp.
|
private |
Map of group IDs to their associated cancellation callbacks.
Definition at line 304 of file HttpRequestManager.hpp.
|
private |
Maximum number of requests accepted into the pending queue, or zero if unbounded.
Definition at line 311 of file HttpRequestManager.hpp.
|
mutableprivate |
Mutex to protect access to manager state shared with public entry points.
Definition at line 297 of file HttpRequestManager.hpp.
|
private |
Atomic counter for sequential rate-limit tokens.
Definition at line 307 of file HttpRequestManager.hpp.
|
private |
List of pending HTTP requests awaiting processing.
Definition at line 298 of file HttpRequestManager.hpp.
|
private |
Rate limiter for controlling request frequency.
Definition at line 306 of file HttpRequestManager.hpp.
|
private |
Atomic counter for unique request IDs.
Definition at line 308 of file HttpRequestManager.hpp.
|
private |
Map of request IDs to their associated cancellation callbacks.
Definition at line 303 of file HttpRequestManager.hpp.
|
private |
Flag indicating if shutdown has been requested.
Definition at line 310 of file HttpRequestManager.hpp.