UnaryCall.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Foundation
  17. import SwiftProtobuf
  18. import NIO
  19. import NIOHTTP1
  20. import NIOHTTP2
  21. import NIOHPACK
  22. import Logging
  23. /// A unary gRPC call. The request is sent on initialization.
  24. public final class UnaryCall<
  25. RequestPayload: GRPCPayload,
  26. ResponsePayload: GRPCPayload
  27. >: UnaryResponseClientCall {
  28. private let transport: ChannelTransport<RequestPayload, ResponsePayload>
  29. /// The options used to make the RPC.
  30. public let options: CallOptions
  31. /// The `Channel` used to transport messages for this RPC.
  32. public var subchannel: EventLoopFuture<Channel> {
  33. return self.transport.streamChannel()
  34. }
  35. /// The `EventLoop` this call is running on.
  36. public var eventLoop: EventLoop {
  37. return self.transport.eventLoop
  38. }
  39. /// Cancel this RPC if it hasn't already completed.
  40. public func cancel(promise: EventLoopPromise<Void>?) {
  41. self.transport.cancel(promise: promise)
  42. }
  43. // MARK: - Response Parts
  44. /// The initial metadata returned from the server.
  45. public var initialMetadata: EventLoopFuture<HPACKHeaders> {
  46. if self.eventLoop.inEventLoop {
  47. return self.transport.responseContainer.lazyInitialMetadataPromise.getFutureResult()
  48. } else {
  49. return self.eventLoop.flatSubmit {
  50. return self.transport.responseContainer.lazyInitialMetadataPromise.getFutureResult()
  51. }
  52. }
  53. }
  54. /// The response returned by the server.
  55. public let response: EventLoopFuture<ResponsePayload>
  56. /// The trailing metadata returned from the server.
  57. public var trailingMetadata: EventLoopFuture<HPACKHeaders> {
  58. if self.eventLoop.inEventLoop {
  59. return self.transport.responseContainer.lazyTrailingMetadataPromise.getFutureResult()
  60. } else {
  61. return self.eventLoop.flatSubmit {
  62. return self.transport.responseContainer.lazyTrailingMetadataPromise.getFutureResult()
  63. }
  64. }
  65. }
  66. /// The final status of the the RPC.
  67. public var status: EventLoopFuture<GRPCStatus> {
  68. if self.eventLoop.inEventLoop {
  69. return self.transport.responseContainer.lazyStatusPromise.getFutureResult()
  70. } else {
  71. return self.eventLoop.flatSubmit {
  72. return self.transport.responseContainer.lazyStatusPromise.getFutureResult()
  73. }
  74. }
  75. }
  76. internal init(
  77. path: String,
  78. scheme: String,
  79. authority: String,
  80. callOptions: CallOptions,
  81. eventLoop: EventLoop,
  82. multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>,
  83. errorDelegate: ClientErrorDelegate?,
  84. logger: Logger,
  85. request: RequestPayload
  86. ) {
  87. let requestID = callOptions.requestIDProvider.requestID()
  88. var logger = logger
  89. logger[metadataKey: MetadataKey.requestID] = "\(requestID)"
  90. logger[metadataKey: "path"] = "\(path)"
  91. let responsePromise: EventLoopPromise<ResponsePayload> = eventLoop.makePromise()
  92. self.transport = ChannelTransport(
  93. multiplexer: multiplexer,
  94. responseContainer: .init(eventLoop: eventLoop, unaryResponsePromise: responsePromise),
  95. callType: .unary,
  96. timeLimit: callOptions.timeLimit,
  97. errorDelegate: errorDelegate,
  98. logger: logger
  99. )
  100. self.options = callOptions
  101. self.response = responsePromise.futureResult
  102. let requestHead = _GRPCRequestHead(
  103. scheme: scheme,
  104. path: path,
  105. host: authority,
  106. requestID: requestID,
  107. options: callOptions
  108. )
  109. self.transport.sendUnary(
  110. requestHead,
  111. request: request,
  112. compressed: callOptions.messageEncoding.enabledForRequests
  113. )
  114. }
  115. }