Skip to content

Commit 5e28860

Browse files
committed
ssl session cannot be resused for new connections
1 parent ed5ff7f commit 5e28860

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,14 @@ class FoxyClient : public Client,
8383
logger_(std::move(logger)),
8484
errors_(std::move(errors)),
8585
body_parser_(std::nullopt),
86-
session_(std::move(executor),
87-
launchdarkly::foxy::session_opts{
88-
ToOptRef(ssl_context_),
89-
connect_timeout_.value_or(kNoTimeout)}),
86+
session_(std::nullopt),
9087
last_event_id_(std::nullopt),
9188
backoff_(
9289
initial_reconnect_delay.value_or(kDefaultInitialReconnectDelay),
9390
kDefaultMaxBackoffDelay),
94-
backoff_timer_(session_.get_executor()),
91+
backoff_timer_(std::move(executor)),
9592
last_read_(std::nullopt) {
93+
create_session();
9694
create_parser();
9795
}
9896

@@ -133,6 +131,13 @@ class FoxyClient : public Client,
133131
body_parser_->get().body().on_event(event_receiver_);
134132
}
135133

134+
void create_session() {
135+
session_.emplace(
136+
backoff_timer_.get_executor(),
137+
launchdarkly::foxy::session_opts{
138+
ToOptRef(ssl_context_), connect_timeout_.value_or(kNoTimeout)});
139+
}
140+
136141
/**
137142
* Called whenever the connection needs to be reattempted, triggering
138143
* a timed wait for the current backoff duration.
@@ -158,6 +163,7 @@ class FoxyClient : public Client,
158163

159164
logger_(msg.str());
160165

166+
create_session();
161167
create_parser();
162168
backoff_timer_.expires_from_now(backoff_.delay());
163169
backoff_timer_.async_wait(beast::bind_front_handler(
@@ -172,7 +178,7 @@ class FoxyClient : public Client,
172178
}
173179

174180
void run() override {
175-
session_.async_connect(
181+
session_->async_connect(
176182
host_, port_,
177183
beast::bind_front_handler(&FoxyClient::on_connect,
178184
shared_from_this()));
@@ -191,10 +197,10 @@ class FoxyClient : public Client,
191197
} else {
192198
req_.erase("last-event-id");
193199
}
194-
session_.opts.timeout = write_timeout_.value_or(kNoTimeout);
195-
session_.async_write(req_,
196-
beast::bind_front_handler(&FoxyClient::on_write,
197-
shared_from_this()));
200+
session_->opts.timeout = write_timeout_.value_or(kNoTimeout);
201+
session_->async_write(req_,
202+
beast::bind_front_handler(&FoxyClient::on_write,
203+
shared_from_this()));
198204
}
199205

200206
void on_write(boost::system::error_code ec, std::size_t amount) {
@@ -206,8 +212,8 @@ class FoxyClient : public Client,
206212
return do_backoff(ec.what());
207213
}
208214

209-
session_.opts.timeout = read_timeout_.value_or(kNoTimeout);
210-
session_.async_read_header(
215+
session_->opts.timeout = read_timeout_.value_or(kNoTimeout);
216+
session_->async_read_header(
211217
*body_parser_, beast::bind_front_handler(&FoxyClient::on_headers,
212218
shared_from_this()));
213219
}
@@ -223,7 +229,7 @@ class FoxyClient : public Client,
223229

224230
if (!body_parser_->is_header_done()) {
225231
/* keep reading headers */
226-
return session_.async_read_header(
232+
return session_->async_read_header(
227233
*body_parser_,
228234
beast::bind_front_handler(&FoxyClient::on_headers,
229235
shared_from_this()));
@@ -242,10 +248,11 @@ class FoxyClient : public Client,
242248
return do_backoff("invalid Content-Type");
243249
}
244250

251+
logger_("connected");
245252
backoff_.succeed();
246253

247254
last_read_ = std::chrono::steady_clock::now();
248-
return session_.async_read_some(
255+
return session_->async_read_some(
249256
*body_parser_,
250257
beast::bind_front_handler(&FoxyClient::on_read_body,
251258
shared_from_this()));
@@ -292,8 +299,8 @@ class FoxyClient : public Client,
292299
void on_read_body(boost::system::error_code ec, std::size_t amount) {
293300
boost::ignore_unused(amount);
294301
if (ec == boost::asio::error::operation_aborted) {
295-
logger_("read HTTP response body aborted");
296-
return;
302+
return do_backoff(
303+
"aborting read of response body (timeout/shutdown)");
297304
}
298305
if (body_parser_->is_done()) {
299306
// The server can indicate that the chunk encoded response is done
@@ -303,7 +310,7 @@ class FoxyClient : public Client,
303310
}
304311
if (!ec) {
305312
log_and_update_last_read(amount);
306-
return session_.async_read_some(
313+
return session_->async_read_some(
307314
*body_parser_,
308315
beast::bind_front_handler(&FoxyClient::on_read_body,
309316
shared_from_this()));
@@ -312,15 +319,15 @@ class FoxyClient : public Client,
312319
}
313320

314321
void async_shutdown(std::function<void()> completion) override {
315-
boost::asio::post(session_.get_executor(),
322+
boost::asio::post(session_->get_executor(),
316323
beast::bind_front_handler(&FoxyClient::do_shutdown,
317324
shared_from_this(),
318325
std::move(completion)));
319326
}
320327

321328
void do_shutdown(std::function<void()> completion) {
322329
backoff_timer_.cancel();
323-
session_.async_shutdown(beast::bind_front_handler(
330+
session_->async_shutdown(beast::bind_front_handler(
324331
&FoxyClient::on_shutdown, std::move(completion)));
325332
}
326333

@@ -389,7 +396,7 @@ class FoxyClient : public Client,
389396
Builder::ErrorCallback errors_;
390397

391398
std::optional<http::response_parser<body>> body_parser_;
392-
launchdarkly::foxy::client_session session_;
399+
std::optional<launchdarkly::foxy::client_session> session_;
393400
std::optional<std::string> last_event_id_;
394401
Backoff backoff_;
395402
boost::asio::steady_timer backoff_timer_;

0 commit comments

Comments
 (0)