UnaryResponseCallContext.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 NIO
  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. public convenience init(
  51. eventLoop: EventLoop,
  52. headers: HPACKHeaders,
  53. logger: Logger,
  54. userInfo: UserInfo = UserInfo()
  55. ) {
  56. self.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: .init(userInfo))
  57. }
  58. @inlinable
  59. override internal init(
  60. eventLoop: EventLoop,
  61. headers: HPACKHeaders,
  62. logger: Logger,
  63. userInfoRef: Ref<UserInfo>
  64. ) {
  65. self.responsePromise = eventLoop.makePromise()
  66. super.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: userInfoRef)
  67. }
  68. }
  69. /// Protocol variant of `UnaryResponseCallContext` that only exposes the `responseStatus` and `trailingMetadata`
  70. /// fields, but not `responsePromise`.
  71. ///
  72. /// We can use a protocol (instead of an abstract base class) here because removing the generic
  73. /// `responsePromise` field lets us avoid associated-type requirements on the protocol.
  74. public protocol StatusOnlyCallContext: ServerCallContext {
  75. /// The status sent back to the client at the end of the RPC, providing the `responsePromise` was
  76. /// completed successfully.
  77. var responseStatus: GRPCStatus { get set }
  78. /// Metadata to return at the end of the RPC.
  79. var trailers: HPACKHeaders { get set }
  80. }
  81. /// Concrete implementation of `UnaryResponseCallContext` used for testing.
  82. ///
  83. /// Only provided to make it clear in tests that no "real" implementation is used.
  84. open class UnaryResponseCallContextTestStub<Response>: UnaryResponseCallContext<Response> {}