InProcessTransport+Server.swift 3.2 KB

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