ClientTransport.swift 4.4 KB

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