Repository navigation
Add proportional jitter to the reconnect delays #1255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bf07b20
Add proportional jitter to the reconnect delays
xianshijing-lk 6f887b8
Apply dart format
xianshijing-lk dbf62f0
Fix a flaky bound in the first-retry jitter test
xianshijing-lk ae1560f
Shorten the first-retry jitter window and add the release entry
xianshijing-lk 104686c
Jitter any zero base delay in the reconnect schedule
hiroshihorie 58105f0
Drop the JS SDK value from the first-retry jitter doc
hiroshihorie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| patch type="fixed" "Reconnect delays now carry proportional jitter, so clients disconnected by the same event no longer retry in lockstep. The first retry draws from a small random window instead of firing immediately, and later retries are scaled by a random factor between 0.5 and 1.5" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright 2026 LiveKit, Inc. | ||
| // | ||
| // 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. | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| import 'package:livekit_client/src/core/engine.dart'; | ||
|
|
||
| void main() { | ||
| group('reconnect delay', () { | ||
| test('spreads out clients that disconnect at the same moment', () { | ||
| // The first retry's base delay is 0, so without jitter every client | ||
| // disconnected by the same event reconnects at the same instant. | ||
| final observed = List.generate(100, (_) => reconnectDelayInMs(0)).toSet(); | ||
|
|
||
| expect( | ||
| observed.length, | ||
| greaterThan(1), | ||
| reason: 'every caller got the same first retry delay: $observed', | ||
| ); | ||
| }); | ||
|
|
||
| test('keeps the first retry inside a bounded window', () { | ||
| for (var i = 0; i < 1000; i++) { | ||
| final delay = reconnectDelayInMs(0); | ||
|
|
||
| expect(delay, greaterThanOrEqualTo(0)); | ||
| // Inclusive: the window is rounded to whole milliseconds, so the top of | ||
| // the range rounds up to the bound itself. | ||
| expect(delay, lessThanOrEqualTo(maxFirstRetryJitterInMs)); | ||
| } | ||
| }); | ||
|
|
||
| test('scales jitter with the base delay', () { | ||
| for (var retryCount = 1; retryCount < defaultRetryDelaysInMs.length; retryCount++) { | ||
| final base = defaultRetryDelaysInMs[retryCount]; | ||
|
|
||
| for (var i = 0; i < 100; i++) { | ||
| final delay = reconnectDelayInMs(retryCount); | ||
|
|
||
| expect( | ||
| delay, | ||
| greaterThanOrEqualTo((base * 0.5).round()), | ||
| reason: 'retry $retryCount fell below half of $base', | ||
| ); | ||
| expect( | ||
| delay, | ||
| lessThanOrEqualTo((base * 1.5).round()), | ||
| reason: 'retry $retryCount exceeded 1.5x of $base', | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test('later retries also vary between callers', () { | ||
| final observed = List.generate(100, (_) => reconnectDelayInMs(1)).toSet(); | ||
|
|
||
| expect(observed.length, greaterThan(1)); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.