@@ -19,79 +19,79 @@ def reset(self):
19
19
pass
20
20
21
21
@abstractmethod
22
- def compute (self , failures ) :
22
+ def compute (self , failures : int ) -> float :
23
23
"""Compute backoff in seconds upon failure"""
24
24
pass
25
25
26
26
27
27
class ConstantBackoff (AbstractBackoff ):
28
28
"""Constant backoff upon failure"""
29
29
30
- def __init__ (self , backoff ) :
30
+ def __init__ (self , backoff : float ) -> None :
31
31
"""`backoff`: backoff time in seconds"""
32
32
self ._backoff = backoff
33
33
34
- def compute (self , failures ) :
34
+ def compute (self , failures : int ) -> float :
35
35
return self ._backoff
36
36
37
37
38
38
class NoBackoff (ConstantBackoff ):
39
39
"""No backoff upon failure"""
40
40
41
- def __init__ (self ):
41
+ def __init__ (self ) -> None :
42
42
super ().__init__ (0 )
43
43
44
44
45
45
class ExponentialBackoff (AbstractBackoff ):
46
46
"""Exponential backoff upon failure"""
47
47
48
- def __init__ (self , cap = DEFAULT_CAP , base = DEFAULT_BASE ):
48
+ def __init__ (self , cap : float = DEFAULT_CAP , base : float = DEFAULT_BASE ):
49
49
"""
50
50
`cap`: maximum backoff time in seconds
51
51
`base`: base backoff time in seconds
52
52
"""
53
53
self ._cap = cap
54
54
self ._base = base
55
55
56
- def compute (self , failures ) :
56
+ def compute (self , failures : int ) -> float :
57
57
return min (self ._cap , self ._base * 2 ** failures )
58
58
59
59
60
60
class FullJitterBackoff (AbstractBackoff ):
61
61
"""Full jitter backoff upon failure"""
62
62
63
- def __init__ (self , cap = DEFAULT_CAP , base = DEFAULT_BASE ):
63
+ def __init__ (self , cap : float = DEFAULT_CAP , base : float = DEFAULT_BASE ) -> None :
64
64
"""
65
65
`cap`: maximum backoff time in seconds
66
66
`base`: base backoff time in seconds
67
67
"""
68
68
self ._cap = cap
69
69
self ._base = base
70
70
71
- def compute (self , failures ) :
71
+ def compute (self , failures : int ) -> float :
72
72
return random .uniform (0 , min (self ._cap , self ._base * 2 ** failures ))
73
73
74
74
75
75
class EqualJitterBackoff (AbstractBackoff ):
76
76
"""Equal jitter backoff upon failure"""
77
77
78
- def __init__ (self , cap = DEFAULT_CAP , base = DEFAULT_BASE ):
78
+ def __init__ (self , cap : float = DEFAULT_CAP , base : float = DEFAULT_BASE ) -> None :
79
79
"""
80
80
`cap`: maximum backoff time in seconds
81
81
`base`: base backoff time in seconds
82
82
"""
83
83
self ._cap = cap
84
84
self ._base = base
85
85
86
- def compute (self , failures ) :
86
+ def compute (self , failures : int ) -> float :
87
87
temp = min (self ._cap , self ._base * 2 ** failures ) / 2
88
88
return temp + random .uniform (0 , temp )
89
89
90
90
91
91
class DecorrelatedJitterBackoff (AbstractBackoff ):
92
92
"""Decorrelated jitter backoff upon failure"""
93
93
94
- def __init__ (self , cap = DEFAULT_CAP , base = DEFAULT_BASE ):
94
+ def __init__ (self , cap : float = DEFAULT_CAP , base : float = DEFAULT_BASE ) -> None :
95
95
"""
96
96
`cap`: maximum backoff time in seconds
97
97
`base`: base backoff time in seconds
@@ -100,10 +100,10 @@ def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
100
100
self ._base = base
101
101
self ._previous_backoff = 0
102
102
103
- def reset (self ):
103
+ def reset (self ) -> None :
104
104
self ._previous_backoff = 0
105
105
106
- def compute (self , failures ) :
106
+ def compute (self , failures : int ) -> float :
107
107
max_backoff = max (self ._base , self ._previous_backoff * 3 )
108
108
temp = random .uniform (self ._base , max_backoff )
109
109
self ._previous_backoff = min (self ._cap , temp )
0 commit comments