Skip to content

Commit 16b8cd5

Browse files
committed
recoonect fix
Signed-off-by: elestrias <[email protected]>
1 parent 59e534c commit 16b8cd5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

core/api/rpc/wsc.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ namespace fc::api::rpc {
4242
const std::string &target,
4343
const std::string &token) {
4444
boost::system::error_code ec;
45-
socket.next_layer().connect({boost::asio::ip::make_address(host),
45+
socket->next_layer().connect({boost::asio::ip::make_address(host),
4646
boost::lexical_cast<uint16_t>(port)},
4747
ec);
4848
if (ec) {
4949
return ec;
5050
}
5151
if (not token.empty()) {
52-
socket.set_option(
52+
socket->set_option(
5353
boost::beast::websocket::stream_base::decorator([&](auto &req) {
5454
req.set(boost::beast::http::field::authorization,
5555
"Bearer " + token);
5656
}));
5757
}
58-
socket.handshake(host, target, ec);
58+
socket->handshake(host, target, ec);
5959
client_data = ClientData{host, port, target, token};
6060
if (ec) {
6161
return ec;
@@ -92,10 +92,10 @@ namespace fc::api::rpc {
9292
}
9393

9494
void Client::_flush() {
95-
if (!writing && !write_queue.empty() && !reconnecting){
95+
if (!writing && !write_queue.empty() && not reconnecting){
9696
auto &[id, buffer] = write_queue.front();
9797
writing = true;
98-
socket.async_write(boost::asio::buffer(buffer.data(), buffer.size()),
98+
socket->async_write(boost::asio::buffer(buffer.data(), buffer.size()),
9999
[=](auto &&ec, auto) {
100100
std::lock_guard lock{mutex};
101101
if (ec) {
@@ -109,7 +109,7 @@ namespace fc::api::rpc {
109109
}
110110

111111
void Client::_read() {
112-
socket.async_read(buffer, [=](auto &&ec, auto) {
112+
socket->async_read(buffer, [=](auto &&ec, auto) {
113113
if (ec) {
114114
std::lock_guard lock{mutex};
115115
return _error(ec);
@@ -189,10 +189,13 @@ namespace fc::api::rpc {
189189
}
190190

191191
void Client::reconnect(int counter, std::chrono::milliseconds wait) {
192-
if(reconnecting.exchange(true)) return;
192+
if(reconnecting) return;
193+
reconnecting = true;
193194
logger_->info("Starting reconnect to {}:{}", client_data.host, client_data.port);
194195
for(int i = 0; i < counter; i++){
195-
std::this_thread::sleep_for(wait*(i+1));
196+
std::this_thread::sleep_for(wait);
197+
socket.reset();
198+
socket.emplace(io);
196199
auto res = connect(client_data.host,
197200
client_data.port,
198201
client_data.target,
@@ -201,7 +204,7 @@ namespace fc::api::rpc {
201204
break;
202205
}
203206
}
204-
reconnecting.store(false);
207+
reconnecting = false;
205208
logger_->info("Reconnect to {}:{} was successful", client_data.host, client_data.port);
206209
_flush();
207210
}

core/api/rpc/wsc.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ namespace fc::api::rpc {
7676
io_context io;
7777
io_context &io2;
7878
boost::asio::executor_work_guard<io_context::executor_type> work_guard;
79-
boost::beast::websocket::stream<boost::asio::ip::tcp::socket> socket;
79+
boost::optional<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>> socket;
8080
boost::beast::flat_buffer buffer;
8181
std::mutex mutex;
8282
uint64_t next_req{};
8383
std::map<uint64_t, ResultCb> result_queue;
8484
std::map<uint64_t, ChanCb> chans;
8585
std::queue<std::pair<uint64_t, Bytes>> write_queue;
8686
bool writing{false};
87-
std::atomic<bool> reconnecting;
87+
bool reconnecting{false};
8888

8989
template <typename M>
9090
void _setup(Client &c, M &m);

0 commit comments

Comments
 (0)