2
0

Shims.swift 13 KB

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