TimeLimit.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 NIO
  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 `.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 `.deadlineExceeded` even if no time limit was set by the client.
  26. public struct TimeLimit: Equatable, CustomStringConvertible {
  27. // private but for shimming.
  28. internal enum Wrapped: Equatable {
  29. case none
  30. case timeout(TimeAmount)
  31. case deadline(NIODeadline)
  32. }
  33. // private but for shimming.
  34. internal 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. internal func makeDeadline() -> NIODeadline {
  76. switch self.wrapped {
  77. case .none:
  78. return .distantFuture
  79. case let .timeout(timeout) where timeout.nanoseconds == .max:
  80. return .distantFuture
  81. case let .timeout(timeout):
  82. return .now() + timeout
  83. case let .deadline(deadline):
  84. return deadline
  85. }
  86. }
  87. public var description: String {
  88. switch self.wrapped {
  89. case .none:
  90. return "none"
  91. case let .timeout(timeout) where timeout.nanoseconds == .max:
  92. return "timeout=never"
  93. case let .timeout(timeout):
  94. return "timeout=\(timeout.nanoseconds) nanoseconds"
  95. case let .deadline(deadline) where deadline == .distantFuture:
  96. return "deadline=.distantFuture"
  97. case let .deadline(deadline):
  98. return "deadline=\(deadline.uptimeNanoseconds) uptimeNanoseconds"
  99. }
  100. }
  101. }