Skip to content

Commit 3782ee9

Browse files
committed
Remove debug logging.
1 parent 417b1de commit 3782ee9

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

libs/networking/src/curl_multi_manager.cpp

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#include <boost/asio/post.hpp>
66

7-
#include <iostream>
8-
97
namespace launchdarkly::network {
108
std::shared_ptr<CurlMultiManager> CurlMultiManager::create(
119
boost::asio::any_io_executor executor) {
@@ -49,7 +47,8 @@ CurlMultiManager::~CurlMultiManager() {
4947
void CurlMultiManager::add_handle(const std::shared_ptr<CURL>& easy,
5048
curl_slist* headers,
5149
CompletionCallback callback,
52-
std::optional<std::chrono::milliseconds> read_timeout) {
50+
std::optional<std::chrono::milliseconds>
51+
read_timeout) {
5352
if (const CURLMcode rc = curl_multi_add_handle(
5453
multi_handle_.get(), easy.get());
5554
rc != CURLM_OK) {
@@ -58,8 +57,6 @@ void CurlMultiManager::add_handle(const std::shared_ptr<CURL>& easy,
5857
curl_slist_free_all(headers);
5958
}
6059

61-
std::cerr << "Failed to add handle to multi: "
62-
<< curl_multi_strerror(rc) << std::endl;
6360
return;
6461
}
6562

@@ -72,19 +69,21 @@ void CurlMultiManager::add_handle(const std::shared_ptr<CURL>& easy,
7269
// Setup read timeout timer if specified
7370
if (read_timeout) {
7471
auto timer = std::make_shared<boost::asio::steady_timer>(executor_);
75-
handle_timeouts_[easy.get()] = HandleTimeoutInfo{read_timeout, timer};
72+
handle_timeouts_[easy.get()] = HandleTimeoutInfo{
73+
read_timeout, timer};
7674

7775
// Start the timeout timer
7876
timer->expires_after(*read_timeout);
7977
auto weak_self = weak_from_this();
8078
CURL* easy_ptr = easy.get();
81-
timer->async_wait([weak_self, easy_ptr](const boost::system::error_code& ec) {
82-
if (!ec) {
83-
if (auto self = weak_self.lock()) {
84-
self->handle_read_timeout(easy_ptr);
79+
timer->async_wait(
80+
[weak_self, easy_ptr](const boost::system::error_code& ec) {
81+
if (!ec) {
82+
if (auto self = weak_self.lock()) {
83+
self->handle_read_timeout(easy_ptr);
84+
}
8585
}
86-
}
87-
});
86+
});
8887
}
8988
}
9089
}
@@ -153,14 +152,11 @@ int CurlMultiManager::timer_callback(CURLM* multi,
153152
void CurlMultiManager::handle_socket_action(curl_socket_t s,
154153
const int event_bitmask) {
155154
int running_handles = 0;
156-
const CURLMcode rc = curl_multi_socket_action(multi_handle_.get(), s,
157-
event_bitmask,
158-
&running_handles);
159-
160-
if (rc != CURLM_OK) {
161-
std::cerr << "curl_multi_socket_action failed: "
162-
<< curl_multi_strerror(rc) << std::endl;
163-
}
155+
// This can return an error code, but checking the multi_info will be
156+
// sufficient without additional handling for this error code.
157+
curl_multi_socket_action(multi_handle_.get(), s,
158+
event_bitmask,
159+
&running_handles);
164160

165161
check_multi_info();
166162

@@ -191,7 +187,6 @@ void CurlMultiManager::check_multi_info() {
191187
callback = std::move(it->second);
192188
callbacks_.erase(it);
193189
}
194-
callbacks_.erase(easy);
195190

196191
// Get and remove headers
197192
if (auto header_it = headers_.find(easy);
@@ -226,7 +221,8 @@ void CurlMultiManager::check_multi_info() {
226221
if (callback) {
227222
boost::asio::post(executor_, [callback = std::move(callback),
228223
result, handle]() {
229-
callback(handle, Result::FromCurlCode(result));
224+
callback(
225+
handle, Result::FromCurlCode(result));
230226
});
231227
}
232228
}
@@ -247,8 +243,6 @@ void CurlMultiManager::start_socket_monitor(SocketInfo* socket_info,
247243
socket_info->sockfd, ec);
248244

249245
if (ec) {
250-
std::cerr << "Failed to assign socket: " << ec.message() <<
251-
std::endl;
252246
socket_info->handle.reset();
253247
return;
254248
}
@@ -368,13 +362,14 @@ void CurlMultiManager::reset_read_timeout(CURL* easy) {
368362

369363
auto weak_self = weak_from_this();
370364
CURL* easy_ptr = easy;
371-
timeout_info.timer->async_wait([weak_self, easy_ptr](const boost::system::error_code& ec) {
372-
if (!ec) {
373-
if (auto self = weak_self.lock()) {
374-
self->handle_read_timeout(easy_ptr);
365+
timeout_info.timer->async_wait(
366+
[weak_self, easy_ptr](const boost::system::error_code& ec) {
367+
if (!ec) {
368+
if (auto self = weak_self.lock()) {
369+
self->handle_read_timeout(easy_ptr);
370+
}
375371
}
376-
}
377-
});
372+
});
378373
}
379374
}
380375

@@ -389,7 +384,7 @@ void CurlMultiManager::handle_read_timeout(CURL* easy) {
389384
// Check if handle still exists
390385
auto it = callbacks_.find(easy);
391386
if (it == callbacks_.end()) {
392-
return; // Handle already completed
387+
return; // Handle already completed
393388
}
394389

395390
// Get and remove callback
@@ -429,9 +424,9 @@ void CurlMultiManager::handle_read_timeout(CURL* easy) {
429424
// Invoke completion callback with read timeout result
430425
if (callback) {
431426
boost::asio::post(executor_, [callback = std::move(callback),
432-
handle]() {
433-
callback(handle, Result::FromReadTimeout());
434-
});
427+
handle]() {
428+
callback(handle, Result::FromReadTimeout());
429+
});
435430
}
436431
}
437432
} // namespace launchdarkly::network

libs/server-sent-events/src/curl_client.hpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,22 @@ namespace net = boost::asio;
2727
using launchdarkly::network::CurlMultiManager;
2828

2929
/**
30-
* The lifecycle of the CurlClient is managed in an RAII manner. This introduces
31-
* some complexity with interaction with CURL, which requires a thread to be
32-
* driven. The desutruction of the CurlClient will signal that the CURL thread
33-
* should stop. Though depending on the operation it may linger for some
34-
* time.
35-
*
36-
* The CurlClient itself is not accessible from the CURL thread and instead
37-
* all their shared state is in a RequestContext. The lifetime of this context
38-
* can exceed that of the CurlClient while CURL shuts down. This approach
39-
* prevents the lifetime of the CurlClient being attached to that of the
40-
* curl thread.
30+
* The CurlClient uses the CURL multi-socket interface to allow for
31+
* single-threaded usage of CURL. We drive this usage using boost::asio
32+
* and every thing is executed in the IO context provided during client
33+
* construction. Calling into the API of the client could be done from threads
34+
* other than the IO context thread, so some thread-safety is required to
35+
* manage those interactions. For example the CurlClient destructor will
36+
* be ran on whatever thread last retains a reference to the client.
4137
*/
4238
class CurlClient final : public Client,
4339
public std::enable_shared_from_this<CurlClient> {
40+
/**
41+
* Structure containing callbacks between the CURL interactions and the
42+
* IO executor. Callbacks are set while a connection is being established,
43+
* instead of at construction time, to allow the use of weak_from_self.
44+
* The weak_from_self method cannot be used during the constructor.
45+
*/
4446
struct Callbacks {
4547
std::function<void(std::string)> do_backoff;
4648
std::function<void(Event)> on_receive;
@@ -63,6 +65,16 @@ class CurlClient final : public Client,
6365
}
6466
};
6567

68+
/**
69+
* The request context represents the state required by the executing CURL
70+
* request. Not directly including the shared data in the CurlClient allows
71+
* for easy seperation of its lifetime from that of the CURL client. This
72+
* facilitates destruction of the CurlClient being used to stop in-progress
73+
* requests.
74+
*
75+
* Also the CURL client can be destructed and pending tasks will still
76+
* have a valid RequestContext and will detect the shutdown.
77+
*/
6678
class RequestContext {
6779
// Only items used by both the curl thread and the executor/main
6880
// thread need to be mutex protected.

0 commit comments

Comments
 (0)