ClientTransport.swift 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. public protocol ClientTransport: Sendable {
  28. typealias Inbound = RPCAsyncSequence<RPCResponsePart, any Error>
  29. typealias Outbound = RPCWriter<RPCRequestPart>.Closable
  30. /// Returns a throttle which gRPC uses to determine whether retries can be executed.
  31. ///
  32. /// Client transports don't need to implement the throttle or interact with it beyond its
  33. /// creation. gRPC will record the results of requests to determine whether retries can be
  34. /// performed.
  35. var retryThrottle: RetryThrottle? { get }
  36. /// Establish and maintain a connection to the remote destination.
  37. ///
  38. /// Maintains a long-lived connection, or set of connections, to a remote destination.
  39. /// Connections may be added or removed over time as required by the implementation and the
  40. /// demand for streams by the client.
  41. ///
  42. /// Implementations of this function will typically create a long-lived task group which
  43. /// maintains connections. The function exits when all open streams have been closed and new connections
  44. /// are no longer required by the caller who signals this by calling ``beginGracefulShutdown()``, or by cancelling the
  45. /// task this function runs in.
  46. func connect() async throws
  47. /// Signal to the transport that no new streams may be created.
  48. ///
  49. /// Existing streams may run to completion naturally but calling
  50. /// ``ClientTransport/withStream(descriptor:options:_:)`` should result in an ``RPCError`` with
  51. /// code ``RPCError/Code/failedPrecondition`` being thrown.
  52. ///
  53. /// If you want to forcefully cancel all active streams then cancel the task
  54. /// running ``connect()``.
  55. func beginGracefulShutdown()
  56. /// Opens a stream using the transport, and uses it as input into a user-provided closure alongisde the given context.
  57. ///
  58. /// - Important: The opened stream is closed after the closure is finished.
  59. ///
  60. /// Transport implementations should throw an ``RPCError`` with the following error codes:
  61. /// - ``RPCError/Code/failedPrecondition`` if the transport is closing or has been closed.
  62. /// - ``RPCError/Code/unavailable`` if it's temporarily not possible to create a stream and it
  63. /// may be possible after some backoff period.
  64. ///
  65. /// - Parameters:
  66. /// - descriptor: A description of the method to open a stream for.
  67. /// - options: Options specific to the stream.
  68. /// - closure: A closure that takes the opened stream and the client context as its parameters.
  69. /// - Returns: Whatever value was returned from `closure`.
  70. func withStream<T: Sendable>(
  71. descriptor: MethodDescriptor,
  72. options: CallOptions,
  73. _ closure: (_ stream: RPCStream<Inbound, Outbound>, _ context: ClientContext) async throws -> T
  74. ) async throws -> T
  75. /// Returns the configuration for a given method.
  76. ///
  77. /// - Parameter descriptor: The method to lookup configuration for.
  78. /// - Returns: Configuration for the method, if it exists.
  79. func config(forMethod descriptor: MethodDescriptor) -> MethodConfig?
  80. }