22//
33// This source file is part of the Swift Distributed Actors open source project
44//
5- // Copyright (c) 2018-2019 Apple Inc. and the Swift Distributed Actors project authors
5+ // Copyright (c) 2018-2022 Apple Inc. and the Swift Distributed Actors project authors
66// Licensed under Apache License v2.0
77//
88// See LICENSE.txt for license information
2222/// See also: `ConstantBackoffStrategy`, `ExponentialBackoffStrategy`
2323public protocol BackoffStrategy {
2424 /// Returns next backoff interval to use OR `nil` if no further retries should be performed.
25- mutating func next( ) -> TimeAmount ?
25+ mutating func next( ) -> Duration ?
2626
2727 /// Reset the strategy to its initial backoff amount.
2828 mutating func reset( )
@@ -46,8 +46,8 @@ public enum Backoff {
4646 /// Backoff each time using the same, constant, time amount.
4747 ///
4848 /// See `ConstantBackoffStrategy` for details
49- public static func constant( _ backoff: TimeAmount ) -> ConstantBackoffStrategy {
50- . init( timeAmount : backoff)
49+ public static func constant( _ backoff: Duration ) -> ConstantBackoffStrategy {
50+ . init( duration : backoff)
5151 }
5252
5353 /// Creates a strategy implementing the exponential backoff pattern.
@@ -68,9 +68,9 @@ public enum Backoff {
6868 /// - maxAttempts: An optional maximum number of times backoffs shall be attempted.
6969 /// MUST be `> 0` if set (or `nil`).
7070 public static func exponential(
71- initialInterval: TimeAmount = ExponentialBackoffStrategy . Defaults. initialInterval,
71+ initialInterval: Duration = ExponentialBackoffStrategy . Defaults. initialInterval,
7272 multiplier: Double = ExponentialBackoffStrategy . Defaults. multiplier,
73- capInterval: TimeAmount = ExponentialBackoffStrategy . Defaults. capInterval,
73+ capInterval: Duration = ExponentialBackoffStrategy . Defaults. capInterval,
7474 randomFactor: Double = ExponentialBackoffStrategy . Defaults. randomFactor,
7575 maxAttempts: Int ? = ExponentialBackoffStrategy . Defaults. maxAttempts
7676 ) -> ExponentialBackoffStrategy {
@@ -94,14 +94,14 @@ public enum Backoff {
9494/// - SeeAlso: Also used to configure `_SupervisionStrategy`.
9595public struct ConstantBackoffStrategy : BackoffStrategy {
9696 /// The constant time amount to back-off by each time.
97- internal let timeAmount : TimeAmount
97+ internal let duration : Duration
9898
99- public init ( timeAmount : TimeAmount ) {
100- self . timeAmount = timeAmount
99+ public init ( duration : Duration ) {
100+ self . duration = duration
101101 }
102102
103- public func next( ) -> TimeAmount ? {
104- self . timeAmount
103+ public func next( ) -> Duration ? {
104+ self . duration
105105 }
106106
107107 public func reset( ) {
@@ -150,28 +150,28 @@ public struct ExponentialBackoffStrategy: BackoffStrategy {
150150
151151 /// Default values for the backoff parameters.
152152 public enum Defaults {
153- public static let initialInterval : TimeAmount = . milliseconds( 200 )
153+ public static let initialInterval : Duration = . milliseconds( 200 )
154154 public static let multiplier : Double = 1.5
155- public static let capInterval : TimeAmount = . effectivelyInfinite
155+ public static let capInterval : Duration = . effectivelyInfinite
156156 public static let randomFactor : Double = 0.25
157157
158158 // TODO: We could also implement taking a Clock, and using it see if there's a total limit exceeded
159- // public static let maxElapsedTime: TimeAmount = .minutes(30)
159+ // public static let maxElapsedTime: Duration = .minutes(30)
160160
161161 public static let maxAttempts : Int ? = nil
162162 }
163163
164- let initialInterval : TimeAmount
164+ let initialInterval : Duration
165165 let multiplier : Double
166- let capInterval : TimeAmount
166+ let capInterval : Duration
167167 let randomFactor : Double
168168
169169 var limitedRemainingAttempts : Int ?
170170
171171 // interval that will be used in the `next()` call, does NOT include the random noise component
172- private var currentBaseInterval : TimeAmount
172+ private var currentBaseInterval : Duration
173173
174- internal init ( initialInterval: TimeAmount , multiplier: Double , capInterval: TimeAmount , randomFactor: Double , maxAttempts: Int ? ) {
174+ internal init ( initialInterval: Duration , multiplier: Double , capInterval: Duration , randomFactor: Double , maxAttempts: Int ? ) {
175175 precondition ( initialInterval. nanoseconds > 0 , " initialInterval MUST be > 0ns, was: [ \( initialInterval. prettyDescription) ] " )
176176 precondition ( multiplier >= 1.0 , " multiplier MUST be >= 1.0, was: [ \( multiplier) ] " )
177177 precondition ( initialInterval <= capInterval, " capInterval MUST be >= initialInterval, was: [ \( capInterval) ] " )
@@ -188,7 +188,7 @@ public struct ExponentialBackoffStrategy: BackoffStrategy {
188188 self . limitedRemainingAttempts = maxAttempts
189189 }
190190
191- public mutating func next( ) -> TimeAmount ? {
191+ public mutating func next( ) -> Duration ? {
192192 defer { self . limitedRemainingAttempts? -= 1 }
193193 if let remainingAttempts = self . limitedRemainingAttempts, remainingAttempts <= 0 {
194194 return nil
@@ -226,5 +226,5 @@ public struct ExponentialBackoffStrategy: BackoffStrategy {
226226// MARK: Errors
227227
228228enum BackoffError {
229- case exceededNumberOfAttempts( limit: Int , period: TimeAmount )
229+ case exceededNumberOfAttempts( limit: Int , period: Duration )
230230}
0 commit comments