ClientTransport.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 ``withStream(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()``.
  44. func close()
  45. /// Opens a stream using the transport, and uses it as input into a user-provided closure.
  46. ///
  47. /// - Important: The opened stream is closed after the closure is finished.
  48. ///
  49. /// Transport implementations should throw an ``RPCError`` with the following error codes:
  50. /// - ``RPCError/Code/failedPrecondition`` if the transport is closing or has been closed.
  51. /// - ``RPCError/Code/unavailable`` if it's temporarily not possible to create a stream and it
  52. /// may be possible after some backoff period.
  53. ///
  54. /// - Parameters:
  55. /// - descriptor: A description of the method to open a stream for.
  56. /// - options: Options specific to the stream.
  57. /// - closure: A closure that takes the opened stream as parameter.
  58. /// - Returns: Whatever value was returned from `closure`.
  59. func withStream<T: Sendable>(
  60. descriptor: MethodDescriptor,
  61. options: CallOptions,
  62. _ closure: (_ stream: RPCStream<Inbound, Outbound>) async throws -> T
  63. ) async throws -> T
  64. /// Returns the configuration for a given method.
  65. ///
  66. /// - Parameter descriptor: The method to lookup configuration for.
  67. /// - Returns: Configuration for the method, if it exists.
  68. func configuration(forMethod descriptor: MethodDescriptor) -> MethodConfig?
  69. }