GRPCAsyncRequestStreamWriter.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2021, 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. #if compiler(>=5.6)
  17. import NIOCore
  18. /// An object allowing the holder -- a client -- to send requests on an RPC.
  19. ///
  20. /// Requests may be sent using ``send(_:compression:)``. After all requests have been sent
  21. /// the user is responsible for closing the request stream by calling ``finish()``.
  22. ///
  23. /// ```
  24. /// // Send a request on the request stream, use the compression setting configured for the RPC.
  25. /// try await stream.send(request)
  26. ///
  27. /// // Send a request and explicitly disable compression.
  28. /// try await stream.send(request, compression: .disabled)
  29. ///
  30. /// // Finish the stream to indicate that no more messages will be sent.
  31. /// try await stream.finish()
  32. /// ```
  33. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  34. public struct GRPCAsyncRequestStreamWriter<Request: Sendable>: Sendable {
  35. @usableFromInline
  36. typealias AsyncWriter = NIOAsyncWriter<
  37. (Request, Compression),
  38. GRPCAsyncWriterSinkDelegate<(Request, Compression)>
  39. >
  40. @usableFromInline
  41. /* private */ internal let asyncWriter: AsyncWriter
  42. @inlinable
  43. internal init(asyncWriter: AsyncWriter) {
  44. self.asyncWriter = asyncWriter
  45. }
  46. /// Send a single request.
  47. ///
  48. /// It is safe to send multiple requests concurrently by sharing the ``GRPCAsyncRequestStreamWriter`` across tasks.
  49. ///
  50. /// Callers must call ``finish()`` when they have no more requests left to send.
  51. ///
  52. /// - Parameters:
  53. /// - request: The request to send.
  54. /// - compression: Whether the request should be compressed or not. Ignored if compression was
  55. /// not enabled for the RPC.
  56. /// - Throws: If the request stream has already been finished.
  57. @inlinable
  58. public func send(
  59. _ request: Request,
  60. compression: Compression = .deferToCallDefault
  61. ) async throws {
  62. try await self.asyncWriter.yield((request, compression))
  63. }
  64. /// Send a sequence of requests.
  65. ///
  66. /// It is safe to send multiple requests concurrently by sharing the ``GRPCAsyncRequestStreamWriter`` across tasks.
  67. ///
  68. /// Callers must call ``finish()`` when they have no more requests left to send.
  69. ///
  70. /// - Parameters:
  71. /// - requests: The requests to send.
  72. /// - compression: Whether the requests should be compressed or not. Ignored if compression was
  73. /// not enabled for the RPC.
  74. /// - Throws: If the request stream has already been finished.
  75. @inlinable
  76. public func send<S: Sequence>(
  77. _ requests: S,
  78. compression: Compression = .deferToCallDefault
  79. ) async throws where S.Element == Request {
  80. try await self.asyncWriter.yield(contentsOf: requests.lazy.map { ($0, compression) })
  81. }
  82. /// Finish the request stream for the RPC. This must be called when there are no more requests to be sent.
  83. public func finish() async throws {
  84. self.asyncWriter.finish()
  85. }
  86. /// Finish the request stream for the RPC with the given error.
  87. internal func finish(_ error: Error) {
  88. self.asyncWriter.finish(error: error)
  89. }
  90. }
  91. #endif // compiler(>=5.6)