2
0

GRPCTimeout.swift 5.2 KB

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