RetryDelaySequence.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2023, 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. #if canImport(Darwin)
  17. import Darwin
  18. #else
  19. import Glibc
  20. #endif
  21. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  22. @usableFromInline
  23. struct RetryDelaySequence: Sequence {
  24. @usableFromInline
  25. typealias Element = Duration
  26. @usableFromInline
  27. let policy: RetryPolicy
  28. @inlinable
  29. init(policy: RetryPolicy) {
  30. self.policy = policy
  31. }
  32. @inlinable
  33. func makeIterator() -> Iterator {
  34. Iterator(policy: self.policy)
  35. }
  36. @usableFromInline
  37. struct Iterator: IteratorProtocol {
  38. @usableFromInline
  39. let policy: RetryPolicy
  40. @usableFromInline
  41. private(set) var n = 1
  42. @inlinable
  43. init(policy: RetryPolicy) {
  44. self.policy = policy
  45. }
  46. @inlinable
  47. var _initialBackoffSeconds: Double {
  48. Self._durationToTimeInterval(self.policy.initialBackoff)
  49. }
  50. @inlinable
  51. var _maximumBackoffSeconds: Double {
  52. Self._durationToTimeInterval(self.policy.maximumBackoff)
  53. }
  54. @inlinable
  55. mutating func next() -> Duration? {
  56. defer { self.n += 1 }
  57. /// The nth retry will happen after a randomly chosen delay between zero and
  58. /// `min(initialBackoff * backoffMultiplier^(n-1), maximumBackoff)`.
  59. let factor = pow(self.policy.backoffMultiplier, Double(self.n - 1))
  60. let computedBackoff = self._initialBackoffSeconds * factor
  61. let clampedBackoff = Swift.min(computedBackoff, self._maximumBackoffSeconds)
  62. let randomisedBackoff = Double.random(in: 0.0 ... clampedBackoff)
  63. return Self._timeIntervalToDuration(randomisedBackoff)
  64. }
  65. @inlinable
  66. static func _timeIntervalToDuration(_ seconds: Double) -> Duration {
  67. let secondsComponent = Int64(seconds)
  68. let attoseconds = (seconds - Double(secondsComponent)) * 1e18
  69. let attosecondsComponent = Int64(attoseconds)
  70. return Duration(
  71. secondsComponent: secondsComponent,
  72. attosecondsComponent: attosecondsComponent
  73. )
  74. }
  75. @inlinable
  76. static func _durationToTimeInterval(_ duration: Duration) -> Double {
  77. var seconds = Double(duration.components.seconds)
  78. seconds += (Double(duration.components.attoseconds) / 1e18)
  79. return seconds
  80. }
  81. }
  82. }