Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curl minutiae #65

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/datadog/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class CurlImpl {
};

void run();
void handle_message(const CURLMsg &);
void handle_message(const CURLMsg &, std::unique_lock<std::mutex> &);
CURLcode log_on_error(CURLcode result);
CURLMcode log_on_error(CURLMcode result);

Expand Down Expand Up @@ -476,7 +476,7 @@ void CurlImpl::run() {
// us to handle. Handle any pending messages.
while ((message = curl_.multi_info_read(multi_handle_,
&num_messages_remaining))) {
handle_message(*message);
handle_message(*message, lock);
}

const int max_wait_milliseconds = 10 * 1000;
Expand Down Expand Up @@ -514,7 +514,8 @@ void CurlImpl::run() {
curl_.global_cleanup();
}

void CurlImpl::handle_message(const CURLMsg &message) {
void CurlImpl::handle_message(const CURLMsg &message,
std::unique_lock<std::mutex> &lock) {
if (message.msg != CURLMSG_DONE) {
return;
}
Expand All @@ -536,17 +537,21 @@ void CurlImpl::handle_message(const CURLMsg &message) {
error_message += curl_.easy_strerror(result);
error_message += "): ";
error_message += request.error_buffer;
lock.unlock();
request.on_error(
Error{Error::CURL_REQUEST_FAILURE, std::move(error_message)});
lock.lock();
} else {
long status;
if (log_on_error(curl_.easy_getinfo_response_code(request_handle,
&status)) != CURLE_OK) {
status = -1;
}
HeaderReader reader(&request.response_headers_lower);
lock.unlock();
request.on_response(static_cast<int>(status), reader,
std::move(request.response_body));
lock.lock();
}

log_on_error(curl_.multi_remove_handle(multi_handle_, request_handle));
Expand Down
3 changes: 2 additions & 1 deletion src/datadog/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class HTTPClient {
// a response is delivered (even if that response contains an error HTTP
// response status). Invoke the specified `on_error` if an error occurs
// outside of HTTP, such as a connection failure. If an error occurs while
// preparing the request, return an `Error`.
// preparing the request, return an `Error`. The behavior is undefined if
// either of `on_response` or `on_error` throws an exception.
virtual Expected<void> post(const URL& url, HeadersSetter set_headers,
std::string body, ResponseHandler on_response,
ErrorHandler on_error) = 0;
Expand Down
2 changes: 1 addition & 1 deletion test/test_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ TEST_CASE("128-bit trace IDs") {
// Use a clock that always returns a hard-coded `TimePoint`.
// May 6, 2010 14:45:13 America/New_York
const std::time_t flash_crash = 1273171513;
const Clock clock = [flash_crash]() {
const Clock clock = []() {
TimePoint result;
result.wall = std::chrono::system_clock::from_time_t(flash_crash);
return result;
Expand Down
2 changes: 1 addition & 1 deletion test/test_tracer_telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace datadog::tracing;

TEST_CASE("Tracer telemetry") {
const std::time_t mock_time = 1672484400;
const Clock clock = [mock_time]() {
const Clock clock = []() {
TimePoint result;
result.wall = std::chrono::system_clock::from_time_t(mock_time);
return result;
Expand Down