Kurlyk
Loading...
Searching...
No Matches
HttpRequestContext.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef KURLYK_HEADER_KURLYK_HTTP_HTTP_REQUEST_MANAGER_HTTP_REQUEST_CONTEXT_HPP_INCLUDED
3#define KURLYK_HEADER_KURLYK_HTTP_HTTP_REQUEST_MANAGER_HTTP_REQUEST_CONTEXT_HPP_INCLUDED
4
7
8namespace kurlyk {
9
13 public:
14 using time_point_t = std::chrono::steady_clock::time_point;
15
16 std::unique_ptr<HttpRequest> request;
20 uint64_t in_flight_token = 0;
21 std::function<void()> on_complete;
22 std::function<void()> on_group_complete;
23 std::atomic<bool> complete_called{false};
24
29 std::unique_ptr<HttpRequest> request_ptr,
31 : request(std::move(request_ptr)),
32 callback(std::move(callback)),
35 complete_called(false) {
36 }
37
38 HttpRequestContext() = default;
39
42 void invoke_callback(HttpResponsePtr response) noexcept {
43 if (!callback) return;
44
45 try {
46 callback(std::move(response));
47 } catch (...) {
49 std::current_exception(),
50 "Unhandled exception in HttpRequestContext response callback");
51 }
52 }
53
56 void invoke_final_callback(HttpResponsePtr response) noexcept {
57 invoke_callback(std::move(response));
58 complete();
59 }
60
62 void complete() noexcept {
63 bool expected = false;
64 if (!complete_called.compare_exchange_strong(expected, true)) {
65 return;
66 }
67
68 try {
69 if (on_complete) {
71 }
72 } catch (...) {
74 std::current_exception(),
75 "Exception in HttpRequestContext completion callback");
76 }
77
78 try {
81 }
82 } catch (...) {
84 std::current_exception(),
85 "Exception in HttpRequestContext group completion callback");
86 }
87 }
88
89 private:
91 std::exception_ptr exception,
92 const char* message) noexcept {
93 try {
94 KURLYK_HANDLE_ERROR(exception, message);
95 } catch (...) {
96 // Error reporting must not interrupt request cleanup.
97 }
98 }
99 }; // HttpRequestContext
100
101} // namespace kurlyk
102
103#endif // KURLYK_HEADER_KURLYK_HTTP_HTTP_REQUEST_MANAGER_HTTP_REQUEST_CONTEXT_HPP_INCLUDED
#define KURLYK_HANDLE_ERROR(e, msg)
std::function< void()> on_group_complete
Callback invoked after the final user callback for the group.
time_point_t start_time
Time when the request was initially created or last retried.
std::unique_ptr< HttpRequest > request
The HTTP request associated with this context.
std::atomic< bool > complete_called
True after completion callbacks have been claimed for invocation.
std::function< void()> on_complete
Callback invoked once when the request finishes (including retries).
long retry_attempt
Number of retry attempts made for this request.
std::chrono::steady_clock::time_point time_point_t
void invoke_final_callback(HttpResponsePtr response) noexcept
Invokes the final response callback, then performs idempotent request completion.
uint64_t in_flight_token
Token for sequential rate-limit tracking.
void complete() noexcept
Invokes completion callbacks exactly once. Thread-safe and idempotent.
static void report_callback_exception(std::exception_ptr exception, const char *message) noexcept
HttpResponseCallback callback
Callback function to be invoked when the request completes.
void invoke_callback(HttpResponsePtr response) noexcept
Invokes the response callback and reports callback exceptions.
HttpRequestContext(std::unique_ptr< HttpRequest > request_ptr, HttpResponseCallback callback)
Constructs a HttpRequestContext with the specified request and callback.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
std::function< void(HttpResponsePtr response)> HttpResponseCallback
Callback invoked with an HTTP response.
std::unique_ptr< HttpResponse > HttpResponsePtr
Owning pointer to an HTTP response.
Enables use of ClientError with std::error_code.