Shims.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 NIOHPACK
  18. import NIOSSL
  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(
  54. *,
  55. deprecated,
  56. renamed: "init(customMetadata:timeLimit:messageEncoding:requestIDProvider:requestIDHeader:cacheable:)"
  57. )
  58. public init(
  59. customMetadata: HPACKHeaders = HPACKHeaders(),
  60. timeout: GRPCTimeout,
  61. messageEncoding: ClientMessageEncoding = .disabled,
  62. requestIDProvider: RequestIDProvider = .autogenerated,
  63. requestIDHeader: String? = nil,
  64. cacheable: Bool = false
  65. ) {
  66. self.init(
  67. customMetadata: customMetadata,
  68. timeLimit: .timeout(timeout.asNIOTimeAmount),
  69. messageEncoding: messageEncoding,
  70. requestIDProvider: requestIDProvider,
  71. requestIDHeader: requestIDHeader,
  72. cacheable: cacheable
  73. )
  74. }
  75. // TODO: `timeLimit.wrapped` can be private when the shims are removed.
  76. @available(*, deprecated, renamed: "timeLimit")
  77. public var timeout: GRPCTimeout {
  78. get {
  79. switch self.timeLimit.wrapped {
  80. case .none:
  81. return .infinite
  82. case let .timeout(timeout) where timeout.nanoseconds == .max:
  83. return .infinite
  84. case let .deadline(deadline) where deadline == .distantFuture:
  85. return .infinite
  86. case let .timeout(timeout):
  87. return GRPCTimeout.nanoseconds(rounding: Int(timeout.nanoseconds))
  88. case let .deadline(deadline):
  89. return GRPCTimeout(deadline: deadline)
  90. }
  91. }
  92. set {
  93. self.timeLimit = .timeout(newValue.asNIOTimeAmount)
  94. }
  95. }
  96. }
  97. extension GRPCTimeout {
  98. /// Creates a new GRPCTimeout for the given amount of hours.
  99. ///
  100. /// `amount` must be positive and at most 8-digits.
  101. ///
  102. /// - Parameter amount: the amount of hours this `GRPCTimeout` represents.
  103. /// - Returns: A `GRPCTimeout` representing the given number of hours.
  104. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  105. @available(
  106. *,
  107. deprecated,
  108. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  109. )
  110. public static func hours(_ amount: Int) throws -> GRPCTimeout {
  111. return try makeTimeout(Int64(amount), .hours)
  112. }
  113. /// Creates a new GRPCTimeout for the given amount of hours.
  114. ///
  115. /// The timeout will be rounded up if it may not be represented in the wire format.
  116. ///
  117. /// - Parameter amount: The number of hours to represent.
  118. @available(
  119. *,
  120. deprecated,
  121. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  122. )
  123. public static func hours(rounding amount: Int) -> GRPCTimeout {
  124. return .init(rounding: Int64(amount), unit: .hours)
  125. }
  126. /// Creates a new GRPCTimeout for the given amount of minutes.
  127. ///
  128. /// `amount` must be positive and at most 8-digits.
  129. ///
  130. /// - Parameter amount: the amount of minutes this `GRPCTimeout` represents.
  131. /// - Returns: A `GRPCTimeout` representing the given number of minutes.
  132. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  133. @available(
  134. *,
  135. deprecated,
  136. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  137. )
  138. public static func minutes(_ amount: Int) throws -> GRPCTimeout {
  139. return try makeTimeout(Int64(amount), .minutes)
  140. }
  141. /// Creates a new GRPCTimeout for the given amount of minutes.
  142. ///
  143. /// The timeout will be rounded up if it may not be represented in the wire format.
  144. ///
  145. /// - Parameter amount: The number of minutes to represent.
  146. @available(
  147. *,
  148. deprecated,
  149. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  150. )
  151. public static func minutes(rounding amount: Int) -> GRPCTimeout {
  152. return .init(rounding: Int64(amount), unit: .minutes)
  153. }
  154. /// Creates a new GRPCTimeout for the given amount of seconds.
  155. ///
  156. /// `amount` must be positive and at most 8-digits.
  157. ///
  158. /// - Parameter amount: the amount of seconds this `GRPCTimeout` represents.
  159. /// - Returns: A `GRPCTimeout` representing the given number of seconds.
  160. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  161. @available(
  162. *,
  163. deprecated,
  164. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  165. )
  166. public static func seconds(_ amount: Int) throws -> GRPCTimeout {
  167. return try makeTimeout(Int64(amount), .seconds)
  168. }
  169. /// Creates a new GRPCTimeout for the given amount of seconds.
  170. ///
  171. /// The timeout will be rounded up if it may not be represented in the wire format.
  172. ///
  173. /// - Parameter amount: The number of seconds to represent.
  174. @available(
  175. *,
  176. deprecated,
  177. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  178. )
  179. public static func seconds(rounding amount: Int) -> GRPCTimeout {
  180. return .init(rounding: Int64(amount), unit: .seconds)
  181. }
  182. /// Creates a new GRPCTimeout for the given amount of milliseconds.
  183. ///
  184. /// `amount` must be positive and at most 8-digits.
  185. ///
  186. /// - Parameter amount: the amount of milliseconds this `GRPCTimeout` represents.
  187. /// - Returns: A `GRPCTimeout` representing the given number of milliseconds.
  188. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  189. @available(
  190. *,
  191. deprecated,
  192. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  193. )
  194. public static func milliseconds(_ amount: Int) throws -> GRPCTimeout {
  195. return try makeTimeout(Int64(amount), .milliseconds)
  196. }
  197. /// Creates a new GRPCTimeout for the given amount of milliseconds.
  198. ///
  199. /// The timeout will be rounded up if it may not be represented in the wire format.
  200. ///
  201. /// - Parameter amount: The number of milliseconds to represent.
  202. @available(
  203. *,
  204. deprecated,
  205. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  206. )
  207. public static func milliseconds(rounding amount: Int) -> GRPCTimeout {
  208. return .init(rounding: Int64(amount), unit: .milliseconds)
  209. }
  210. /// Creates a new GRPCTimeout for the given amount of microseconds.
  211. ///
  212. /// `amount` must be positive and at most 8-digits.
  213. ///
  214. /// - Parameter amount: the amount of microseconds this `GRPCTimeout` represents.
  215. /// - Returns: A `GRPCTimeout` representing the given number of microseconds.
  216. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  217. @available(
  218. *,
  219. deprecated,
  220. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  221. )
  222. public static func microseconds(_ amount: Int) throws -> GRPCTimeout {
  223. return try makeTimeout(Int64(amount), .microseconds)
  224. }
  225. /// Creates a new GRPCTimeout for the given amount of microseconds.
  226. ///
  227. /// The timeout will be rounded up if it may not be represented in the wire format.
  228. ///
  229. /// - Parameter amount: The number of microseconds to represent.
  230. @available(
  231. *,
  232. deprecated,
  233. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  234. )
  235. public static func microseconds(rounding amount: Int) -> GRPCTimeout {
  236. return .init(rounding: Int64(amount), unit: .microseconds)
  237. }
  238. /// Creates a new GRPCTimeout for the given amount of nanoseconds.
  239. ///
  240. /// `amount` must be positive and at most 8-digits.
  241. ///
  242. /// - Parameter amount: the amount of nanoseconds this `GRPCTimeout` represents.
  243. /// - Returns: A `GRPCTimeout` representing the given number of nanoseconds.
  244. /// - Throws: `GRPCTimeoutError` if the amount was negative or more than 8 digits long.
  245. @available(
  246. *,
  247. deprecated,
  248. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  249. )
  250. public static func nanoseconds(_ amount: Int) throws -> GRPCTimeout {
  251. return try makeTimeout(Int64(amount), .nanoseconds)
  252. }
  253. /// Creates a new GRPCTimeout for the given amount of nanoseconds.
  254. ///
  255. /// The timeout will be rounded up if it may not be represented in the wire format.
  256. ///
  257. /// - Parameter amount: The number of nanoseconds to represent.
  258. @available(
  259. *,
  260. deprecated,
  261. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  262. )
  263. public static func nanoseconds(rounding amount: Int) -> GRPCTimeout {
  264. return .init(rounding: Int64(amount), unit: .nanoseconds)
  265. }
  266. }
  267. extension GRPCTimeout {
  268. /// Returns a NIO `TimeAmount` representing the amount of time as this timeout.
  269. @available(
  270. *,
  271. deprecated,
  272. message: "Use 'TimeLimit.timeout(_:)' or 'TimeLimit.deadline(_:)' instead."
  273. )
  274. public var asNIOTimeAmount: TimeAmount {
  275. return TimeAmount.nanoseconds(numericCast(nanoseconds))
  276. }
  277. internal static func makeTimeout(_ amount: Int64, _ unit: GRPCTimeoutUnit) throws -> GRPCTimeout {
  278. // Timeouts must be positive and at most 8-digits.
  279. if amount < 0 {
  280. throw GRPCTimeoutError.negative
  281. }
  282. if amount > GRPCTimeout.maxAmount {
  283. throw GRPCTimeoutError.tooManyDigits
  284. }
  285. return .init(amount: amount, unit: unit)
  286. }
  287. }
  288. // These will be obsoleted when the shims are removed.
  289. /// Errors thrown when constructing a timeout.
  290. public struct GRPCTimeoutError: Error, Equatable, CustomStringConvertible {
  291. private enum BaseError {
  292. case negative
  293. case tooManyDigits
  294. }
  295. private var error: BaseError
  296. private init(_ error: BaseError) {
  297. self.error = error
  298. }
  299. public var description: String {
  300. switch self.error {
  301. case .negative:
  302. return "GRPCTimeoutError: time amount must not be negative"
  303. case .tooManyDigits:
  304. return "GRPCTimeoutError: too many digits to represent using the gRPC wire-format"
  305. }
  306. }
  307. /// The timeout is negative.
  308. public static let negative = GRPCTimeoutError(.negative)
  309. /// The number of digits in the timeout amount is more than 8-digits and cannot be encoded in
  310. /// the gRPC wire-format.
  311. public static let tooManyDigits = GRPCTimeoutError(.tooManyDigits)
  312. }
  313. extension UnaryCallHandler {
  314. @available(*, unavailable, message: "Please regenerate your code or use 'CallHandler.makeUnary'")
  315. public convenience init(
  316. callHandlerContext: CallHandlerContext,
  317. eventObserverFactory: @escaping (UnaryResponseCallContext<ResponsePayload>) -> EventObserver
  318. ) {
  319. fatalError("Unimplemented: please regenerate your code.")
  320. }
  321. }
  322. extension ServerStreamingCallHandler {
  323. @available(
  324. *,
  325. unavailable,
  326. message: "Please regenerate your code or use 'CallHandler.makeServerStreaming'"
  327. )
  328. public convenience init(
  329. callHandlerContext: CallHandlerContext,
  330. eventObserverFactory: @escaping (StreamingResponseCallContext<ResponsePayload>) -> EventObserver
  331. ) {
  332. fatalError("Unimplemented: please regenerate your code.")
  333. }
  334. }
  335. extension ClientStreamingCallHandler {
  336. @available(
  337. *,
  338. unavailable,
  339. message: "Please regenerate your code or use 'CallHandler.makeClientStreaming'"
  340. )
  341. public convenience init(
  342. callHandlerContext: CallHandlerContext,
  343. eventObserverFactory: @escaping EventObserverFactory
  344. ) {
  345. fatalError("Unimplemented: please regenerate your code.")
  346. }
  347. }
  348. extension BidirectionalStreamingCallHandler {
  349. @available(
  350. *,
  351. unavailable,
  352. message: "Please regenerate your code or use 'CallHandler.makeBidirectionalStreaming'"
  353. )
  354. public convenience init(
  355. callHandlerContext: CallHandlerContext,
  356. eventObserverFactory: @escaping (StreamingResponseCallContext<ResponsePayload>)
  357. -> EventLoopFuture<EventObserver>
  358. ) {
  359. fatalError("Unimplemented: please regenerate your code.")
  360. }
  361. }
  362. @available(*, deprecated, message: "This protocol is longer required. Please regenerate your code.")
  363. public protocol GRPCProtobufPayload {}
  364. extension GRPCStatus.Code {
  365. @available(*, deprecated, message: "Use a 'default' branch")
  366. public static let doNotUse = GRPCStatus.Code.unknown
  367. }