RetryDelaySequence.swift 2.9 KB

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