@@ -56,6 +56,7 @@ namespace fc::api::rpc {
56
56
}));
57
57
}
58
58
socket.handshake (host, target, ec);
59
+ client_data = ClientData{host, port, target, token};
59
60
if (ec) {
60
61
return ec;
61
62
}
@@ -87,10 +88,11 @@ namespace fc::api::rpc {
87
88
}
88
89
}
89
90
chans.clear ();
91
+ reconnect (3 , std::chrono::seconds (5 ));
90
92
}
91
93
92
94
void Client::_flush () {
93
- if (!writing && !write_queue.empty ()) {
95
+ if (!writing && !write_queue.empty () && !reconnecting) {
94
96
auto &[id, buffer] = write_queue.front ();
95
97
writing = true ;
96
98
socket.async_write (boost::asio::buffer (buffer.data (), buffer.size ()),
@@ -185,4 +187,22 @@ namespace fc::api::rpc {
185
187
}
186
188
}
187
189
}
190
+
191
+ void Client::reconnect (int counter, std::chrono::milliseconds wait) {
192
+ if (reconnecting.exchange (true )) return ;
193
+ logger_->info (" Starting reconnect to {}:{}" , client_data.host , client_data.port );
194
+ for (int i = 0 ; i < counter; i++){
195
+ std::this_thread::sleep_for (wait *(i+1 ));
196
+ auto res = connect (client_data.host ,
197
+ client_data.port ,
198
+ client_data.target ,
199
+ client_data.token );
200
+ if (!res.has_error ()) {
201
+ break ;
202
+ }
203
+ }
204
+ reconnecting.store (false );
205
+ logger_->info (" Reconnect to {}:{} was successful" , client_data.host , client_data.port );
206
+ _flush ();
207
+ }
188
208
} // namespace fc::api::rpc
0 commit comments