ClientTransport.swift 3.5 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 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  17. public protocol ClientTransport: Sendable {
  18. typealias Inbound = RPCAsyncSequence<RPCResponsePart, any Error>
  19. typealias Outbound = RPCWriter<RPCRequestPart>.Closable
  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 all open streams have been closed and new connections
  34. /// are no longer required by the caller who signals this by calling ``close()``, or by cancelling the
  35. /// task this function runs in.
  36. func connect() 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
  40. /// ``ClientTransport/withStream(descriptor:options:_:)`` should result in an ``RPCError`` with
  41. /// code ``RPCError/Code/failedPrecondition`` being thrown.
  42. ///
  43. /// If you want to forcefully cancel all active streams then cancel the task
  44. /// running ``connect()``.
  45. func close()
  46. /// Opens a stream using the transport, and uses it as input into a user-provided closure.
  47. ///
  48. /// - Important: The opened stream is closed after the closure is finished.
  49. ///
  50. /// Transport implementations should throw an ``RPCError`` with the following error codes:
  51. /// - ``RPCError/Code/failedPrecondition`` if the transport is closing or has been closed.
  52. /// - ``RPCError/Code/unavailable`` if it's temporarily not possible to create a stream and it
  53. /// may be possible after some backoff period.
  54. ///
  55. /// - Parameters:
  56. /// - descriptor: A description of the method to open a stream for.
  57. /// - options: Options specific to the stream.
  58. /// - closure: A closure that takes the opened stream as parameter.
  59. /// - Returns: Whatever value was returned from `closure`.
  60. func withStream<T: Sendable>(
  61. descriptor: MethodDescriptor,
  62. options: CallOptions,
  63. _ closure: (_ stream: RPCStream<Inbound, Outbound>) async throws -> T
  64. ) async throws -> T
  65. /// Returns the configuration for a given method.
  66. ///
  67. /// - Parameter descriptor: The method to lookup configuration for.
  68. /// - Returns: Configuration for the method, if it exists.
  69. func configuration(forMethod descriptor: MethodDescriptor) -> MethodConfig?
  70. }