TimeLimit.swift 3.8 KB

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