@@ -7,9 +7,9 @@ import 'dart:math';
7
7
class BackoffMachine {
8
8
BackoffMachine ();
9
9
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 ;
13
13
14
14
DateTime ? _startTime;
15
15
@@ -24,8 +24,8 @@ class BackoffMachine {
24
24
///
25
25
/// The popular exponential backoff strategy is to increase the duration
26
26
/// 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 :
29
29
///
30
30
/// 100, 200, 400, 800, 1600, 3200, 6400, 10000, 10000, 10000, ...
31
31
///
@@ -40,12 +40,12 @@ class BackoffMachine {
40
40
Future <void > wait () async {
41
41
_startTime ?? = DateTime .now ();
42
42
43
- final duration =
43
+ final durationMs =
44
44
Random ().nextDouble () // "Jitter"
45
- * min (_durationCeiling ,
46
- _firstDuration * pow (_base, _waitsCompleted));
45
+ * min (_durationCeilingMs ,
46
+ _firstDurationMs * pow (_base, _waitsCompleted));
47
47
48
- await Future <void >.delayed (Duration (milliseconds: duration .round ()));
48
+ await Future <void >.delayed (Duration (milliseconds: durationMs .round ()));
49
49
50
50
_waitsCompleted++ ;
51
51
}
0 commit comments