ServerTransport.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. /// A type that provides a bidirectional communication channel with a client.
  17. ///
  18. /// The server transport is responsible for handling connections created by a client and
  19. /// the multiplexing of those connections into streams corresponding to RPCs.
  20. ///
  21. /// gRPC provides an in-process transport in the `GRPCInProcessTransport` module and HTTP/2
  22. /// transport built on top of SwiftNIO in the https://github.com/grpc/grpc-swift-nio-transport
  23. /// package.
  24. @available(gRPCSwift 2.0, *)
  25. public protocol ServerTransport<Bytes>: Sendable {
  26. /// The bag-of-bytes type used by the transport.
  27. associatedtype Bytes: GRPCContiguousBytes & Sendable
  28. typealias Inbound = RPCAsyncSequence<RPCRequestPart<Bytes>, any Error>
  29. typealias Outbound = RPCWriter<RPCResponsePart<Bytes>>.Closable
  30. /// Starts the transport.
  31. ///
  32. /// Implementations will typically bind to a listening port when this function is called
  33. /// and start accepting new connections. Each accepted inbound RPC stream will be handed over to
  34. /// the provided `streamHandler` to handle accordingly.
  35. ///
  36. /// You can call ``beginGracefulShutdown()`` to stop the transport from accepting new streams. Existing
  37. /// streams must be allowed to complete naturally. However, transports may also enforce a grace
  38. /// period after which any open streams may be cancelled. You can also cancel the task running
  39. /// ``listen(streamHandler:)`` to abruptly close connections and streams.
  40. func listen(
  41. streamHandler: @escaping @Sendable (
  42. _ stream: RPCStream<Inbound, Outbound>,
  43. _ context: ServerContext
  44. ) async -> Void
  45. ) async throws
  46. /// Indicates to the transport that no new streams should be accepted.
  47. ///
  48. /// Existing streams are permitted to run to completion. However, the transport may also enforce
  49. /// a grace period, after which remaining streams are cancelled.
  50. func beginGracefulShutdown()
  51. }