Skip to content

Implement exponential backoff for RTDB Desktop - #1983

Merged
dconeybe merged 34 commits into
mainfrom
rtdb-desktop-exponential-backoff
Nov 26, 2026
Merged

dconeybe merged 34 commits into
mainfrom
rtdb-desktop-exponential-backoff

Conversation

@dconeybe

@dconeybe dconeybe commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

This PR implements exponential backoff with random jitter for Realtime Database (RTDB) Desktop reconnection attempts, matching the Android and iOS SDK behavior to prevent tight reconnect loops. It introduces a thread-safe RetryHelper utility, integrates it into PersistentConnection alongside hardened asynchronous token-fetch lifecycle handling, and adds unit tests and release notes.

Fixes: #1971

Highlights

  • Exponential Backoff with Jitter (RetryHelper): Added RetryHelper to schedule reconnection retries on scheduler::Scheduler using exponential backoff (1 s initial delay after failure, 1.3x exponent, 30 s default cap, 0.7 jitter factor) with an immediate 0 ms attempt after a successful connection, matching Android and iOS RTDB retry behavior.
  • PersistentConnection Integration: Replaced immediate reconnection scheduling in PersistentConnection with RetryHelper. Backoff resets on server resets or connections established for longer than 30 seconds, pending retries are cancelled and reset on interrupts, and repeated invalid auth token responses jump directly to the maximum retry delay.
  • Hardened Token Fetching: Consolidated Auth and App Check token future callbacks into OnTokenFutureComplete using SafeReference and monotonic attempt counters so stale token results from interrupted or superseded attempts are ignored, futures are released outside locks, and null pointers / App Check token errors are handled safely.
  • Thread-Safe & Re-entrant Scheduling: Built RetryHelper with SafeReference synchronization, monotonic task IDs, unlocked callback execution and handle cancellation to avoid deadlocks or lock-order inversion, and immediate release of captured callbacks on cancellation, superseding, or destruction.
  • Unit Tests & Release Notes: Added a GoogleTest suite for RetryHelper covering backoff curves, jitter bounds, cancellation, re-entrancy, and concurrent destruction, and added an entry to the upcoming release notes.
Changelog
  • retry_helper.h
    • Added RetryHelper class declaration and RetryHelper::Options with default backoff constants (1000 ms min delay, 30000 ms max delay, 1.3 exponent, 0.7 jitter factor), kMaxSupportedRetryDelayMs (INT_MAX) bound, and optional custom RandomGenerator.
    • Declared thread-safe Retry(), SignalSuccess(), SetMaxDelay(), and Cancel() methods along with RetryHelperTestPeer test inspection accessors.
  • retry_helper.cc
    • Implemented RetryHelper exponential backoff and jitter calculations, including non-blocking RNG seeding (SeedRng) and defensive handling of NaN, overflow, and closed upper-bound [0.0, 1.0] RNG outputs.
    • Implemented SafeReference-guarded scheduling with monotonic task IDs, unlocked callback execution and RequestHandle::Cancel() calls, and immediate release of stored callbacks on cancel, supersede, or destruction.
  • persistent_connection.h
    • Added RetryHelper member, last_connection_established_time_ (steady_clock::time_point), and token attempt tracking (current_get_token_attempt_, pending_get_token_attempt_).
    • Introduced PendingToken struct, ClearPendingTokenFutures(), and unified OnTokenFutureComplete() replacing separate Auth and App Check static callbacks.
  • persistent_connection.cc
    • Recorded connection establishment time in OnReady() and called retry_helper_.SignalSuccess() in OnDisconnect() on server reset or when a connection lasted longer than 30 seconds.
    • Updated InterruptInternal() and ~PersistentConnection() to cancel pending retries and clear pending token futures, and updated HandleAuthTokenResponse() to call retry_helper_.SetMaxDelay() when invalid auth tokens hit the threshold.
    • Switched TryScheduleReconnect() to schedule attempts via retry_helper_.Retry() with ShouldReconnect() and state checks.
    • Updated token future handling to use SafeReference callbacks, ignore stale get_token_attempt results, release moved futures outside pending_token_future_mutex_, guard null error_message() / result() pointers, and log App Check token errors.
  • database/CMakeLists.txt
    • Added src/desktop/connection/retry_helper.cc to desktop_SRCS.
  • database/tests/CMakeLists.txt
    • Added the firebase_rtdb_desktop_connection_retry_helper_test test target.
  • retry_helper_test.cc
    • Added unit tests verifying immediate initial retries, deterministic exponential backoff and jitter curves, capping before jitter, SignalSuccess(), SetMaxDelay(), Cancel(), overlapping retries, re-entrant Retry()/Cancel(), safe destruction during pending or in-flight callbacks, immediate callback resource release, RNG clamping and default RNG bounds, scheduled delay timing, and empty callbacks.
  • readme.md
    • Added an Upcoming release note for Realtime Database Desktop exponential backoff on reconnection attempts.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new RetryHelper class to manage connection retries with exponential backoff and random jitter on the desktop client, matching the behavior of Android and iOS RTDB clients. It integrates RetryHelper into PersistentConnection, tracks token fetch attempts to ignore outdated results, and adds comprehensive unit tests for the new retry logic. There are no review comments, and we have no feedback to provide.

@dconeybe dconeybe added the tests-requested: quick Trigger a quick set of integration tests. label Nov 24, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. and removed tests-requested: quick Trigger a quick set of integration tests. labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

✅  Integration test succeeded!

Requested by @dconeybe on commit 0c31588
Last updated: Thu Nov 26 11:28 PST 2026
View integration test log & download artifacts

@dconeybe dconeybe self-assigned this Nov 24, 2026
@github-actions github-actions Bot added the tests: failed This PR's integration tests failed. label Nov 24, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 24, 2026
@dconeybe dconeybe added the tests-requested: quick Trigger a quick set of integration tests. label Nov 24, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. tests: failed This PR's integration tests failed. and removed tests-requested: quick Trigger a quick set of integration tests. tests: failed This PR's integration tests failed. labels Nov 24, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 24, 2026
@dconeybe dconeybe added the tests-requested: quick Trigger a quick set of integration tests. label Nov 24, 2026
@github-actions github-actions Bot added the tests: in-progress This PR's integration tests are in progress. label Nov 24, 2026
@github-actions github-actions Bot added tests: succeeded This PR's integration tests succeeded. and removed tests: failed This PR's integration tests failed. labels Nov 25, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 25, 2026
Base automatically changed from dconeybe/fix-desktop-persistence-across-instances to main November 26, 2026 02:04
@dconeybe dconeybe added api: database tests-requested: quick Trigger a quick set of integration tests. labels Nov 26, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. tests: succeeded This PR's integration tests succeeded. and removed tests-requested: quick Trigger a quick set of integration tests. tests: succeeded This PR's integration tests succeeded. labels Nov 26, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 26, 2026
@dconeybe dconeybe added the tests-requested: quick Trigger a quick set of integration tests. label Nov 26, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. tests: succeeded This PR's integration tests succeeded. and removed tests-requested: quick Trigger a quick set of integration tests. tests: succeeded This PR's integration tests succeeded. labels Nov 26, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 26, 2026
@dconeybe
dconeybe merged commit 0c31588 into main Nov 26, 2026
69 of 70 checks passed
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. and removed tests: succeeded This PR's integration tests succeeded. labels Nov 26, 2026
@github-actions github-actions Bot added the tests: succeeded This PR's integration tests succeeded. label Nov 26, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: database tests: succeeded This PR's integration tests succeeded.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Database on desktop reconnects with no delay, retrying thousands of times a second

2 participants