11import Foundation
22
3- open class ExponentialBackoffRetryPolicy : RetryPolicy {
3+ public struct ExponentialBackoffRetryPolicy : RetryPolicy {
44
5- public enum Jitter {
5+ public enum Jitter : Sendable {
66 case none
77 case full
88 case decorrelated( growthFactor: Double = ExponentialBackoffConstants . defaultDecorrelatedJitterGrowthFactor)
@@ -11,14 +11,16 @@ open class ExponentialBackoffRetryPolicy: RetryPolicy {
1111 public let timeSlot : TimeInterval
1212 public let maxDelay : TimeInterval
1313 public let jitter : Jitter
14- private var previousDelay : TimeInterval ?
14+ private let previousDelay : TimeInterval ?
1515
1616 public init ( timeSlot: TimeInterval = ExponentialBackoffConstants . defaultTimeSlot,
1717 maxDelay: TimeInterval = ExponentialBackoffConstants . defaultMaxDelay,
18- jitter: Jitter = ExponentialBackoffConstants . defaultJitter) {
18+ jitter: Jitter = ExponentialBackoffConstants . defaultJitter,
19+ previousDelay: TimeInterval ? = nil ) {
1920 self . timeSlot = timeSlot
2021 self . maxDelay = maxDelay
2122 self . jitter = jitter
23+ self . previousDelay = previousDelay
2224 }
2325
2426 public func exponentiationBySquaring< T: BinaryInteger > ( _ base: T , _ multiplier: T , _ exponent: T ) -> T {
@@ -57,7 +59,6 @@ open class ExponentialBackoffRetryPolicy: RetryPolicy {
5759 } else {
5860 delay = fullJitterDelay ( attemptIndex: attemptIndex)
5961 }
60- previousDelay = delay
6162 return delay
6263 }
6364
@@ -72,15 +73,15 @@ open class ExponentialBackoffRetryPolicy: RetryPolicy {
7273 }
7374 }
7475
75- open func retryDelay( for attemptFailure: AttemptFailure ) -> TimeInterval {
76+ public func retryDelay( for attemptFailure: AttemptFailure ) -> TimeInterval {
7677 min ( maxDelay, uncappedDelay ( attemptIndex: attemptFailure. index) )
7778 }
7879
7980 public func shouldRetry( on attemptFailure: AttemptFailure ) -> RetryDecision {
8081 . retry( delay: retryDelay ( for: attemptFailure) )
8182 }
8283
83- public func freshCopy ( ) -> RetryPolicy {
84- ExponentialBackoffRetryPolicy ( timeSlot: timeSlot, maxDelay: maxDelay, jitter: jitter)
84+ public func policyAfter ( attemptFailure : AttemptFailure , delay : TimeInterval ) -> any RetryPolicy {
85+ ExponentialBackoffRetryPolicy ( timeSlot: timeSlot, maxDelay: maxDelay, jitter: jitter, previousDelay : delay )
8586 }
8687}
0 commit comments