GRPCAsyncRequestStreamWriter.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /// To ensure requests are delivered in order callers should `await` the result of this call
  49. /// before sending another request. Callers who do not need this guarantee do not have to `await`
  50. /// the completion of this call and may send messages concurrently from multiple ``Task``s.
  51. /// However, it is important to note that no more than 16 writes may be pending at any one time
  52. /// and attempting to exceed this will result in an ``GRPCAsyncWriterError/tooManyPendingWrites``
  53. /// error being thrown.
  54. ///
  55. /// Callers must call ``finish()`` when they have no more requests left to send.
  56. ///
  57. /// - Parameters:
  58. /// - request: The request to send.
  59. /// - compression: Whether the request should be compressed or not. Ignored if compression was
  60. /// not enabled for the RPC.
  61. /// - Throws: ``GRPCAsyncWriterError`` if there are too many pending writes or the request stream
  62. /// has already been finished.
  63. @inlinable
  64. public func send(
  65. _ request: Request,
  66. compression: Compression = .deferToCallDefault
  67. ) async throws {
  68. try await self.asyncWriter.yield((request, compression))
  69. }
  70. /// Finish the request stream for the RPC. This must be called when there are no more requests to be sent.
  71. public func finish() async throws {
  72. self.asyncWriter.finish()
  73. }
  74. /// Finish the request stream for the RPC with the given error.
  75. internal func finish(_ error: Error) {
  76. self.asyncWriter.finish(error: error)
  77. }
  78. }
  79. #endif // compiler(>=5.6)