GRPCAsyncRequestStreamWriter.swift 3.4 KB

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