@@ -21,6 +21,7 @@ public protocol BackoffStrategy<Duration> {
2121struct ConstantBackoffStrategy < Duration: DurationProtocol > : BackoffStrategy {
2222 @usableFromInline let constant : Duration
2323 @usableFromInline init ( constant: Duration ) {
24+ precondition ( constant >= . zero, " Constsnt must be greater than or equal to 0 " )
2425 self . constant = constant
2526 }
2627 @inlinable func nextDuration( ) -> Duration {
@@ -32,8 +33,10 @@ struct ConstantBackoffStrategy<Duration: DurationProtocol>: BackoffStrategy {
3233@usableFromInline
3334struct LinearBackoffStrategy < Duration: DurationProtocol > : BackoffStrategy {
3435 @usableFromInline var current : Duration
35- @usableFromInline var increment : Duration
36+ @usableFromInline let increment : Duration
3637 @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 " )
3740 self . current = initial
3841 self . increment = increment
3942 }
@@ -48,6 +51,8 @@ struct LinearBackoffStrategy<Duration: DurationProtocol>: BackoffStrategy {
4851 @usableFromInline var current : Duration
4952 @usableFromInline let factor : Int
5053 @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 " )
5156 self . current = initial
5257 self . factor = factor
5358 }
@@ -109,8 +114,8 @@ struct EqualJitterBackoffStrategy<Base: BackoffStrategy, RNG: RandomNumberGenera
109114 self . generator = generator
110115 }
111116 @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) )
114119 }
115120}
116121
@@ -122,6 +127,7 @@ struct DecorrelatedJitterBackoffStrategy<Base: BackoffStrategy, RNG: RandomNumbe
122127 @usableFromInline var current : Duration ?
123128 @usableFromInline let factor : Int
124129 @usableFromInline init ( base: Base , generator: RNG , factor: Int ) {
130+ precondition ( factor >= 1 , " Factor must be greater than or equal to 1 " )
125131 self . base = base
126132 self . generator = generator
127133 self . factor = factor
0 commit comments