| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /*
- * Copyright 2023, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #if canImport(Darwin)
- public import Darwin // should be @usableFromInline
- #elseif canImport(Android)
- public import Android // should be @usableFromInline
- #elseif canImport(Glibc)
- public import Glibc // should be @usableFromInline
- #elseif canImport(Musl)
- public import Musl // should be @usableFromInline
- #else
- #error("Unsupported OS")
- #endif
- @usableFromInline
- struct RetryDelaySequence: Sequence {
- @usableFromInline
- typealias Element = Duration
- @usableFromInline
- let policy: RetryPolicy
- @inlinable
- init(policy: RetryPolicy) {
- self.policy = policy
- }
- @inlinable
- func makeIterator() -> Iterator {
- Iterator(policy: self.policy)
- }
- @usableFromInline
- struct Iterator: IteratorProtocol {
- @usableFromInline
- let policy: RetryPolicy
- @usableFromInline
- private(set) var n = 1
- @inlinable
- init(policy: RetryPolicy) {
- self.policy = policy
- }
- @inlinable
- var _initialBackoffSeconds: Double {
- Self._durationToTimeInterval(self.policy.initialBackoff)
- }
- @inlinable
- var _maxBackoffSeconds: Double {
- Self._durationToTimeInterval(self.policy.maxBackoff)
- }
- @inlinable
- mutating func next() -> Duration? {
- defer { self.n += 1 }
- /// The nth retry will happen after a randomly chosen delay between zero and
- /// `min(initialBackoff * backoffMultiplier^(n-1), maxBackoff)`.
- let factor = pow(self.policy.backoffMultiplier, Double(self.n - 1))
- let computedBackoff = self._initialBackoffSeconds * factor
- let clampedBackoff = Swift.min(computedBackoff, self._maxBackoffSeconds)
- let randomisedBackoff = Double.random(in: 0.0 ... clampedBackoff)
- return Self._timeIntervalToDuration(randomisedBackoff)
- }
- @inlinable
- static func _timeIntervalToDuration(_ seconds: Double) -> Duration {
- let secondsComponent = Int64(seconds)
- let attoseconds = (seconds - Double(secondsComponent)) * 1e18
- let attosecondsComponent = Int64(attoseconds)
- return Duration(
- secondsComponent: secondsComponent,
- attosecondsComponent: attosecondsComponent
- )
- }
- @inlinable
- static func _durationToTimeInterval(_ duration: Duration) -> Double {
- var seconds = Double(duration.components.seconds)
- seconds += (Double(duration.components.attoseconds) / 1e18)
- return seconds
- }
- }
- }
|