@@ -21,6 +21,7 @@ public protocol BackoffStrategy<Duration> {
21
21
struct ConstantBackoffStrategy < Duration: DurationProtocol > : BackoffStrategy {
22
22
@usableFromInline let constant : Duration
23
23
@usableFromInline init ( constant: Duration ) {
24
+ precondition ( constant >= . zero, " Constsnt must be greater than or equal to 0 " )
24
25
self . constant = constant
25
26
}
26
27
@inlinable func nextDuration( ) -> Duration {
@@ -32,8 +33,10 @@ struct ConstantBackoffStrategy<Duration: DurationProtocol>: BackoffStrategy {
32
33
@usableFromInline
33
34
struct LinearBackoffStrategy < Duration: DurationProtocol > : BackoffStrategy {
34
35
@usableFromInline var current : Duration
35
- @usableFromInline var increment : Duration
36
+ @usableFromInline let increment : Duration
36
37
@usableFromInline init ( increment: Duration , initial: Duration ) {
38
+ precondition ( initial >= . zero, " Initial must be greater than or equal to 0 " )
39
+ precondition ( increment >= . zero, " Increment must be greater than or equal to 0 " )
37
40
self . current = initial
38
41
self . increment = increment
39
42
}
@@ -48,6 +51,8 @@ struct LinearBackoffStrategy<Duration: DurationProtocol>: BackoffStrategy {
48
51
@usableFromInline var current : Duration
49
52
@usableFromInline let factor : Int
50
53
@usableFromInline init ( factor: Int , initial: Duration ) {
54
+ precondition ( initial >= . zero, " Initial must be greater than or equal to 0 " )
55
+ precondition ( factor >= 1 , " Factor must be greater than or equal to 1 " )
51
56
self . current = initial
52
57
self . factor = factor
53
58
}
@@ -109,8 +114,8 @@ struct EqualJitterBackoffStrategy<Base: BackoffStrategy, RNG: RandomNumberGenera
109
114
self . generator = generator
110
115
}
111
116
@inlinable mutating func nextDuration( ) -> Base . Duration {
112
- let halfBase = ( base. nextDuration ( ) / 2 ) . attoseconds
113
- return . init( attoseconds: halfBase + Int128. random ( in: 0 ... halfBase , using: & generator) )
117
+ let base = base. nextDuration ( )
118
+ return . init( attoseconds: Int128 . random ( in: ( base / 2 ) . attoseconds ... base . attoseconds , using: & generator) )
114
119
}
115
120
}
116
121
@@ -122,6 +127,7 @@ struct DecorrelatedJitterBackoffStrategy<Base: BackoffStrategy, RNG: RandomNumbe
122
127
@usableFromInline var current : Duration ?
123
128
@usableFromInline let factor : Int
124
129
@usableFromInline init ( base: Base , generator: RNG , factor: Int ) {
130
+ precondition ( factor >= 1 , " Factor must be greater than or equal to 1 " )
125
131
self . base = base
126
132
self . generator = generator
127
133
self . factor = factor
0 commit comments