|
| 1 | +/* |
| 2 | + * Copyright 2024, gRPC Authors All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) |
| 18 | +struct ConnectionBackoff { |
| 19 | + var initial: Duration |
| 20 | + var max: Duration |
| 21 | + var multiplier: Double |
| 22 | + var jitter: Double |
| 23 | + |
| 24 | + init(initial: Duration, max: Duration, multiplier: Double, jitter: Double) { |
| 25 | + self.initial = initial |
| 26 | + self.max = max |
| 27 | + self.multiplier = multiplier |
| 28 | + self.jitter = jitter |
| 29 | + } |
| 30 | + |
| 31 | + func makeIterator() -> Iterator { |
| 32 | + return Iterator(self) |
| 33 | + } |
| 34 | + |
| 35 | + // Deliberately not conforming to `IteratorProtocol` as `next()` never returns `nil` which |
| 36 | + // isn't expressible via `IteratorProtocol`. |
| 37 | + struct Iterator { |
| 38 | + private var isInitial: Bool |
| 39 | + private var currentBackoffSeconds: Double |
| 40 | + |
| 41 | + private let jitter: Double |
| 42 | + private let multiplier: Double |
| 43 | + private let maxBackoffSeconds: Double |
| 44 | + |
| 45 | + init(_ backoff: ConnectionBackoff) { |
| 46 | + self.isInitial = true |
| 47 | + self.currentBackoffSeconds = Self.seconds(from: backoff.initial) |
| 48 | + self.jitter = backoff.jitter |
| 49 | + self.multiplier = backoff.multiplier |
| 50 | + self.maxBackoffSeconds = Self.seconds(from: backoff.max) |
| 51 | + } |
| 52 | + |
| 53 | + private static func seconds(from duration: Duration) -> Double { |
| 54 | + var seconds = Double(duration.components.seconds) |
| 55 | + seconds += Double(duration.components.attoseconds) / 1e18 |
| 56 | + return seconds |
| 57 | + } |
| 58 | + |
| 59 | + private static func duration(from seconds: Double) -> Duration { |
| 60 | + let nanoseconds = seconds * 1e9 |
| 61 | + let wholeNanos = Int64(nanoseconds) |
| 62 | + return .nanoseconds(wholeNanos) |
| 63 | + } |
| 64 | + |
| 65 | + mutating func next() -> Duration { |
| 66 | + // The initial backoff doesn't get jittered. |
| 67 | + if self.isInitial { |
| 68 | + self.isInitial = false |
| 69 | + return Self.duration(from: self.currentBackoffSeconds) |
| 70 | + } |
| 71 | + |
| 72 | + // Scale up the last backoff. |
| 73 | + self.currentBackoffSeconds *= self.multiplier |
| 74 | + |
| 75 | + // Limit it to the max backoff. |
| 76 | + if self.currentBackoffSeconds > self.maxBackoffSeconds { |
| 77 | + self.currentBackoffSeconds = self.maxBackoffSeconds |
| 78 | + } |
| 79 | + |
| 80 | + let backoff = self.currentBackoffSeconds |
| 81 | + let jitter = Double.random(in: -(self.jitter * backoff) ... self.jitter * backoff) |
| 82 | + let jitteredBackoff = backoff + jitter |
| 83 | + |
| 84 | + return Self.duration(from: jitteredBackoff) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments