2
0

Shims.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 NIO
  17. import NIOSSL
  18. import NIOHPACK
  19. // This file contains shims to notify users of API changes between v1.0.0-alpha.1 and v1.0.0.
  20. // TODO: Remove these shims before v1.0.0 is tagged.
  21. extension ClientConnection.Configuration {
  22. @available(*, deprecated, message: "use 'tls' and 'ClientConnection.Configuration.TLS'")
  23. public var tlsConfiguration: TLSConfiguration? {
  24. return nil
  25. }
  26. }
  27. extension Server.Configuration {
  28. @available(*, deprecated, message: "use 'tls' and 'Server.Configuration.TLS'")
  29. public var tlsConfiguration: TLSConfiguration? {
  30. return nil
  31. }
  32. }
  33. @available(*, deprecated, renamed: "PlatformSupport")
  34. public enum GRPCNIO {}
  35. extension ClientErrorDelegate {
  36. @available(*, deprecated, message: "Please use 'didCatchError(_:logger:file:line:)' instead")
  37. public func didCatchError(_ error: Error, file: StaticString, line: Int) { }
  38. }
  39. extension GRPCStatusTransformable {
  40. @available(*, deprecated, renamed: "makeGRPCStatus")
  41. func asGRPCStatus() -> GRPCStatus {
  42. return self.makeGRPCStatus()
  43. }
  44. }
  45. extension GRPCClient {
  46. @available(*, deprecated, renamed: "channel")
  47. public var connection: GRPCChannel {
  48. return self.channel
  49. }
  50. }
  51. extension CallOptions {
  52. @available(*, deprecated, renamed: "init(customMetadata:timeLimit:messageEncoding:requestIDProvider:requestIDHeader:cacheable:)")
  53. public init(
  54. customMetadata: HPACKHeaders = HPACKHeaders(),
  55. timeout: GRPCTimeout,
  56. messageEncoding: ClientMessageEncoding = .disabled,
  57. requestIDProvider: RequestIDProvider = .autogenerated,
  58. requestIDHeader: String? = nil,
  59. cacheable: Bool = false
  60. ) {
  61. self.init(
  62. customMetadata: customMetadata,
  63. timeLimit: .timeout(timeout.asNIOTimeAmount),
  64. messageEncoding: messageEncoding,
  65. requestIDProvider: requestIDProvider,
  66. requestIDHeader: requestIDHeader,
  67. cacheable: cacheable
  68. )
  69. }
  70. // TODO: `timeLimit.wrapped` can be private when the shims are removed.
  71. @available(*, deprecated, renamed: "timeLimit")
  72. public var timeout: GRPCTimeout {
  73. get {
  74. switch self.timeLimit.wrapped {
  75. case .none:
  76. return .infinite
  77. case .timeout(let timeout) where timeout.nanoseconds == .max:
  78. return .infinite
  79. case .deadline(let deadline) where deadline == .distantFuture:
  80. return .infinite
  81. case .timeout(let timeout):
  82. return GRPCTimeout.nanoseconds(rounding: Int(timeout.nanoseconds))
  83. case .deadline(let deadline):
  84. return GRPCTimeout(deadline: deadline)
  85. }
  86. }
  87. set {
  88. self.timeLimit = .timeout(newValue.asNIOTimeAmount)
  89. }
  90. }
  91. }
  92. extension GRPCTimeout {
  93. /// Creates a new GRPCTimeout for the given amount of hours.
  94. ///
  95. /// `amount` must be positive and at most 8-digits.
  96. ///
  97. /// - Parameter amount: the amount of hours this `GRPCTimeout` represents.
  98. /// - Returns: A `GRPCTimeout` representing the given number of hours.
  99. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  100. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  101. public static func hours(_ amount: Int) throws -> GRPCTimeout {
  102. return try makeTimeout(Int64(amount), .hours)
  103. }
  104. /// Creates a new GRPCTimeout for the given amount of hours.
  105. ///
  106. /// The timeout will be rounded up if it may not be represented in the wire format.
  107. ///
  108. /// - Parameter amount: The number of hours to represent.
  109. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  110. public static func hours(rounding amount: Int) -> GRPCTimeout {
  111. return .init(rounding: Int64(amount), unit: .hours)
  112. }
  113. /// Creates a new GRPCTimeout for the given amount of minutes.
  114. ///
  115. /// `amount` must be positive and at most 8-digits.
  116. ///
  117. /// - Parameter amount: the amount of minutes this `GRPCTimeout` represents.
  118. /// - Returns: A `GRPCTimeout` representing the given number of minutes.
  119. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  120. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  121. public static func minutes(_ amount: Int) throws -> GRPCTimeout {
  122. return try makeTimeout(Int64(amount), .minutes)
  123. }
  124. /// Creates a new GRPCTimeout for the given amount of minutes.
  125. ///
  126. /// The timeout will be rounded up if it may not be represented in the wire format.
  127. ///
  128. /// - Parameter amount: The number of minutes to represent.
  129. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  130. public static func minutes(rounding amount: Int) -> GRPCTimeout {
  131. return .init(rounding: Int64(amount), unit: .minutes)
  132. }
  133. /// Creates a new GRPCTimeout for the given amount of seconds.
  134. ///
  135. /// `amount` must be positive and at most 8-digits.
  136. ///
  137. /// - Parameter amount: the amount of seconds this `GRPCTimeout` represents.
  138. /// - Returns: A `GRPCTimeout` representing the given number of seconds.
  139. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  140. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  141. public static func seconds(_ amount: Int) throws -> GRPCTimeout {
  142. return try makeTimeout(Int64(amount), .seconds)
  143. }
  144. /// Creates a new GRPCTimeout for the given amount of seconds.
  145. ///
  146. /// The timeout will be rounded up if it may not be represented in the wire format.
  147. ///
  148. /// - Parameter amount: The number of seconds to represent.
  149. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  150. public static func seconds(rounding amount: Int) -> GRPCTimeout {
  151. return .init(rounding: Int64(amount), unit: .seconds)
  152. }
  153. /// Creates a new GRPCTimeout for the given amount of milliseconds.
  154. ///
  155. /// `amount` must be positive and at most 8-digits.
  156. ///
  157. /// - Parameter amount: the amount of milliseconds this `GRPCTimeout` represents.
  158. /// - Returns: A `GRPCTimeout` representing the given number of milliseconds.
  159. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  160. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  161. public static func milliseconds(_ amount: Int) throws -> GRPCTimeout {
  162. return try makeTimeout(Int64(amount), .milliseconds)
  163. }
  164. /// Creates a new GRPCTimeout for the given amount of milliseconds.
  165. ///
  166. /// The timeout will be rounded up if it may not be represented in the wire format.
  167. ///
  168. /// - Parameter amount: The number of milliseconds to represent.
  169. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  170. public static func milliseconds(rounding amount: Int) -> GRPCTimeout {
  171. return .init(rounding: Int64(amount), unit: .milliseconds)
  172. }
  173. /// Creates a new GRPCTimeout for the given amount of microseconds.
  174. ///
  175. /// `amount` must be positive and at most 8-digits.
  176. ///
  177. /// - Parameter amount: the amount of microseconds this `GRPCTimeout` represents.
  178. /// - Returns: A `GRPCTimeout` representing the given number of microseconds.
  179. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  180. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  181. public static func microseconds(_ amount: Int) throws -> GRPCTimeout {
  182. return try makeTimeout(Int64(amount), .microseconds)
  183. }
  184. /// Creates a new GRPCTimeout for the given amount of microseconds.
  185. ///
  186. /// The timeout will be rounded up if it may not be represented in the wire format.
  187. ///
  188. /// - Parameter amount: The number of microseconds to represent.
  189. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  190. public static func microseconds(rounding amount: Int) -> GRPCTimeout {
  191. return .init(rounding: Int64(amount), unit: .microseconds)
  192. }
  193. /// Creates a new GRPCTimeout for the given amount of nanoseconds.
  194. ///
  195. /// `amount` must be positive and at most 8-digits.
  196. ///
  197. /// - Parameter amount: the amount of nanoseconds this `GRPCTimeout` represents.
  198. /// - Returns: A `GRPCTimeout` representing the given number of nanoseconds.
  199. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  200. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  201. public static func nanoseconds(_ amount: Int) throws -> GRPCTimeout {
  202. return try makeTimeout(Int64(amount), .nanoseconds)
  203. }
  204. /// Creates a new GRPCTimeout for the given amount of nanoseconds.
  205. ///
  206. /// The timeout will be rounded up if it may not be represented in the wire format.
  207. ///
  208. /// - Parameter amount: The number of nanoseconds to represent.
  209. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  210. public static func nanoseconds(rounding amount: Int) -> GRPCTimeout {
  211. return .init(rounding: Int64(amount), unit: .nanoseconds)
  212. }
  213. }
  214. extension GRPCTimeout {
  215. /// Returns a NIO `TimeAmount` representing the amount of time as this timeout.
  216. @available(*, deprecated, message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead.")
  217. public var asNIOTimeAmount: TimeAmount {
  218. return TimeAmount.nanoseconds(numericCast(nanoseconds))
  219. }
  220. internal static func makeTimeout(_ amount: Int64, _ unit: GRPCTimeoutUnit) throws -> GRPCTimeout {
  221. // Timeouts must be positive and at most 8-digits.
  222. if amount < 0 {
  223. throw GRPCTimeoutError.negative
  224. }
  225. if amount > GRPCTimeout.maxAmount {
  226. throw GRPCTimeoutError.tooManyDigits
  227. }
  228. return .init(amount: amount, unit: unit)
  229. }
  230. }
  231. // These will be obsoleted when the shims are removed.
  232. /// Errors thrown when constructing a timeout.
  233. public struct GRPCTimeoutError: Error, Equatable, CustomStringConvertible {
  234. private enum BaseError {
  235. case negative
  236. case tooManyDigits
  237. }
  238. private var error: BaseError
  239. private init(_ error: BaseError) {
  240. self.error = error
  241. }
  242. public var description: String {
  243. switch self.error {
  244. case .negative:
  245. return "GRPCTimeoutError: time amount must not be negative"
  246. case .tooManyDigits:
  247. return "GRPCTimeoutError: too many digits to represent using the gRPC wire-format"
  248. }
  249. }
  250. /// The timeout is negative.
  251. public static let negative = GRPCTimeoutError(.negative)
  252. /// The number of digits in the timeout amount is more than 8-digits and cannot be encoded in
  253. /// the gRPC wire-format.
  254. public static let tooManyDigits = GRPCTimeoutError(.tooManyDigits)
  255. }