RetryDelaySequence.swift 2.8 KB

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