InProcessTransport+Server.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. private import Synchronization
  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. public final class Server: ServerTransport, Sendable {
  30. public typealias Inbound = RPCAsyncSequence<RPCRequestPart, any Error>
  31. public typealias Outbound = RPCWriter<RPCResponsePart>.Closable
  32. private let newStreams: AsyncStream<RPCStream<Inbound, Outbound>>
  33. private let newStreamsContinuation: AsyncStream<RPCStream<Inbound, Outbound>>.Continuation
  34. private struct State: Sendable {
  35. private var _nextID: UInt64
  36. private var handles: [UInt64: ServerContext.RPCCancellationHandle]
  37. private var isShutdown: Bool
  38. private mutating func nextID() -> UInt64 {
  39. let id = self._nextID
  40. self._nextID &+= 1
  41. return id
  42. }
  43. init() {
  44. self._nextID = 0
  45. self.handles = [:]
  46. self.isShutdown = false
  47. }
  48. mutating func addHandle(_ handle: ServerContext.RPCCancellationHandle) -> (UInt64, Bool) {
  49. let handleID = self.nextID()
  50. self.handles[handleID] = handle
  51. return (handleID, self.isShutdown)
  52. }
  53. mutating func removeHandle(withID id: UInt64) {
  54. self.handles.removeValue(forKey: id)
  55. }
  56. mutating func beginShutdown() -> [ServerContext.RPCCancellationHandle] {
  57. self.isShutdown = true
  58. let values = Array(self.handles.values)
  59. self.handles.removeAll()
  60. return values
  61. }
  62. }
  63. private let handles: Mutex<State>
  64. /// Creates a new instance of ``Server``.
  65. public init() {
  66. (self.newStreams, self.newStreamsContinuation) = AsyncStream.makeStream()
  67. self.handles = Mutex(State())
  68. }
  69. /// Publish a new ``RPCStream``, which will be returned by the transport's ``events``
  70. /// successful case.
  71. ///
  72. /// - Parameter stream: The new ``RPCStream`` to publish.
  73. /// - Throws: ``RPCError`` with code ``RPCError/Code-swift.struct/failedPrecondition``
  74. /// if the server transport stopped listening to new streams (i.e., if ``beginGracefulShutdown()`` has been called).
  75. internal func acceptStream(_ stream: RPCStream<Inbound, Outbound>) throws {
  76. let yieldResult = self.newStreamsContinuation.yield(stream)
  77. if case .terminated = yieldResult {
  78. throw RPCError(
  79. code: .failedPrecondition,
  80. message: "The server transport is closed."
  81. )
  82. }
  83. }
  84. public func listen(
  85. streamHandler: @escaping @Sendable (
  86. _ stream: RPCStream<Inbound, Outbound>,
  87. _ context: ServerContext
  88. ) async -> Void
  89. ) async throws {
  90. await withDiscardingTaskGroup { group in
  91. for await stream in self.newStreams {
  92. group.addTask {
  93. await withServerContextRPCCancellationHandle { handle in
  94. let (id, isShutdown) = self.handles.withLock({ $0.addHandle(handle) })
  95. defer {
  96. self.handles.withLock { $0.removeHandle(withID: id) }
  97. }
  98. // This happens if `beginGracefulShutdown` is called after the stream is added to
  99. // new streams but before it's dequeued.
  100. if isShutdown {
  101. handle.cancel()
  102. }
  103. let context = ServerContext(descriptor: stream.descriptor, cancellation: handle)
  104. await streamHandler(stream, context)
  105. }
  106. }
  107. }
  108. }
  109. }
  110. /// Stop listening to any new `RPCStream` publications.
  111. ///
  112. /// - SeeAlso: `ServerTransport`
  113. public func beginGracefulShutdown() {
  114. self.newStreamsContinuation.finish()
  115. for handle in self.handles.withLock({ $0.beginShutdown() }) {
  116. handle.cancel()
  117. }
  118. }
  119. }
  120. }