Call+AsyncRequestStreamWriter.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  19. extension Call where Request: Sendable, Response: Sendable {
  20. typealias AsyncWriter = NIOAsyncWriter<
  21. (Request, Compression),
  22. GRPCAsyncWriterSinkDelegate<(Request, Compression)>
  23. >
  24. internal func makeRequestStreamWriter()
  25. -> (GRPCAsyncRequestStreamWriter<Request>, AsyncWriter.Sink) {
  26. let delegate = GRPCAsyncWriterSinkDelegate<(Request, Compression)>(
  27. didYield: { requests in
  28. for (request, compression) in requests {
  29. let compress = compression
  30. .isEnabled(callDefault: self.options.messageEncoding.enabledForRequests)
  31. // TODO: be smarter about inserting flushes.
  32. // We currently always flush after every write which may trigger more syscalls than necessary.
  33. let metadata = MessageMetadata(compress: compress, flush: true)
  34. self.send(.message(request, metadata), promise: nil)
  35. }
  36. },
  37. didTerminate: { _ in self.send(.end, promise: nil) }
  38. )
  39. let writer = NIOAsyncWriter.makeWriter(isWritable: false, delegate: delegate)
  40. // Start as not-writable; writability will be toggled when the stream comes up.
  41. return (GRPCAsyncRequestStreamWriter<Request>(asyncWriter: writer.writer), writer.sink)
  42. }
  43. }
  44. #endif // compiler(>=5.6)