TimeLimit.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2020, 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. import Dispatch
  17. #if swift(>=5.6)
  18. @preconcurrency import NIOCore
  19. #else
  20. import NIOCore
  21. #endif // swift(>=5.6)
  22. /// A time limit for an RPC.
  23. ///
  24. /// RPCs may have a time limit imposed on them by a caller which may be timeout or deadline based.
  25. /// If the RPC has not completed before the limit is reached then the call will be cancelled and
  26. /// completed with a `.deadlineExceeded` status code.
  27. ///
  28. /// - Note: Servers may impose a time limit on an RPC independent of the client's time limit; RPCs
  29. /// may therefore complete with `.deadlineExceeded` even if no time limit was set by the client.
  30. public struct TimeLimit: Equatable, CustomStringConvertible, GRPCSendable {
  31. // private but for shimming.
  32. private enum Wrapped: Equatable, GRPCSendable {
  33. case none
  34. case timeout(TimeAmount)
  35. case deadline(NIODeadline)
  36. }
  37. // private but for shimming.
  38. private var wrapped: Wrapped
  39. private init(_ wrapped: Wrapped) {
  40. self.wrapped = wrapped
  41. }
  42. /// No time limit, the RPC will not be automatically cancelled by the client. Note: some services
  43. /// may impose a time limit on RPCs independent of the client's time limit.
  44. public static let none = TimeLimit(.none)
  45. /// Create a timeout before which the RPC must have completed. Failure to complete before the
  46. /// deadline will result in the RPC being cancelled.
  47. ///
  48. /// - Note: The timeout is started once the call has been invoked and the call may timeout waiting
  49. /// for an active connection.
  50. public static func timeout(_ timeout: TimeAmount) -> TimeLimit {
  51. return TimeLimit(.timeout(timeout))
  52. }
  53. /// Create a point in time by which the RPC must have completed. Failure to complete before the
  54. /// deadline will result in the RPC being cancelled.
  55. public static func deadline(_ deadline: NIODeadline) -> TimeLimit {
  56. return TimeLimit(.deadline(deadline))
  57. }
  58. /// Return the timeout, if one was set.
  59. public var timeout: TimeAmount? {
  60. switch self.wrapped {
  61. case let .timeout(timeout):
  62. return timeout
  63. case .none, .deadline:
  64. return nil
  65. }
  66. }
  67. /// Return the deadline, if one was set.
  68. public var deadline: NIODeadline? {
  69. switch self.wrapped {
  70. case let .deadline(deadline):
  71. return deadline
  72. case .none, .timeout:
  73. return nil
  74. }
  75. }
  76. }
  77. extension TimeLimit {
  78. /// Make a non-distant-future deadline from the give time limit.
  79. @usableFromInline
  80. internal func makeDeadline() -> NIODeadline {
  81. switch self.wrapped {
  82. case .none:
  83. return .distantFuture
  84. case let .timeout(timeout) where timeout.nanoseconds == .max:
  85. return .distantFuture
  86. case let .timeout(timeout):
  87. return .now() + timeout
  88. case let .deadline(deadline):
  89. return deadline
  90. }
  91. }
  92. public var description: String {
  93. switch self.wrapped {
  94. case .none:
  95. return "none"
  96. case let .timeout(timeout) where timeout.nanoseconds == .max:
  97. return "timeout=never"
  98. case let .timeout(timeout):
  99. return "timeout=\(timeout.nanoseconds) nanoseconds"
  100. case let .deadline(deadline) where deadline == .distantFuture:
  101. return "deadline=.distantFuture"
  102. case let .deadline(deadline):
  103. return "deadline=\(deadline.uptimeNanoseconds) uptimeNanoseconds"
  104. }
  105. }
  106. }