ClientTransport.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /// Establish and maintain a connection to the remote destination.
  21. ///
  22. /// Maintains a long-lived connection, or set of connections, to a remote destination.
  23. /// Connections may be added or removed over time as required by the implementation and the
  24. /// demand for streams by the client.
  25. ///
  26. /// Implementations of this function will typically create a long-lived task group which
  27. /// maintains connections. The function exits when connections are no longer required by
  28. /// the caller who signals this by calling ``close()`` to indicate that no new streams are
  29. /// required or by cancelling the task this function runs in.
  30. ///
  31. /// - Parameter lazily: Whether the transport should establish connections lazily, that is,
  32. /// when the first stream is opened or eagerly, when this function is called. If `false`
  33. /// then the transport should attempt to establish a connection immediately. Note that
  34. /// this is a _hint_: transports aren't required to respect this value and you should
  35. /// refer to the documentation of the transport you're using to check whether it's supported.
  36. func connect(lazily: Bool) async throws
  37. /// Signal to the transport that no new streams may be created.
  38. ///
  39. /// Existing streams may run to completion naturally but calling ``openStream(descriptor:)``
  40. /// should result in an ``RPCError`` with code ``RPCError/Code/failedPrecondition`` being thrown.
  41. ///
  42. /// If you want to forcefully cancel all active streams then cancel the task
  43. /// running ``connect(lazily:)``.
  44. func close()
  45. /// Open a stream using the transport.
  46. ///
  47. /// Transport implementations should throw an ``RPCError`` with the following error codes:
  48. /// - ``RPCError/Code/failedPrecondition`` if the transport is closing or has been closed.
  49. /// - ``RPCError/Code/unavailable`` if it's temporarily not possible to create a stream and it
  50. /// may be possible after some backoff period.
  51. ///
  52. /// - Parameter descriptor: A description of the method to open a stream for.
  53. /// - Returns: A stream.
  54. func openStream(
  55. descriptor: MethodDescriptor
  56. ) async throws -> RPCStream<Inbound, Outbound>
  57. /// Returns the execution configuration for a given method.
  58. ///
  59. /// - Parameter descriptor: The method to lookup configuration for.
  60. /// - Returns: Execution configuration for the method, if it exists.
  61. func executionConfiguration(
  62. forMethod descriptor: MethodDescriptor
  63. ) -> ClientRPCExecutionConfiguration?
  64. }