Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8a9104a
Implement exponential backoff with jitter for RTDB Desktop
dconeybe Nov 23, 2026
79dc9a4
Address RetryHelper code review findings 6-13
dconeybe Nov 23, 2026
c621235
Clear pending token futures on interrupt and deduplicate RetryHelper …
dconeybe Nov 23, 2026
185839a
Update retry_helper_test for RetryHelper::Options, shared callback st…
dconeybe Nov 23, 2026
c05f645
Use [RetryHelper] log tag prefix in RetryHelper
dconeybe Nov 24, 2026
46aaffe
Document closed upper-bound tolerance in RetryHelper::NextRandom
dconeybe Nov 24, 2026
bdca795
Address RetryHelper code_review2 findings 10-14
dconeybe Nov 24, 2026
bd5cfb5
Use SafeReference in token callbacks, steady_clock for connection dur…
dconeybe Nov 24, 2026
6eba46a
Use RetryHelperTestPeer, reorder RetryHelperTest members, and use ste…
dconeybe Nov 24, 2026
ae1c53c
Persist desktop persistence_enabled_ across Database instances and fi…
dconeybe Nov 24, 2026
98e1cdd
Include <windows.h> in level_db_persistence_storage_engine_test.cc
dconeybe Nov 24, 2026
1d8b812
Address RetryHelper code_review3 findings 1-4, 6, and 7
dconeybe Nov 24, 2026
2485032
Harden token future locking, cancel retry unconditionally on interrup…
dconeybe Nov 24, 2026
447bb55
Replace sleeps with sentinel barriers, use atomic assertions, and fix…
dconeybe Nov 24, 2026
ec955d5
Address RetryHelper code_review4 findings
dconeybe Nov 24, 2026
a69615f
Replace boolean is_auth flag with PendingToken member pointer in Pers…
dconeybe Nov 24, 2026
4521079
Split unrelated desktop persistence and LevelDB fixes to separate PR
dconeybe Nov 24, 2026
aa69f68
Simplify jittered lower-bound check to jittered <= 0.0 in RetryHelper
dconeybe Nov 24, 2026
b062f6a
Fix clang-format spacing on PendingToken member pointer
dconeybe Nov 24, 2026
4567139
Add release note for Realtime Database Desktop exponential backoff
dconeybe Nov 24, 2026
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
1 change: 1 addition & 0 deletions database/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ set(desktop_SRCS
src/desktop/connection/connection.cc
src/desktop/connection/host_info.cc
src/desktop/connection/persistent_connection.cc
src/desktop/connection/retry_helper.cc
src/desktop/connection/util_connection.cc
src/desktop/connection/web_socket_client_impl.cc
src/desktop/core/cache_policy.cc
Expand Down
331 changes: 183 additions & 148 deletions database/src/desktop/connection/persistent_connection.cc

Large diffs are not rendered by default.

45 changes: 27 additions & 18 deletions database/src/desktop/connection/persistent_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <atomic>
#include <cassert>
#include <chrono>
#include <map>
#include <memory>
#include <queue>
Expand All @@ -37,6 +38,7 @@
#include "database/src/common/query_spec.h"
#include "database/src/desktop/connection/connection.h"
#include "database/src/desktop/connection/host_info.h"
#include "database/src/desktop/connection/retry_helper.h"
#include "database/src/desktop/core/tag.h"
#include "database/src/include/firebase/database/common.h"

Expand Down Expand Up @@ -324,29 +326,34 @@ class PersistentConnection : public ConnectionEventHandler {

bool ShouldReconnect() { return interrupt_reasons_.empty(); }

// Try to reconnect to RT DB server.
// Try to reconnect to RTDB server.
void TryScheduleReconnect();

// Callback function when the Auth future to fetch token is complete.
static void OnAuthTokenFutureComplete(const Future<std::string>& auth_future,
void* user_data);
// Callback function when the App Check future to fetch token is complete.
static void OnAppCheckTokenFutureComplete(
const Future<std::string>& app_check_future, void* user_data);
void HandleTokenFutures();
typedef firebase::internal::SafeReference<PersistentConnection> ThisRef;
typedef firebase::internal::SafeReferenceLock<PersistentConnection>
ThisRefLock;

// Mutex to protect pending_token_future_
Mutex pending_token_future_mutex_;
enum TokenFutureStatus {
kInvalidTokenFuture = 0,
kWaitingForTokenFuture,
kCompletedTokenFuture,
};
// Pending future to fetch the auth token.
Future<std::string> pending_auth_token_future_;
TokenFutureStatus auth_token_future_status_;
Future<std::string> pending_app_check_token_future_;
TokenFutureStatus app_check_token_future_status_;
struct PendingToken {
Future<std::string> future;
TokenFutureStatus status = kInvalidTokenFuture;
};

static void OnTokenFutureComplete(
ThisRef safe_ref, PendingToken PersistentConnection::*token_member,
const Future<std::string>& result_data);
void HandleTokenFutures(uint64_t get_token_attempt);
void ClearPendingTokenFutures();

// Mutex to protect pending_auth_token_ and pending_app_check_token_.
Mutex pending_token_future_mutex_;
PendingToken pending_auth_token_;
PendingToken pending_app_check_token_;
uint64_t pending_get_token_attempt_;

// Start to establish connection to RT DB server.
void OpenNetworkConnection();
Expand Down Expand Up @@ -506,9 +513,6 @@ class PersistentConnection : public ConnectionEventHandler {
// Safe reference to this. Set in constructor and cleared in destructor
// Should be safe to be copied in any thread because the std::shared_ptr never
// changes, until safe_this_ is completely destroyed.
typedef firebase::internal::SafeReference<PersistentConnection> ThisRef;
typedef firebase::internal::SafeReferenceLock<PersistentConnection>
ThisRefLock;
ThisRef safe_this_;

// Scheduler to make sure all Connection events are handled in worker
Expand All @@ -526,6 +530,9 @@ class PersistentConnection : public ConnectionEventHandler {
// States
ConnectionState connection_state_;
bool is_first_connection_;
Optional<std::chrono::steady_clock::time_point>
last_connection_established_time_;
uint64_t current_get_token_attempt_;

// Session
std::string last_session_id_;
Expand Down Expand Up @@ -575,6 +582,8 @@ class PersistentConnection : public ConnectionEventHandler {
uint64_t next_write_id_;

Logger* logger_;

RetryHelper retry_helper_;
};

class PersistentConnectionEventHandler {
Expand Down
242 changes: 242 additions & 0 deletions database/src/desktop/connection/retry_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "database/src/desktop/connection/retry_helper.h"

#include <atomic>
#include <chrono>
#include <cinttypes>
#include <cmath>
#include <cstdint>
#include <functional>
#include <random>
#include <utility>

#include "app/src/assert.h"

namespace firebase {
namespace database {
namespace internal {
namespace connection {
namespace {

// Mixes several non-blocking, non-throwing sources of entropy to seed the RNG
// for jitter calculations, which does not require cryptographic-strength
// randomness.
std::minstd_rand::result_type SeedRng(const void* instance_addr) {
static std::atomic<uint64_t> counter{0};
const uint64_t time_ticks = static_cast<uint64_t>(
std::chrono::steady_clock::now().time_since_epoch().count());
const uint64_t addr_bits =
static_cast<uint64_t>(reinterpret_cast<std::uintptr_t>(instance_addr));
const uint64_t count = counter.fetch_add(1, std::memory_order_relaxed) + 1;
uint64_t z = time_ticks ^ (addr_bits * 0x9e3779b97f4a7c15ULL) ^
(count * 0xbf58476d1ce4e5b9ULL);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
z = z ^ (z >> 31);
return static_cast<std::minstd_rand::result_type>(
(z % (std::minstd_rand::modulus - 1)) + 1);
}

} // namespace

RetryHelper::RetryHelper(scheduler::Scheduler* scheduler, Logger* logger)
: RetryHelper(scheduler, logger, Options()) {}

RetryHelper::RetryHelper(scheduler::Scheduler* scheduler, Logger* logger,
Options options)
: scheduler_(scheduler),
logger_(logger),
min_retry_delay_after_failure_ms_(
options.min_retry_delay_after_failure_ms),
max_retry_delay_ms_(options.max_retry_delay_ms),
retry_exponent_(options.retry_exponent),
jitter_factor_(options.jitter_factor),
rng_(options.random_generator ? std::minstd_rand::default_seed
: SeedRng(this)),
random_generator_(options.random_generator
? std::move(options.random_generator)
: [this]() {
std::uniform_real_distribution<double> dist(
0.0, 1.0);
return dist(rng_);
}),
safe_this_(this),
scheduled_retry_id_(0),
next_task_id_(0),
current_retry_delay_ms_(0),
last_scheduled_delay_ms_(0),
last_was_success_(true) {
FIREBASE_ASSERT(scheduler_ != nullptr);
FIREBASE_ASSERT(logger_ != nullptr);
FIREBASE_ASSERT(min_retry_delay_after_failure_ms_ <= max_retry_delay_ms_);
FIREBASE_ASSERT(std::isfinite(retry_exponent_) && retry_exponent_ >= 1.0);
FIREBASE_ASSERT(jitter_factor_ >= 0.0 && jitter_factor_ <= 1.0);
}

RetryHelper::~RetryHelper() {
// Clear SafeReference first so any callback checking/clearing state completes
// under SafeReferenceLock before this object is torn down, and any unstarted
// callback becomes a no-op.
safe_this_.ClearReference();
if (scheduled_retry_.IsValid()) {
scheduled_retry_.Cancel();
}
}

void RetryHelper::Retry(std::function<void()> callback) {
scheduler::RequestHandle previous_retry;
{
ThisRefLock lock(&safe_this_);

// Cancel the in-progress or scheduled retry, if there is one.
if (scheduled_retry_.IsValid()) {
logger_->LogDebug("[RetryHelper] Cancelling previous scheduled retry");
std::swap(previous_retry, scheduled_retry_);
scheduled_retry_id_ = 0;
}

uint64_t delay_ms = 0;
if (last_was_success_) {
delay_ms = 0;
} else {
if (current_retry_delay_ms_ == 0) {
current_retry_delay_ms_ = min_retry_delay_after_failure_ms_;
} else {
const double new_delay_double =
static_cast<double>(current_retry_delay_ms_) * retry_exponent_;
if (new_delay_double >= static_cast<double>(max_retry_delay_ms_)) {
current_retry_delay_ms_ = max_retry_delay_ms_;
} else {
current_retry_delay_ms_ = static_cast<uint64_t>(new_delay_double);
}
}
if (jitter_factor_ == 0.0 || current_retry_delay_ms_ == 0) {
delay_ms = current_retry_delay_ms_;
} else {
const double random_value = NextRandom();
const double base_double = static_cast<double>(current_retry_delay_ms_);
const double jittered = ((1.0 - jitter_factor_) * base_double) +
(jitter_factor_ * base_double * random_value);
if (jittered <= 0.0) {
delay_ms = 0;
} else if (jittered >= base_double) {
delay_ms = current_retry_delay_ms_;
} else {
delay_ms = static_cast<uint64_t>(jittered);
}
}
}

last_was_success_ = false;
last_scheduled_delay_ms_ = delay_ms;
logger_->LogDebug("[RetryHelper] Scheduling retry in %" PRIu64 "ms",
delay_ms);

const uint64_t task_id = ++next_task_id_;
scheduled_retry_id_ = task_id;
scheduled_retry_ = scheduler_->Schedule(
[safe_ref = safe_this_, task_id,
callback = std::move(callback)]() mutable {
{
ThisRefLock callback_lock(&safe_ref);
RetryHelper* helper = callback_lock.GetReference();
if (!helper || helper->scheduled_retry_id_ != task_id) {
return;
}
// Clear scheduled_retry_ before invoking the user callback so
// re-entrant Retry() or Cancel() calls will not invoke
// RequestHandle::Cancel() on the currently running request while
// Scheduler::TriggerCallback() holds status->mutex.
helper->scheduled_retry_ = scheduler::RequestHandle();
helper->scheduled_retry_id_ = 0;
}
if (callback) {
callback();
}
},
delay_ms);
}

// Cancel the previous handle outside safe_this_ lock to avoid lock-order
// inversion with Scheduler::TriggerCallback() (which holds status->mutex).
if (previous_retry.IsValid()) {
previous_retry.Cancel();
}
}

void RetryHelper::SignalSuccess() {
ThisRefLock lock(&safe_this_);
last_was_success_ = true;
current_retry_delay_ms_ = 0;
}

void RetryHelper::SetMaxDelay() {
ThisRefLock lock(&safe_this_);
current_retry_delay_ms_ = max_retry_delay_ms_;
}

void RetryHelper::Cancel() {
scheduler::RequestHandle to_cancel;
{
ThisRefLock lock(&safe_this_);
if (scheduled_retry_.IsValid()) {
logger_->LogDebug("[RetryHelper] Cancelling existing retry attempt");
std::swap(to_cancel, scheduled_retry_);
scheduled_retry_id_ = 0;
} else {
logger_->LogDebug("[RetryHelper] No existing retry attempt to cancel");
}
current_retry_delay_ms_ = 0;
}

if (to_cancel.IsValid()) {
to_cancel.Cancel();
}
}

uint64_t RetryHelper::current_retry_delay_ms() const {
ThisRefLock lock(&safe_this_);
return current_retry_delay_ms_;
}

uint64_t RetryHelper::last_scheduled_delay_ms() const {
ThisRefLock lock(&safe_this_);
return last_scheduled_delay_ms_;
}

bool RetryHelper::last_was_success() const {
ThisRefLock lock(&safe_this_);
return last_was_success_;
}

double RetryHelper::NextRandom() {
const double val = random_generator_();
// Allow a closed upper bound (<= 1.0) merely to accept buggy standard
// library implementations (e.g. LWG 2524) that occasionally return 1.0.
FIREBASE_DEV_ASSERT(std::isfinite(val) && val >= 0.0 && val <= 1.0);
if (!std::isfinite(val) || val < 0.0) {
return 0.0;
}
if (val >= 1.0) {
return std::nextafter(1.0, 0.0);
}
return val;
}

} // namespace connection
} // namespace internal
} // namespace database
} // namespace firebase
Loading
Loading