RetryDelaySequence.swift 2.7 KB

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