@@ -83,16 +83,14 @@ class FoxyClient : public Client,
83
83
logger_(std::move(logger)),
84
84
errors_(std::move(errors)),
85
85
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),
90
87
last_event_id_(std::nullopt),
91
88
backoff_(
92
89
initial_reconnect_delay.value_or(kDefaultInitialReconnectDelay ),
93
90
kDefaultMaxBackoffDelay),
94
- backoff_timer_(session_.get_executor( )),
91
+ backoff_timer_(std::move(executor )),
95
92
last_read_(std::nullopt) {
93
+ create_session ();
96
94
create_parser ();
97
95
}
98
96
@@ -133,6 +131,13 @@ class FoxyClient : public Client,
133
131
body_parser_->get ().body ().on_event (event_receiver_);
134
132
}
135
133
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
+
136
141
/* *
137
142
* Called whenever the connection needs to be reattempted, triggering
138
143
* a timed wait for the current backoff duration.
@@ -158,6 +163,7 @@ class FoxyClient : public Client,
158
163
159
164
logger_ (msg.str ());
160
165
166
+ create_session ();
161
167
create_parser ();
162
168
backoff_timer_.expires_from_now (backoff_.delay ());
163
169
backoff_timer_.async_wait (beast::bind_front_handler (
@@ -172,7 +178,7 @@ class FoxyClient : public Client,
172
178
}
173
179
174
180
void run () override {
175
- session_. async_connect (
181
+ session_-> async_connect (
176
182
host_, port_,
177
183
beast::bind_front_handler (&FoxyClient::on_connect,
178
184
shared_from_this ()));
@@ -191,10 +197,10 @@ class FoxyClient : public Client,
191
197
} else {
192
198
req_.erase (" last-event-id" );
193
199
}
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 ()));
198
204
}
199
205
200
206
void on_write (boost::system::error_code ec, std::size_t amount) {
@@ -206,8 +212,8 @@ class FoxyClient : public Client,
206
212
return do_backoff (ec.what ());
207
213
}
208
214
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 (
211
217
*body_parser_, beast::bind_front_handler (&FoxyClient::on_headers,
212
218
shared_from_this ()));
213
219
}
@@ -223,7 +229,7 @@ class FoxyClient : public Client,
223
229
224
230
if (!body_parser_->is_header_done ()) {
225
231
/* keep reading headers */
226
- return session_. async_read_header (
232
+ return session_-> async_read_header (
227
233
*body_parser_,
228
234
beast::bind_front_handler (&FoxyClient::on_headers,
229
235
shared_from_this ()));
@@ -242,10 +248,11 @@ class FoxyClient : public Client,
242
248
return do_backoff (" invalid Content-Type" );
243
249
}
244
250
251
+ logger_ (" connected" );
245
252
backoff_.succeed ();
246
253
247
254
last_read_ = std::chrono::steady_clock::now ();
248
- return session_. async_read_some (
255
+ return session_-> async_read_some (
249
256
*body_parser_,
250
257
beast::bind_front_handler (&FoxyClient::on_read_body,
251
258
shared_from_this ()));
@@ -292,8 +299,8 @@ class FoxyClient : public Client,
292
299
void on_read_body (boost::system::error_code ec, std::size_t amount) {
293
300
boost::ignore_unused (amount);
294
301
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) " ) ;
297
304
}
298
305
if (body_parser_->is_done ()) {
299
306
// The server can indicate that the chunk encoded response is done
@@ -303,7 +310,7 @@ class FoxyClient : public Client,
303
310
}
304
311
if (!ec) {
305
312
log_and_update_last_read (amount);
306
- return session_. async_read_some (
313
+ return session_-> async_read_some (
307
314
*body_parser_,
308
315
beast::bind_front_handler (&FoxyClient::on_read_body,
309
316
shared_from_this ()));
@@ -312,15 +319,15 @@ class FoxyClient : public Client,
312
319
}
313
320
314
321
void async_shutdown (std::function<void ()> completion) override {
315
- boost::asio::post (session_. get_executor (),
322
+ boost::asio::post (session_-> get_executor (),
316
323
beast::bind_front_handler (&FoxyClient::do_shutdown,
317
324
shared_from_this (),
318
325
std::move (completion)));
319
326
}
320
327
321
328
void do_shutdown (std::function<void ()> completion) {
322
329
backoff_timer_.cancel ();
323
- session_. async_shutdown (beast::bind_front_handler (
330
+ session_-> async_shutdown (beast::bind_front_handler (
324
331
&FoxyClient::on_shutdown, std::move (completion)));
325
332
}
326
333
@@ -389,7 +396,7 @@ class FoxyClient : public Client,
389
396
Builder::ErrorCallback errors_;
390
397
391
398
std::optional<http::response_parser<body>> body_parser_;
392
- launchdarkly::foxy::client_session session_;
399
+ std::optional< launchdarkly::foxy::client_session> session_;
393
400
std::optional<std::string> last_event_id_;
394
401
Backoff backoff_;
395
402
boost::asio::steady_timer backoff_timer_;
0 commit comments