ClientTransport.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  17. public protocol ClientTransport: Sendable {
  18. associatedtype Inbound: (AsyncSequence & Sendable) where Inbound.Element == RPCResponsePart
  19. associatedtype Outbound: ClosableRPCWriterProtocol<RPCRequestPart>
  20. /// Returns a throttle which gRPC uses to determine whether retries can be executed.
  21. ///
  22. /// Client transports don't need to implement the throttle or interact with it beyond its
  23. /// creation. gRPC will record the results of requests to determine whether retries can be
  24. /// performed.
  25. var retryThrottle: RetryThrottle { get }
  26. /// Establish and maintain a connection to the remote destination.
  27. ///
  28. /// Maintains a long-lived connection, or set of connections, to a remote destination.
  29. /// Connections may be added or removed over time as required by the implementation and the
  30. /// demand for streams by the client.
  31. ///
  32. /// Implementations of this function will typically create a long-lived task group which
  33. /// maintains connections. The function exits when connections are no longer required by
  34. /// the caller who signals this by calling ``close()`` to indicate that no new streams are
  35. /// required or by cancelling the task this function runs in.
  36. ///
  37. /// - Parameter lazily: Whether the transport should establish connections lazily, that is,
  38. /// when the first stream is opened or eagerly, when this function is called. If `false`
  39. /// then the transport should attempt to establish a connection immediately. Note that
  40. /// this is a _hint_: transports aren't required to respect this value and you should
  41. /// refer to the documentation of the transport you're using to check whether it's supported.
  42. func connect(lazily: Bool) async throws
  43. /// Signal to the transport that no new streams may be created.
  44. ///
  45. /// Existing streams may run to completion naturally but calling ``openStream(descriptor:)``
  46. /// should result in an ``RPCError`` with code ``RPCError/Code/failedPrecondition`` being thrown.
  47. ///
  48. /// If you want to forcefully cancel all active streams then cancel the task
  49. /// running ``connect(lazily:)``.
  50. func close()
  51. /// Open a stream using the transport.
  52. ///
  53. /// Transport implementations should throw an ``RPCError`` with the following error codes:
  54. /// - ``RPCError/Code/failedPrecondition`` if the transport is closing or has been closed.
  55. /// - ``RPCError/Code/unavailable`` if it's temporarily not possible to create a stream and it
  56. /// may be possible after some backoff period.
  57. ///
  58. /// - Parameter descriptor: A description of the method to open a stream for.
  59. /// - Returns: A stream.
  60. func openStream(
  61. descriptor: MethodDescriptor
  62. ) async throws -> RPCStream<Inbound, Outbound>
  63. /// Returns the execution configuration for a given method.
  64. ///
  65. /// - Parameter descriptor: The method to lookup configuration for.
  66. /// - Returns: Execution configuration for the method, if it exists.
  67. func executionConfiguration(
  68. forMethod descriptor: MethodDescriptor
  69. ) -> ClientRPCExecutionConfiguration?
  70. }