Skip to content

Commit 012b601

Browse files
committed
backoff [nfc]: Clarify units on durations
Also mark the constants as constants.
1 parent c8d91cf commit 012b601

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/api/backoff.dart

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import 'dart:math';
77
class BackoffMachine {
88
BackoffMachine();
99

10-
final double _firstDuration = 100;
11-
final double _durationCeiling = 10 * 1000;
12-
final double _base = 2;
10+
static const double _firstDurationMs = 100;
11+
static const double _durationCeilingMs = 10 * 1000;
12+
static const double _base = 2;
1313

1414
DateTime? _startTime;
1515

@@ -24,8 +24,8 @@ class BackoffMachine {
2424
///
2525
/// The popular exponential backoff strategy is to increase the duration
2626
/// exponentially with the number of sleeps completed, with a base of 2,
27-
/// until a ceiling is reached. E.g., if firstDuration is 100 and
28-
/// durationCeiling is 10 * 1000 = 10000, the sequence is:
27+
/// until a ceiling is reached. E.g., if the first duration is 100ms and
28+
/// the ceiling is 10s = 10000ms, the sequence is, in ms:
2929
///
3030
/// 100, 200, 400, 800, 1600, 3200, 6400, 10000, 10000, 10000, ...
3131
///
@@ -40,12 +40,12 @@ class BackoffMachine {
4040
Future<void> wait() async {
4141
_startTime ??= DateTime.now();
4242

43-
final duration =
43+
final durationMs =
4444
Random().nextDouble() // "Jitter"
45-
* min(_durationCeiling,
46-
_firstDuration * pow(_base, _waitsCompleted));
45+
* min(_durationCeilingMs,
46+
_firstDurationMs * pow(_base, _waitsCompleted));
4747

48-
await Future<void>.delayed(Duration(milliseconds: duration.round()));
48+
await Future<void>.delayed(Duration(milliseconds: durationMs.round()));
4949

5050
_waitsCompleted++;
5151
}

0 commit comments

Comments
 (0)