Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <glog/logging.h>
#include <cerrno>
#include <chrono>
#include <exception>
#include <utility>

using namespace std::literals;
Expand All @@ -22,6 +23,7 @@ namespace facebook::react::jsinspector_modern {

static constexpr const std::chrono::duration RECONNECT_DELAY =
std::chrono::milliseconds{2000};
static constexpr const std::chrono::milliseconds MAX_RECONNECT_DELAY{120000};
static constexpr const char* INVALID = "<invalid>";

// InspectorPackagerConnection::Impl method definitions
Expand Down Expand Up @@ -301,6 +303,8 @@ void InspectorPackagerConnection::Impl::didReceiveMessage(

void InspectorPackagerConnection::Impl::didOpen() {
connected_ = true;
reconnectDelayMs_ = RECONNECT_DELAY;
suppressConnectionErrors_ = false;
}

void InspectorPackagerConnection::Impl::didClose() {
Expand Down Expand Up @@ -333,7 +337,15 @@ void InspectorPackagerConnection::Impl::connect() {
<< "Illegal state: Can't connect after having previously been closed.";
return;
}
webSocket_ = delegate_->connectWebSocket(url_, weak_from_this());
try {
webSocket_ = delegate_->connectWebSocket(url_, weak_from_this());
} catch (const std::exception& e) {
LOG(ERROR) << "Failed to create WebSocket connection: " << e.what();
reconnect();
} catch (...) {
LOG(ERROR) << "Failed to create WebSocket connection: unknown error";
reconnect();
}
}

void InspectorPackagerConnection::Impl::reconnect() {
Expand All @@ -357,6 +369,11 @@ void InspectorPackagerConnection::Impl::reconnect() {

reconnectPending_ = true;

auto currentDelay = reconnectDelayMs_;

// Exponential backoff: double the delay for next time, up to the max.
reconnectDelayMs_ = std::min(reconnectDelayMs_ * 2, MAX_RECONNECT_DELAY);

delegate_->scheduleCallback(
[weakSelf = weak_from_this()] {
auto strongSelf = weakSelf.lock();
Expand All @@ -370,7 +387,7 @@ void InspectorPackagerConnection::Impl::reconnect() {
strongSelf->connect();
}
},
RECONNECT_DELAY);
currentDelay);
}

void InspectorPackagerConnection::Impl::closeQuietly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class InspectorPackagerConnection::Impl : public IWebSocketDelegate,
// Whether a reconnection is currently pending.
bool reconnectPending_{false};

// Current reconnect delay in milliseconds, used for exponential backoff.
std::chrono::milliseconds reconnectDelayMs_{2000};

SessionId nextSessionId_{1};
};

Expand Down
Loading