ClientTransport.swift 3.9 KB

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