Skip to content

Commit 45af1fc

Browse files
as-ciimaxdeviant
andauthored
Always double reconnection delay and add jitter (#35337)
Previously, we would pick an exponent between 0.5 and 2.5, which would cause a lot of clients to try reconnecting in rapid succession, overwhelming the server as a result. This pull request always doubles the previous delay and introduces a jitter that can, at most, double it. As part of this, we're also increasing the maximum reconnection delay from 10s to 30s: this gives us more space to spread out the reconnection requests. Release Notes: - N/A --------- Co-authored-by: Marshall Bowers <git@maxdeviant.com>
1 parent 0aea5ac commit 45af1fc

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

crates/client/src/client.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use rpc::proto::{AnyTypedEnvelope, EnvelopedMessage, PeerId, RequestMessage};
3131
use schemars::JsonSchema;
3232
use serde::{Deserialize, Serialize};
3333
use settings::{Settings, SettingsSources};
34-
use std::pin::Pin;
3534
use std::{
3635
any::TypeId,
3736
convert::TryFrom,
@@ -45,6 +44,7 @@ use std::{
4544
},
4645
time::{Duration, Instant},
4746
};
47+
use std::{cmp, pin::Pin};
4848
use telemetry::Telemetry;
4949
use thiserror::Error;
5050
use tokio::net::TcpStream;
@@ -78,7 +78,7 @@ pub static ZED_ALWAYS_ACTIVE: LazyLock<bool> =
7878
LazyLock::new(|| std::env::var("ZED_ALWAYS_ACTIVE").map_or(false, |e| !e.is_empty()));
7979

8080
pub const INITIAL_RECONNECTION_DELAY: Duration = Duration::from_millis(500);
81-
pub const MAX_RECONNECTION_DELAY: Duration = Duration::from_secs(10);
81+
pub const MAX_RECONNECTION_DELAY: Duration = Duration::from_secs(30);
8282
pub const CONNECTION_TIMEOUT: Duration = Duration::from_secs(20);
8383

8484
actions!(
@@ -727,11 +727,10 @@ impl Client {
727727
},
728728
&cx,
729729
);
730-
cx.background_executor().timer(delay).await;
731-
delay = delay
732-
.mul_f32(rng.gen_range(0.5..=2.5))
733-
.max(INITIAL_RECONNECTION_DELAY)
734-
.min(MAX_RECONNECTION_DELAY);
730+
let jitter =
731+
Duration::from_millis(rng.gen_range(0..delay.as_millis() as u64));
732+
cx.background_executor().timer(delay + jitter).await;
733+
delay = cmp::min(delay * 2, MAX_RECONNECTION_DELAY);
735734
} else {
736735
break;
737736
}

crates/collab/src/tests/integration_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ async fn test_client_disconnecting_from_room(
842842

843843
// Allow user A to reconnect to the server.
844844
server.allow_connections();
845-
executor.advance_clock(RECEIVE_TIMEOUT);
845+
executor.advance_clock(RECONNECT_TIMEOUT);
846846

847847
// Call user B again from client A.
848848
active_call_a
@@ -1358,7 +1358,7 @@ async fn test_calls_on_multiple_connections(
13581358

13591359
// User A reconnects automatically, then calls user B again.
13601360
server.allow_connections();
1361-
executor.advance_clock(RECEIVE_TIMEOUT);
1361+
executor.advance_clock(RECONNECT_TIMEOUT);
13621362
active_call_a
13631363
.update(cx_a, |call, cx| {
13641364
call.invite(client_b1.user_id().unwrap(), None, cx)

0 commit comments

Comments
 (0)