Repository navigation
Commit c13eb81
Add proportional jitter to the reconnect delays (#1255)
Port of
[livekit/client-sdk-js#2124](livekit/client-sdk-js#2124).
## The bug
`Engine.reconnect` computed its delay as:
```dart
var delay = defaultRetryDelaysInMs[_reconnectAttempts];
// Add random jitter to prevent thundering herd on reconnect
if (_reconnectAttempts > 1) {
delay += math.Random().nextInt(1000);
}
```
Two problems, the same pair the JS SDK had before #2124:
1. **The first two attempts get no jitter at all.**
`defaultRetryDelaysInMs[0]` is `0`, so every client disconnected by the
same event reconnects at the *same instant*, then again in lockstep
300ms later. The herd only begins to spread on the third attempt — by
which point the server has already taken two synchronised waves.
2. **The jitter that is applied is additive, not proportional.** A flat
0–1000ms window swamps a 300ms delay (up to 4.3x) and barely perturbs a
7000ms one (14%).
## Repro
Added `test/core/reconnect_delay_test.dart`. Against the current logic
(first commit extracts the delay into `reconnectDelayInMs` with
behaviour unchanged, so the test exercises the real code):
```
reconnect delay spreads out clients that disconnect at the same moment FAILED
Expected: a value greater than <1> Actual: <1>
reconnect delay later retries also vary between callers FAILED
Expected: a value greater than <1> Actual: <1>
reconnect delay scales jitter with the base delay FAILED
Expected: a value less than or equal to <1800> Actual: <1831>
retry 2 exceeded 1.5x of 1200
```
100 callers asking for their first — and second — retry delay all got
the identical value.
## The fix
Scale each delay by a random factor in `[0.5, 1.5)`. The first retry has
a base delay of `0` with nothing to scale, so it draws from a fixed
0–500ms window instead; a zero at any later index is an explicit request
to retry immediately and still passes through as zero.
## Verification
`flutter test` — 471/471 pass (the three assertions above fail before
the change). `flutter analyze lib/src/core/engine.dart` clean.
## Note
`reconnectDelayInMs` is marked `@internal`; `defaultRetryDelaysInMs` was
already public and is unchanged.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com>1 parent 3750763 commit c13eb81
3 files changed
Lines changed: 95 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
69 | 91 | | |
70 | 92 | | |
71 | 93 | | |
| |||
1118 | 1140 | | |
1119 | 1141 | | |
1120 | 1142 | | |
1121 | | - | |
1122 | | - | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
| 1143 | + | |
1126 | 1144 | | |
1127 | 1145 | | |
1128 | 1146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
0 commit comments