Skip to content

Commit a1473e6

Browse files
committed
unclean shutdown works
1 parent 2dbdd29 commit a1473e6

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

libs/client-sdk/src/client_impl.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,14 @@ std::future<bool> ClientImpl::IdentifyAsync(Context context) {
149149
}
150150

151151
void ClientImpl::RestartDataSource() {
152-
if (data_source_) {
153-
auto then = std::chrono::steady_clock::now();
154-
data_source_->ShutdownAsync([then]() {
155-
std::cout << "Shutdown in "
156-
<< std::chrono::duration_cast<std::chrono::milliseconds>(
157-
std::chrono::steady_clock::now() - then)
158-
.count()
159-
<< "ms" << std::endl;
160-
});
152+
auto start_op = [this]() {
153+
data_source_ = data_source_factory_();
154+
data_source_->Start();
155+
};
156+
if (!data_source_) {
157+
return start_op();
161158
}
162-
data_source_ = data_source_factory_();
163-
data_source_->Start();
159+
data_source_->ShutdownAsync(start_op);
164160
}
165161

166162
std::future<bool> ClientImpl::StartAsyncInternal(
@@ -174,7 +170,7 @@ std::future<bool> ClientImpl::StartAsyncInternal(
174170
if (auto const state = status.State(); IsInitialized(state)) {
175171
init_promise->set_value(result(status.State()));
176172
return true; /* delete this change listener since the desired
177-
state was reached */
173+
state was reached */
178174
}
179175
return false; /* keep the change listener */
180176
});

libs/client-sdk/src/data_sources/streaming_data_source.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <boost/json.hpp>
1313
#include <boost/url.hpp>
1414

15-
#include <iostream>
1615
#include <utility>
1716

1817
namespace launchdarkly::client_side::data_sources {

libs/server-sent-events/src/client.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class FoxyClient : public Client,
5858
using cb = std::function<void(launchdarkly::sse::Event)>;
5959
using body = launchdarkly::sse::detail::EventBody<cb>;
6060
using response = http::response<body>;
61-
int count;
6261

6362
public:
6463
FoxyClient(boost::asio::any_io_executor executor,
@@ -91,8 +90,7 @@ class FoxyClient : public Client,
9190
kDefaultMaxBackoffDelay),
9291
backoff_timer_(std::move(executor)),
9392
last_read_(std::nullopt),
94-
shutting_down_(false),
95-
count(get_counter()) {
93+
shutting_down_(false) {
9694
create_session();
9795
create_parser();
9896
}
@@ -282,17 +280,10 @@ class FoxyClient : public Client,
282280
void on_read_body(boost::system::error_code ec, std::size_t amount) {
283281
boost::ignore_unused(amount);
284282
if (ec) {
283+
if (shutting_down_) {
284+
return;
285+
}
285286
if (ec == boost::asio::error::operation_aborted) {
286-
// operation_aborted can occur if the read timeout is reached or
287-
// if we're shutting down, so shutting_down_ is needed to
288-
// disambiguate.
289-
if (shutting_down_) {
290-
boost::system::error_code ec = {};
291-
session_->stream.plain().shutdown(
292-
boost::asio::ip::tcp::socket::shutdown_both, ec);
293-
session_->stream.plain().close(ec);
294-
return;
295-
}
296287
errors_(Error::ReadTimeout);
297288
return async_backoff(
298289
"aborting read of response body (timeout)");
@@ -344,18 +335,17 @@ class FoxyClient : public Client,
344335
}
345336

346337
void do_shutdown(std::function<void()> completion) {
338+
// Signal to the body reader operation that if it completes,
339+
// it should return instead of starting another async read.
347340
shutting_down_ = true;
341+
// If any backoff is taking place, cancel that as well.
348342
backoff_timer_.cancel();
349-
if (session_->stream.is_ssl()) {
350-
session_->stream.ssl().next_layer().cancel();
351-
} else {
352-
session_->stream.plain().cancel();
353-
}
354-
}
343+
344+
boost::system::error_code ec = {};
345+
session_->stream.plain().shutdown(
346+
boost::asio::ip::tcp::socket::shutdown_both, ec);
347+
session_->stream.plain().close(ec);
355348

356-
void on_shutdown(std::function<void()> completion,
357-
boost::system::error_code ec) {
358-
boost::ignore_unused(ec);
359349
if (completion) {
360350
completion();
361351
}

0 commit comments

Comments
 (0)