You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Evolution/NNNN-retry-backoff.md
+15-24Lines changed: 15 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Providing a standard `retry` function and reusable backoff strategies in Swift A
22
22
This proposal introduces a retry function that executes an asynchronous operation up to a specified number of attempts, with customizable delays and error-based retry decisions between attempts.
Additionally, this proposal includes a suite of backoff strategies that can be used to generate delays between retry attempts. The core strategies provide different patterns for calculating delays: constant intervals, linear growth, exponential growth, and decorrelated jitter.
43
+
Additionally, this proposal includes a suite of backoff strategies that can be used to generate delays between retry attempts. The core strategies provide different patterns for calculating delays: constant intervals, linear growth, and exponential growth.
Constant, linear, and exponential backoff provide overloads for both `Duration` and `DurationProtocol`. This matches the `retry` overloads where the default clock is `ContinuousClock` whose duration type is `Duration`.
77
-
78
-
Jitter variants currently require `Duration` rather than a generic `DurationProtocol`, because only `Duration` exposes a numeric representation suitable for randomization (see [SE-0457](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0457-duration-attosecond-represenation.md)).
79
-
80
-
Each of those strategies conforms to the `BackoffStrategy` protocol:
70
+
`BackoffStrategy` is a protocol with an associated type `Duration` which is required to conform to `DurationProtocol`:
Linear, exponential, and jitter backoff require the use of `Swift.Duration` rather than any type conforming to `DurationProtocol` due to limitations of `DurationProtocol` to do more complex mathematical operations, such as adding or multiplying with reporting overflows or generating random values. Constant, minimum and maximum are able to use `DurationProtocol`.
81
+
90
82
## Detailed design
91
83
92
84
### Retry
@@ -123,7 +115,7 @@ var backoff = Backoff
123
115
124
116
#### Custom backoff
125
117
126
-
Adopters may choose to create their own strategies. There is no requirement to conform to `BackoffStrategy`, since retry and backoff are decoupled; however, to use the provided modifiers (`minimum`, `maximum`, `jitter`), a strategy must conform.
118
+
Adopters may choose to create their own strategies. There is no requirement to conform to `BackoffStrategy`, since retry and backoff are decoupled; however, to use the provided modifiers (`minimum`, `maximum`, `fullJitter`, `equalJitter`), a strategy must conform.
127
119
128
120
Each call to `nextDuration()` returns the delay for the next retry attempt. Strategies are naturally stateful. For instance, they may track the number of invocations or the previously returned duration to calculate the next delay.
129
121
@@ -134,7 +126,6 @@ As previously mentioned this proposal introduces several common backoff strategi
/// Jitter prevents the thundering herd problem where multiple clients retry
258
-
/// simultaneously, reducing server load spikes and improving system stability.
259
-
///
260
-
/// - Precondition: `factor` must be greater than or equal to 1, and `base` must be greater than or equal to zero.
261
-
///
262
-
/// - Parameters:
263
-
/// - factor: The multiplication factor for calculating the upper bound of randomness.
264
-
/// - base: The base duration used as the minimum delay and initial reference.
265
-
/// - generator: The random number generator to use. Defaults to `SystemRandomNumberGenerator()`.
266
-
/// - Returns: A backoff strategy with decorrelated jitter.
267
-
@inlinablepublicstaticfunc decorrelatedJitter<RNG:RandomNumberGenerator>(factor:Int, base:Duration, using generator:RNG=SystemRandomNumberGenerator())->someBackoffStrategy<Duration>{
0 commit comments