Shims.swift 11 KB

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