Skip to content

Commit

Permalink
Move log_context to ResultData (#1577)
Browse files Browse the repository at this point in the history
Required to cover the completion callback

Relates-To: HERESUP-4906

Signed-off-by: Mykhailo Kuchma <[email protected]>
  • Loading branch information
mykhailo-kuchma authored Jan 8, 2025
1 parent bfd15ff commit c2e791c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 13 additions & 9 deletions olp-cpp-sdk-core/src/http/winhttp/NetworkWinHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,6 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,

auto* handle = reinterpret_cast<RequestData*>(context);

logging::ScopedLogContext scope(handle->log_context);

if (!handle->connection_data || !handle->result_data) {
OLP_SDK_LOG_WARNING(kLogTag, "RequestCallback to inactive handle, id="
<< handle->request_id);
Expand All @@ -633,6 +631,8 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
ResultData& request_result = *handle->result_data;
handle->connection_data->last_used = GetTickCount64();

logging::ScopedLogContext scope(request_result.log_context);

if (status == WINHTTP_CALLBACK_STATUS_REQUEST_ERROR) {
// Error has occurred
auto* result = reinterpret_cast<WINHTTP_ASYNC_RESULT*>(status_info);
Expand Down Expand Up @@ -972,6 +972,9 @@ void NetworkWinHttp::CompletionThread() {
// protect against multiple calls
std::swap(result->user_callback, callback);
}

logging::ScopedLogContext scope(result->log_context);

// must call outside lock to prevent deadlock
callback(NetworkResponse()
.WithError(str)
Expand Down Expand Up @@ -1049,8 +1052,9 @@ NetworkWinHttp::RequestData* NetworkWinHttp::FindHandle(RequestId id) {
return nullptr;
}

NetworkWinHttp::ResultData::ResultData(RequestId id, Callback callback,
std::shared_ptr<std::ostream> payload)
NetworkWinHttp::ResultData::ResultData(
RequestId id, Callback callback, std::shared_ptr<std::ostream> payload,
std::shared_ptr<const logging::LogContext> context)
: user_callback(std::move(callback)),
payload(std::move(payload)),
content_length(0),
Expand All @@ -1061,7 +1065,8 @@ NetworkWinHttp::ResultData::ResultData(RequestId id, Callback callback,
completed(false),
error(false),
bytes_uploaded(0),
bytes_downloaded(0) {}
bytes_downloaded(0),
log_context(std::move(context)) {}

NetworkWinHttp::ConnectionData::ConnectionData(HINTERNET http_connection)
: http_connection(http_connection) {}
Expand All @@ -1081,8 +1086,8 @@ NetworkWinHttp::RequestData::RequestData(
std::shared_ptr<const logging::LogContext> context)
: self(self),
connection_data(std::move(connection)),
result_data(std::make_shared<ResultData>(id, std::move(callback),
std::move(payload))),
result_data(std::make_shared<ResultData>(
id, std::move(callback), std::move(payload), std::move(context))),
body(request.GetBody()),
header_callback(std::move(header_callback)),
data_callback(std::move(data_callback)),
Expand All @@ -1091,8 +1096,7 @@ NetworkWinHttp::RequestData::RequestData(
ignore_data(request.GetVerb() == NetworkRequest::HttpVerb::HEAD),
no_compression(false),
uncompress(false),
in_use(false),
log_context(std::move(context)) {}
in_use(false) {}

NetworkWinHttp::RequestData::RequestData()
: self(nullptr),
Expand Down
6 changes: 4 additions & 2 deletions olp-cpp-sdk-core/src/http/winhttp/NetworkWinHttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class NetworkWinHttp : public Network {
private:
struct ResultData {
ResultData(RequestId id, Callback callback,
std::shared_ptr<std::ostream> payload);
std::shared_ptr<std::ostream> payload,
std::shared_ptr<const logging::LogContext> context);

Callback user_callback;
std::shared_ptr<std::ostream> payload;
Expand All @@ -77,6 +78,8 @@ class NetworkWinHttp : public Network {

std::uint64_t bytes_uploaded;
std::uint64_t bytes_downloaded;

std::shared_ptr<const logging::LogContext> log_context;
};

struct ConnectionData {
Expand Down Expand Up @@ -118,7 +121,6 @@ class NetworkWinHttp : public Network {
bool no_compression;
bool uncompress;
bool in_use;
std::shared_ptr<const logging::LogContext> log_context;
#ifdef NETWORK_HAS_ZLIB
z_stream strm;
#endif
Expand Down

0 comments on commit c2e791c

Please sign in to comment.