Skip to content

Back off between reconnect attempts that get no session - #23

Open
atsarevskiy wants to merge 1 commit into
krojew:masterfrom
atsarevskiy:fix-reconnect-backoff
Open

atsarevskiy wants to merge 1 commit into
krojew:masterfrom
atsarevskiy:fix-reconnect-backoff

Conversation

@atsarevskiy

Copy link
Copy Markdown

Problem

Fixes #21.

When a server accepts the TCP connection and then closes it, or refuses the connection, the client reconnects at once, for as long as the server keeps doing that.
Each attempt logs about five lines.

Cause

  • On a 0-byte read the io loop logs Connection closed: read and calls reconnect() with no delay
    (io.rs#L538-L544).
  • reconnect() waits retry_time only when the TCP connect fails
    (io.rs#L355-L363),
    and ZooKeeper::connect sets retry_time to 0
    (zookeeper.rs#L115).

Fix

  • reconnect() waits before every attempt after the first one that didn't get a session: 100 ms, doubling up to 2 s.
    The delay applies when retry_time is 0 too; a configured retry_time still adds to it after a failed TCP connect.
  • The delay resets to 0 when a session is established, so the first reconnect after a dropped session is still immediate.
  • The wait runs after the timeouts and the previous connection's read task are stopped, so a connect timeout can't fire during the wait and abort the next attempt.

Tests

  • New tests/test_reconnect_backoff.rs: a local server accepts and closes every connection, and the test requires 2 to 10 connections in 3 s.
    On master it fails with 14,256 connections; with this change the client made 5.
  • cargo test --all-features with zk-test-cluster built: 25 passed on master, 26 passed with this change. cargo fmt --check passes; cargo clippy --all-targets --all-features reports the same 9 warnings as master.
  • Real ClickHouse Keeper 26.3, 5 nodes, together with the fix for Client never recovers after ClickHouse Keeper rejects a handshake: ConnectRequest echoes the server's protocol_version #20:
    • 3 of 5 nodes paused for 45 s: 14 closed connections in the whole run instead of 114 without the backoff, and the client got a session 1.1 s after a new client did.
    • All 5 nodes stopped for 5 s and started: 21 connection attempts instead of 124,169, and the client had a session as soon as the ensemble was back.
  • Not changed: the Zk session timeout warning still prints on every attempt while the client has no session, because ping_sent only moves when a ping is sent.

- Wait 100 ms before the second attempt, doubling up to 2 s, also when retry_time is 0.
- Reset the delay when a session is established.

Fixes krojew#21
@coderabbitai

coderabbitai Bot commented Nov 11, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 23154f8e-bc86-4c78-b4c1-b7f9cd049811
Commits

Reviewing files that changed from the base of the PR and between 5ff1c19 and e4db1a6.

Files selected for processing: 2

  • src/io.rs
  • tests/test_reconnect_backoff.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reconnect loop has no delay when the server closes the connection after accept

1 participant