InProcessServerTransport.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. import GRPCCore
  17. /// An in-process implementation of a ``ServerTransport``.
  18. ///
  19. /// This is useful when you're interested in testing your application without any actual networking layers
  20. /// involved, as the client and server will communicate directly with each other via in-process streams.
  21. ///
  22. /// To use this server, you call ``listen()`` and iterate over the returned `AsyncSequence` to get all
  23. /// RPC requests made from clients (as ``RPCStream``s).
  24. /// To stop listening to new requests, call ``stopListening()``.
  25. ///
  26. /// - SeeAlso: ``ClientTransport``
  27. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  28. public struct InProcessServerTransport: ServerTransport, Sendable {
  29. public typealias Inbound = RPCAsyncSequence<RPCRequestPart>
  30. public typealias Outbound = RPCWriter<RPCResponsePart>.Closable
  31. private let newStreams: AsyncStream<RPCStream<Inbound, Outbound>>
  32. private let newStreamsContinuation: AsyncStream<RPCStream<Inbound, Outbound>>.Continuation
  33. /// Creates a new instance of ``InProcessServerTransport``.
  34. public init() {
  35. (self.newStreams, self.newStreamsContinuation) = AsyncStream.makeStream()
  36. }
  37. /// Publish a new ``RPCStream``, which will be returned by the transport's ``events``
  38. /// successful case.
  39. ///
  40. /// - Parameter stream: The new ``RPCStream`` to publish.
  41. /// - Throws: ``RPCError`` with code ``RPCError/Code-swift.struct/failedPrecondition``
  42. /// if the server transport stopped listening to new streams (i.e., if ``stopListening()`` has been called).
  43. internal func acceptStream(_ stream: RPCStream<Inbound, Outbound>) throws {
  44. let yieldResult = self.newStreamsContinuation.yield(stream)
  45. if case .terminated = yieldResult {
  46. throw RPCError(
  47. code: .failedPrecondition,
  48. message: "The server transport is closed."
  49. )
  50. }
  51. }
  52. public func listen(
  53. _ streamHandler: @escaping (RPCStream<Inbound, Outbound>) async -> Void
  54. ) async throws {
  55. await withDiscardingTaskGroup { group in
  56. for await stream in self.newStreams {
  57. group.addTask {
  58. await streamHandler(stream)
  59. }
  60. }
  61. }
  62. }
  63. /// Stop listening to any new ``RPCStream`` publications.
  64. ///
  65. /// All further calls to ``acceptStream(_:)`` will not produce any new elements on the
  66. /// ``RPCAsyncSequence`` returned by ``listen()``.
  67. ///
  68. /// - SeeAlso: ``ServerTransport``
  69. public func stopListening() {
  70. self.newStreamsContinuation.finish()
  71. }
  72. }