UnaryResponseCallContext.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Logging
  18. import NIOCore
  19. import NIOHPACK
  20. import NIOHTTP1
  21. import SwiftProtobuf
  22. /// A context provided to handlers for RPCs which return a single response, i.e. unary and client
  23. /// streaming RPCs.
  24. ///
  25. /// For client streaming RPCs the handler must complete the `responsePromise` to return the response
  26. /// to the client. Unary RPCs do complete the promise directly: they are provided an
  27. /// ``StatusOnlyCallContext`` view of this context where the `responsePromise` is not exposed. Instead
  28. /// they must return an `EventLoopFuture<Response>` from the method they are implementing.
  29. open class UnaryResponseCallContext<Response>: ServerCallContextBase, StatusOnlyCallContext {
  30. /// A promise for a single response message. This must be completed to send a response back to the
  31. /// client. If the promise is failed, the failure value will be converted to ``GRPCStatus`` and
  32. /// used as the final status for the RPC.
  33. public let responsePromise: EventLoopPromise<Response>
  34. /// The status sent back to the client at the end of the RPC, providing the `responsePromise` was
  35. /// completed successfully.
  36. ///
  37. /// - Important: This *must* be accessed from the context's `eventLoop` in order to ensure
  38. /// thread-safety.
  39. public var responseStatus: GRPCStatus {
  40. get {
  41. self.eventLoop.assertInEventLoop()
  42. return self._responseStatus
  43. }
  44. set {
  45. self.eventLoop.assertInEventLoop()
  46. self._responseStatus = newValue
  47. }
  48. }
  49. private var _responseStatus: GRPCStatus = .ok
  50. @available(*, deprecated, renamed: "init(eventLoop:headers:logger:userInfo:closeFuture:)")
  51. public convenience init(
  52. eventLoop: EventLoop,
  53. headers: HPACKHeaders,
  54. logger: Logger,
  55. userInfo: UserInfo = UserInfo()
  56. ) {
  57. self.init(
  58. eventLoop: eventLoop,
  59. headers: headers,
  60. logger: logger,
  61. userInfoRef: .init(userInfo),
  62. closeFuture: eventLoop.makeFailedFuture(GRPCStatus.closeFutureNotImplemented)
  63. )
  64. }
  65. public convenience init(
  66. eventLoop: EventLoop,
  67. headers: HPACKHeaders,
  68. logger: Logger,
  69. userInfo: UserInfo = UserInfo(),
  70. closeFuture: EventLoopFuture<Void>
  71. ) {
  72. self.init(
  73. eventLoop: eventLoop,
  74. headers: headers,
  75. logger: logger,
  76. userInfoRef: .init(userInfo),
  77. closeFuture: closeFuture
  78. )
  79. }
  80. @inlinable
  81. override internal init(
  82. eventLoop: EventLoop,
  83. headers: HPACKHeaders,
  84. logger: Logger,
  85. userInfoRef: Ref<UserInfo>,
  86. closeFuture: EventLoopFuture<Void>
  87. ) {
  88. self.responsePromise = eventLoop.makePromise()
  89. super.init(
  90. eventLoop: eventLoop,
  91. headers: headers,
  92. logger: logger,
  93. userInfoRef: userInfoRef,
  94. closeFuture: closeFuture
  95. )
  96. }
  97. }
  98. /// Protocol variant of ``UnaryResponseCallContext`` that only exposes the ``responseStatus`` and ``trailers``
  99. /// fields, but not `responsePromise`.
  100. ///
  101. /// We can use a protocol (instead of an abstract base class) here because removing the generic
  102. /// `responsePromise` field lets us avoid associated-type requirements on the protocol.
  103. public protocol StatusOnlyCallContext: ServerCallContext {
  104. /// The status sent back to the client at the end of the RPC, providing the `responsePromise` was
  105. /// completed successfully.
  106. var responseStatus: GRPCStatus { get set }
  107. /// Metadata to return at the end of the RPC.
  108. var trailers: HPACKHeaders { get set }
  109. }
  110. /// Concrete implementation of `UnaryResponseCallContext` used for testing.
  111. ///
  112. /// Only provided to make it clear in tests that no "real" implementation is used.
  113. open class UnaryResponseCallContextTestStub<Response>: UnaryResponseCallContext<Response> {}