@@ -56,6 +56,7 @@ namespace fc::api::rpc {
5656 }));
5757 }
5858 socket.handshake (host, target, ec);
59+ client_data = ClientData{host, port, target, token};
5960 if (ec) {
6061 return ec;
6162 }
@@ -87,10 +88,11 @@ namespace fc::api::rpc {
8788 }
8889 }
8990 chans.clear ();
91+ reconnect (3 , std::chrono::seconds (5 ));
9092 }
9193
9294 void Client::_flush () {
93- if (!writing && !write_queue.empty ()) {
95+ if (!writing && !write_queue.empty () && !reconnecting) {
9496 auto &[id, buffer] = write_queue.front ();
9597 writing = true ;
9698 socket.async_write (boost::asio::buffer (buffer.data (), buffer.size ()),
@@ -185,4 +187,22 @@ namespace fc::api::rpc {
185187 }
186188 }
187189 }
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+ }
188208} // namespace fc::api::rpc
0 commit comments