GRPCTimeout.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import NIO
  18. public enum GRPCTimeoutError: String, Error {
  19. case negative = "GRPCTimeout must be non-negative"
  20. case tooManyDigits = "GRPCTimeout must be at most 8 digits"
  21. }
  22. /// A timeout for a gRPC call.
  23. ///
  24. /// Timeouts must be positive and at most 8-digits long.
  25. public struct GRPCTimeout: CustomStringConvertible, Equatable {
  26. public static let `default`: GRPCTimeout = try! .minutes(1)
  27. /// Creates an infinite timeout. This is a sentinel value which must __not__ be sent to a gRPC service.
  28. public static let infinite: GRPCTimeout = GRPCTimeout(nanoseconds: Int64.max, description: "infinite")
  29. /// A description of the timeout in the format described in the
  30. /// [gRPC protocol](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md).
  31. public let description: String
  32. public let nanoseconds: Int64
  33. private init(nanoseconds: Int64, description: String) {
  34. self.nanoseconds = nanoseconds
  35. self.description = description
  36. }
  37. private static func makeTimeout(_ amount: Int, _ unit: GRPCTimeoutUnit) throws -> GRPCTimeout {
  38. // Timeouts must be positive and at most 8-digits.
  39. if amount < 0 { throw GRPCTimeoutError.negative }
  40. if amount >= 100_000_000 { throw GRPCTimeoutError.tooManyDigits }
  41. // See "Timeout" in https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
  42. let description = "\(amount)\(unit.rawValue)"
  43. let nanoseconds = Int64(amount) * unit.asNanoseconds
  44. return GRPCTimeout(nanoseconds: nanoseconds, description: description)
  45. }
  46. /// Creates a new GRPCTimeout for the given amount of hours.
  47. ///
  48. /// `amount` must be positive and at most 8-digits.
  49. ///
  50. /// - Parameter amount: the amount of hours this `GRPCTimeout` represents.
  51. /// - Returns: A `GRPCTimeout` representing the given number of hours.
  52. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  53. public static func hours(_ amount: Int) throws -> GRPCTimeout {
  54. return try makeTimeout(amount, .hours)
  55. }
  56. /// Creates a new GRPCTimeout for the given amount of minutes.
  57. ///
  58. /// `amount` must be positive and at most 8-digits.
  59. ///
  60. /// - Parameter amount: the amount of minutes this `GRPCTimeout` represents.
  61. /// - Returns: A `GRPCTimeout` representing the given number of minutes.
  62. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  63. public static func minutes(_ amount: Int) throws -> GRPCTimeout {
  64. return try makeTimeout(amount, .minutes)
  65. }
  66. /// Creates a new GRPCTimeout for the given amount of seconds.
  67. ///
  68. /// `amount` must be positive and at most 8-digits.
  69. ///
  70. /// - Parameter amount: the amount of seconds this `GRPCTimeout` represents.
  71. /// - Returns: A `GRPCTimeout` representing the given number of seconds.
  72. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  73. public static func seconds(_ amount: Int) throws -> GRPCTimeout {
  74. return try makeTimeout(amount, .seconds)
  75. }
  76. /// Creates a new GRPCTimeout for the given amount of milliseconds.
  77. ///
  78. /// `amount` must be positive and at most 8-digits.
  79. ///
  80. /// - Parameter amount: the amount of milliseconds this `GRPCTimeout` represents.
  81. /// - Returns: A `GRPCTimeout` representing the given number of milliseconds.
  82. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  83. public static func milliseconds(_ amount: Int) throws -> GRPCTimeout {
  84. return try makeTimeout(amount, .milliseconds)
  85. }
  86. /// Creates a new GRPCTimeout for the given amount of microseconds.
  87. ///
  88. /// `amount` must be positive and at most 8-digits.
  89. ///
  90. /// - Parameter amount: the amount of microseconds this `GRPCTimeout` represents.
  91. /// - Returns: A `GRPCTimeout` representing the given number of microseconds.
  92. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  93. public static func microseconds(_ amount: Int) throws -> GRPCTimeout {
  94. return try makeTimeout(amount, .microseconds)
  95. }
  96. /// Creates a new GRPCTimeout for the given amount of nanoseconds.
  97. ///
  98. /// `amount` must be positive and at most 8-digits.
  99. ///
  100. /// - Parameter amount: the amount of nanoseconds this `GRPCTimeout` represents.
  101. /// - Returns: A `GRPCTimeout` representing the given number of nanoseconds.
  102. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  103. public static func nanoseconds(_ amount: Int) throws -> GRPCTimeout {
  104. return try makeTimeout(amount, .nanoseconds)
  105. }
  106. }
  107. extension GRPCTimeout {
  108. /// Returns a NIO `TimeAmount` representing the amount of time as this timeout.
  109. public var asNIOTimeAmount: TimeAmount {
  110. return TimeAmount.nanoseconds(numericCast(nanoseconds))
  111. }
  112. }
  113. private enum GRPCTimeoutUnit: String {
  114. case hours = "H"
  115. case minutes = "M"
  116. case seconds = "S"
  117. case milliseconds = "m"
  118. case microseconds = "u"
  119. case nanoseconds = "n"
  120. internal var asNanoseconds: Int64 {
  121. switch self {
  122. case .hours:
  123. return 60 * 60 * 1000 * 1000 * 1000
  124. case .minutes:
  125. return 60 * 1000 * 1000 * 1000
  126. case .seconds:
  127. return 1000 * 1000 * 1000
  128. case .milliseconds:
  129. return 1000 * 1000
  130. case .microseconds:
  131. return 1000
  132. case .nanoseconds:
  133. return 1
  134. }
  135. }
  136. }