ClientStreamingCall.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2019, 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 NIO
  17. import NIOHTTP2
  18. import Logging
  19. /// A client-streaming gRPC call.
  20. ///
  21. /// Messages should be sent via the `send` method; an `.end` message should be sent
  22. /// to indicate the final message has been sent.
  23. ///
  24. /// The following futures are available to the caller:
  25. /// - `initialMetadata`: the initial metadata returned from the server,
  26. /// - `response`: the response from the call,
  27. /// - `status`: the status of the gRPC call after it has ended,
  28. /// - `trailingMetadata`: any metadata returned from the server alongside the `status`.
  29. public final class ClientStreamingCall<RequestPayload: GRPCPayload, ResponsePayload: GRPCPayload>
  30. : BaseClientCall<RequestPayload, ResponsePayload>,
  31. StreamingRequestClientCall,
  32. UnaryResponseClientCall {
  33. public let response: EventLoopFuture<ResponsePayload>
  34. private var messageQueue: EventLoopFuture<Void>
  35. init(
  36. path: String,
  37. scheme: String,
  38. authority: String,
  39. callOptions: CallOptions,
  40. eventLoop: EventLoop,
  41. multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>,
  42. errorDelegate: ClientErrorDelegate?,
  43. logger: Logger
  44. ) {
  45. let requestID = callOptions.requestIDProvider.requestID()
  46. var logger = logger
  47. logger[metadataKey: MetadataKey.requestID] = "\(requestID)"
  48. logger.debug("starting rpc", metadata: ["path": "\(path)"])
  49. self.messageQueue = eventLoop.makeSucceededFuture(())
  50. let responsePromise = eventLoop.makePromise(of: ResponsePayload.self)
  51. self.response = responsePromise.futureResult
  52. let responseHandler = GRPCClientUnaryResponseChannelHandler(
  53. initialMetadataPromise: eventLoop.makePromise(),
  54. trailingMetadataPromise: eventLoop.makePromise(),
  55. responsePromise: responsePromise,
  56. statusPromise: eventLoop.makePromise(),
  57. errorDelegate: errorDelegate,
  58. timeout: callOptions.timeout,
  59. logger: logger
  60. )
  61. let requestHead = _GRPCRequestHead(
  62. scheme: scheme,
  63. path: path,
  64. host: authority,
  65. requestID: requestID,
  66. options: callOptions
  67. )
  68. let requestHandler = _StreamingRequestChannelHandler<RequestPayload>(requestHead: requestHead)
  69. super.init(
  70. eventLoop: eventLoop,
  71. multiplexer: multiplexer,
  72. callType: .clientStreaming,
  73. callOptions: callOptions,
  74. responseHandler: responseHandler,
  75. requestHandler: requestHandler,
  76. logger: logger
  77. )
  78. }
  79. public func newMessageQueue() -> EventLoopFuture<Void> {
  80. return self.messageQueue
  81. }
  82. }