ServerHandlerStateMachine+Draining.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2022, 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 NIOHPACK
  18. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  19. extension ServerHandlerStateMachine {
  20. /// In the 'Draining' state the user handler has been invoked and the request stream has been
  21. /// closed (i.e. we have seen 'end' but it has not necessarily been consumed by the user handler).
  22. /// We can transition to a new state either by sending the end of the response stream or by
  23. /// cancelling.
  24. internal struct Draining {
  25. typealias NextStateAndOutput<Output> =
  26. ServerHandlerStateMachine.NextStateAndOutput<
  27. ServerHandlerStateMachine.Draining.NextState,
  28. Output
  29. >
  30. /// Whether the response headers have been written yet.
  31. private var headersWritten: Bool
  32. internal let context: GRPCAsyncServerCallContext
  33. init(from state: ServerHandlerStateMachine.Handling) {
  34. self.headersWritten = state.headersWritten
  35. self.context = state.context
  36. }
  37. mutating func handleMetadata() -> Self.NextStateAndOutput<HandleMetadataAction> {
  38. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  39. return .init(nextState: .draining(self), output: .cancel)
  40. }
  41. mutating func handleMessage() -> Self.NextStateAndOutput<HandleMessageAction> {
  42. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  43. return .init(nextState: .draining(self), output: .cancel)
  44. }
  45. mutating func handleEnd() -> Self.NextStateAndOutput<HandleEndAction> {
  46. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  47. return .init(nextState: .draining(self), output: .cancel)
  48. }
  49. mutating func sendMessage() -> Self.NextStateAndOutput<SendMessageAction> {
  50. let headers: HPACKHeaders?
  51. if self.headersWritten {
  52. headers = nil
  53. } else {
  54. self.headersWritten = true
  55. headers = self.context.initialResponseMetadata
  56. }
  57. return .init(nextState: .draining(self), output: .intercept(headers: headers))
  58. }
  59. mutating func sendStatus() -> Self.NextStateAndOutput<SendStatusAction> {
  60. let trailers = self.context.trailingResponseMetadata
  61. return .init(nextState: .finished(from: self), output: .intercept(trailers: trailers))
  62. }
  63. mutating func cancel() -> Self.NextStateAndOutput<CancelAction> {
  64. return .init(nextState: .finished(from: self), output: .cancelAndNilOutHandlerComponents)
  65. }
  66. }
  67. }
  68. #endif // compiler(>=5.6)