InProcessServerTransport.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. public 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 ``beginGracefulShutdown()``.
  25. ///
  26. /// - SeeAlso: ``ClientTransport``
  27. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  28. public struct InProcessServerTransport: ServerTransport, Sendable {
  29. public typealias Inbound = RPCAsyncSequence<RPCRequestPart, any Error>
  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 ``beginGracefulShutdown()`` 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 @Sendable (
  54. _ stream: RPCStream<Inbound, Outbound>,
  55. _ context: ServerContext
  56. ) async -> Void
  57. ) async throws {
  58. await withDiscardingTaskGroup { group in
  59. for await stream in self.newStreams {
  60. group.addTask {
  61. let context = ServerContext(descriptor: stream.descriptor)
  62. await streamHandler(stream, context)
  63. }
  64. }
  65. }
  66. }
  67. /// Stop listening to any new ``RPCStream`` publications.
  68. ///
  69. /// - SeeAlso: ``ServerTransport``
  70. public func beginGracefulShutdown() {
  71. self.newStreamsContinuation.finish()
  72. }
  73. }