Skip to content

Commit 2dbdd29

Browse files
committed
shutdown plain socket
1 parent e8d3a71 commit 2dbdd29

File tree

3 files changed

+42
-66
lines changed

3 files changed

+42
-66
lines changed

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

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

15+
#include <iostream>
1516
#include <utility>
1617

1718
namespace launchdarkly::client_side::data_sources {

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ auto const kDefaultInitialReconnectDelay = std::chrono::seconds(1);
4444
// Maximum duration between backoff attempts.
4545
auto const kDefaultMaxBackoffDelay = std::chrono::seconds(30);
4646

47-
// When we shut down the SSL stream using async_shutdown operation, it
48-
// appears that the completion handler isn't invoked for about 5 minutes. Either
49-
// we or the server are misbehaving here. In any case, there is no need to wait
50-
// 5 minutes, we should give a couple seconds to receive a valid response or
51-
// else stop waiting.
52-
auto const kShutdownTimeout = std::chrono::seconds(10);
53-
5447
static boost::optional<net::ssl::context&> ToOptRef(
5548
std::optional<net::ssl::context>& maybe_val) {
5649
if (maybe_val) {
@@ -65,6 +58,7 @@ class FoxyClient : public Client,
6558
using cb = std::function<void(launchdarkly::sse::Event)>;
6659
using body = launchdarkly::sse::detail::EventBody<cb>;
6760
using response = http::response<body>;
61+
int count;
6862

6963
public:
7064
FoxyClient(boost::asio::any_io_executor executor,
@@ -97,7 +91,8 @@ class FoxyClient : public Client,
9791
kDefaultMaxBackoffDelay),
9892
backoff_timer_(std::move(executor)),
9993
last_read_(std::nullopt),
100-
shutting_down_(false) {
94+
shutting_down_(false),
95+
count(get_counter()) {
10196
create_session();
10297
create_parser();
10398
}
@@ -292,6 +287,10 @@ class FoxyClient : public Client,
292287
// if we're shutting down, so shutting_down_ is needed to
293288
// disambiguate.
294289
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);
295294
return;
296295
}
297296
errors_(Error::ReadTimeout);
@@ -336,7 +335,6 @@ class FoxyClient : public Client,
336335
}
337336

338337
void async_shutdown(std::function<void()> completion) override {
339-
std::cout << "shutdown requested, posting..\n";
340338
// Get on the session's executor, otherwise the code in the completion
341339
// handler could race.
342340
boost::asio::post(session_->get_executor(),
@@ -345,25 +343,14 @@ class FoxyClient : public Client,
345343
std::move(completion)));
346344
}
347345

348-
void on_shutdown_write() { std::cout << "shutdown write completed\n"; }
349346
void do_shutdown(std::function<void()> completion) {
350-
std::cout << "shutdown request executing..\n";
351347
shutting_down_ = true;
352348
backoff_timer_.cancel();
353349
if (session_->stream.is_ssl()) {
354350
session_->stream.ssl().next_layer().cancel();
355351
} else {
356352
session_->stream.plain().cancel();
357353
}
358-
session_->opts.timeout = kShutdownTimeout;
359-
session_->async_shutdown(beast::bind_front_handler(
360-
&FoxyClient::on_shutdown, shared_from_this(),
361-
std::move(completion)));
362-
// Run async_write with a single null byte:
363-
session_->stream.async_write_some(
364-
net::buffer("\0", 1),
365-
beast::bind_front_handler(&FoxyClient::on_shutdown_write,
366-
shared_from_this()));
367354
}
368355

369356
void on_shutdown(std::function<void()> completion,
Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//
2-
// Copyright (c) 2018-2019 Christian Mazakas (christian dot mazakas at gmail dot
3-
// com)
2+
// Copyright (c) 2018-2019 Christian Mazakas (christian dot mazakas at gmail dot com)
43
//
5-
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6-
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
5+
// or copy at http://www.boost.org/LICENSE_1_0.txt)
76
//
87
// Official repository: https://github.com/LeonineKing1199/foxy
98
//
@@ -12,50 +11,39 @@
1211
#define FOXY_IMPL_CLIENT_SESSION_ASYNC_SHUTDOWN_IMPL_HPP_
1312

1413
#include <foxy/client_session.hpp>
15-
#include <iostream>
16-
namespace launchdarkly::foxy {
14+
15+
namespace launchdarkly::foxy
16+
{
1717
template <class DynamicBuffer>
1818
template <class ShutdownHandler>
19-
auto basic_client_session<DynamicBuffer>::async_shutdown(
20-
ShutdownHandler&& handler) & ->
21-
typename boost::asio::async_result<
22-
std::decay_t<ShutdownHandler>,
23-
void(boost::system::error_code)>::return_type {
24-
return ::launchdarkly::foxy::detail::async_timer<void(
25-
boost::system::error_code)>(
26-
[self = this, coro = boost::asio::coroutine()](
27-
auto& cb, boost::system::error_code ec = {},
28-
std::size_t bytes_transferrred = 0) mutable {
29-
auto& s = *self;
30-
31-
BOOST_ASIO_CORO_REENTER(coro) {
32-
if (s.stream.is_ssl()) {
33-
std::cout << "-> ssl().async_shutdown()\n";
34-
BOOST_ASIO_CORO_YIELD s.stream.ssl().async_shutdown(
35-
std::move(cb));
36-
std::cout << "<- ssl().async_shutdown: " << ec.message()
37-
<< '\n';
38-
if (ec == boost::asio::ssl::error::stream_truncated) {
39-
ec = {};
40-
}
41-
if (ec) {
42-
goto upcall;
43-
}
44-
}
45-
46-
std::cout << "plain socket shutdown\n";
47-
s.stream.plain().shutdown(
48-
boost::asio::ip::tcp::socket::shutdown_both, ec);
49-
s.stream.plain().close(ec);
50-
51-
upcall:
52-
std::cout << "invoke completion handler\n";
53-
return cb.complete(ec);
54-
}
55-
},
56-
*this, std::forward<ShutdownHandler>(handler));
19+
auto
20+
basic_client_session<DynamicBuffer>::async_shutdown(ShutdownHandler&& handler) & ->
21+
typename boost::asio::async_result<std::decay_t<ShutdownHandler>,
22+
void(boost::system::error_code)>::return_type
23+
{
24+
return ::launchdarkly::foxy::detail::async_timer<void(boost::system::error_code)>(
25+
[self = this, coro = boost::asio::coroutine()](auto& cb, boost::system::error_code ec = {},
26+
std::size_t bytes_transferrred = 0) mutable {
27+
auto& s = *self;
28+
29+
BOOST_ASIO_CORO_REENTER(coro)
30+
{
31+
if (s.stream.is_ssl()) {
32+
BOOST_ASIO_CORO_YIELD s.stream.ssl().async_shutdown(std::move(cb));
33+
if (ec == boost::asio::ssl::error::stream_truncated) { ec = {}; }
34+
if (ec) { goto upcall; }
35+
}
36+
37+
s.stream.plain().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
38+
s.stream.plain().close(ec);
39+
40+
upcall:
41+
return cb.complete(ec);
42+
}
43+
},
44+
*this, std::forward<ShutdownHandler>(handler));
5745
}
5846

59-
} // namespace launchdarkly::foxy
47+
} // namespace launchdarkly::foxy
6048

61-
#endif // FOXY_IMPL_CLIENT_SESSION_ASYNC_SHUTDOWN_IMPL_HPP_
49+
#endif // FOXY_IMPL_CLIENT_SESSION_ASYNC_SHUTDOWN_IMPL_HPP_

0 commit comments

Comments
 (0)