ClientTransport.swift 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2023, 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. /// A type that provides a long-lived bidirectional communication channel to a server.
  17. ///
  18. /// The client transport is responsible for providing streams to a backend on top of which an
  19. /// RPC can be executed. A typical transport implementation will establish and maintain connections
  20. /// to a server (or servers) and manage these over time, potentially closing idle connections and
  21. /// creating new ones on demand. As such transports can be expensive to create and as such are
  22. /// intended to be used as long-lived objects which exist for the lifetime of your application.
  23. ///
  24. /// gRPC provides an in-process transport in the `GRPCInProcessTransport` module and HTTP/2
  25. /// transport built on top of SwiftNIO in the https://github.com/grpc/grpc-swift-nio-transport
  26. /// package.
  27. @available(gRPCSwift 2.0, *)
  28. @available(*, deprecated, message: "See https://forums.swift.org/t/80177")
  29. public protocol ClientTransport<Bytes>: Sendable {
  30. /// The bag-of-bytes type used by the transport.
  31. associatedtype Bytes: GRPCContiguousBytes & Sendable
  32. typealias Inbound = RPCAsyncSequence<RPCResponsePart<Bytes>, any Error>
  33. typealias Outbound = RPCWriter<RPCRequestPart<Bytes>>.Closable
  34. /// Returns a throttle which gRPC uses to determine whether retries can be executed.
  35. ///
  36. /// Client transports don't need to implement the throttle or interact with it beyond its
  37. /// creation. gRPC will record the results of requests to determine whether retries can be
  38. /// performed.
  39. var retryThrottle: RetryThrottle? { get }
  40. /// Establish and maintain a connection to the remote destination.
  41. ///
  42. /// Maintains a long-lived connection, or set of connections, to a remote destination.
  43. /// Connections may be added or removed over time as required by the implementation and the
  44. /// demand for streams by the client.
  45. ///
  46. /// Implementations of this function will typically create a long-lived task group which
  47. /// maintains connections. The function exits when all open streams have been closed and new connections
  48. /// are no longer required by the caller who signals this by calling ``beginGracefulShutdown()``, or by cancelling the
  49. /// task this function runs in.
  50. func connect() async throws
  51. /// Signal to the transport that no new streams may be created.
  52. ///
  53. /// Existing streams may run to completion naturally but calling
  54. /// ``ClientTransport/withStream(descriptor:options:_:)`` should result in an ``RPCError`` with
  55. /// code ``RPCError/Code/failedPrecondition`` being thrown.
  56. ///
  57. /// If you want to forcefully cancel all active streams then cancel the task
  58. /// running ``connect()``.
  59. func beginGracefulShutdown()
  60. /// Opens a stream using the transport, and uses it as input into a user-provided closure alongisde the given context.
  61. ///
  62. /// - Important: The opened stream is closed after the closure is finished.
  63. ///
  64. /// Transport implementations should throw an ``RPCError`` with the following error codes:
  65. /// - ``RPCError/Code/failedPrecondition`` if the transport is closing or has been closed.
  66. /// - ``RPCError/Code/unavailable`` if it's temporarily not possible to create a stream and it
  67. /// may be possible after some backoff period.
  68. ///
  69. /// - Parameters:
  70. /// - descriptor: A description of the method to open a stream for.
  71. /// - options: Options specific to the stream.
  72. /// - closure: A closure that takes the opened stream and the client context as its parameters.
  73. /// - Returns: Whatever value was returned from `closure`.
  74. func withStream<T: Sendable>(
  75. descriptor: MethodDescriptor,
  76. options: CallOptions,
  77. _ closure: (_ stream: RPCStream<Inbound, Outbound>, _ context: ClientContext) async throws -> T
  78. ) async throws -> T
  79. /// Returns the configuration for a given method.
  80. ///
  81. /// - Parameter descriptor: The method to lookup configuration for.
  82. /// - Returns: Configuration for the method, if it exists.
  83. func config(forMethod descriptor: MethodDescriptor) -> MethodConfig?
  84. }