ServerHandlerStateMachine+Draining.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. import NIOHPACK
  17. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  18. extension ServerHandlerStateMachine {
  19. /// In the 'Draining' state the user handler has been invoked and the request stream has been
  20. /// closed (i.e. we have seen 'end' but it has not necessarily been consumed by the user handler).
  21. /// We can transition to a new state either by sending the end of the response stream or by
  22. /// cancelling.
  23. @usableFromInline
  24. internal struct Draining {
  25. @usableFromInline
  26. typealias NextStateAndOutput<Output> =
  27. ServerHandlerStateMachine.NextStateAndOutput<
  28. ServerHandlerStateMachine.Draining.NextState,
  29. Output
  30. >
  31. /// The response headers.
  32. @usableFromInline
  33. internal private(set) var responseHeaders: ResponseMetadata
  34. /// The response trailers.
  35. @usableFromInline
  36. internal private(set) var responseTrailers: ResponseMetadata
  37. /// The request headers.
  38. @usableFromInline
  39. internal let requestHeaders: HPACKHeaders
  40. @inlinable
  41. init(from state: ServerHandlerStateMachine.Handling) {
  42. self.responseHeaders = state.responseHeaders
  43. self.responseTrailers = state.responseTrailers
  44. self.requestHeaders = state.requestHeaders
  45. }
  46. @inlinable
  47. mutating func setResponseHeaders(
  48. _ metadata: HPACKHeaders
  49. ) -> Self.NextStateAndOutput<Bool> {
  50. let output = self.responseHeaders.update(metadata)
  51. return .init(nextState: .draining(self), output: output)
  52. }
  53. @inlinable
  54. mutating func setResponseTrailers(
  55. _ metadata: HPACKHeaders
  56. ) -> Self.NextStateAndOutput<Void> {
  57. _ = self.responseTrailers.update(metadata)
  58. return .init(nextState: .draining(self))
  59. }
  60. @inlinable
  61. mutating func handleMetadata() -> Self.NextStateAndOutput<HandleMetadataAction> {
  62. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  63. return .init(nextState: .draining(self), output: .cancel)
  64. }
  65. @inlinable
  66. mutating func handleMessage() -> Self.NextStateAndOutput<HandleMessageAction> {
  67. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  68. return .init(nextState: .draining(self), output: .cancel)
  69. }
  70. @inlinable
  71. mutating func handleEnd() -> Self.NextStateAndOutput<HandleEndAction> {
  72. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  73. return .init(nextState: .draining(self), output: .cancel)
  74. }
  75. @inlinable
  76. mutating func sendMessage() -> Self.NextStateAndOutput<SendMessageAction> {
  77. let headers = self.responseHeaders.getIfNotWritten()
  78. return .init(nextState: .draining(self), output: .intercept(headers: headers))
  79. }
  80. @inlinable
  81. mutating func sendStatus() -> Self.NextStateAndOutput<SendStatusAction> {
  82. return .init(
  83. nextState: .finished(from: self),
  84. output: .intercept(
  85. requestHeaders: self.requestHeaders,
  86. // If trailers had been written we'd already be in the finished state so
  87. // the force unwrap is okay here.
  88. trailers: self.responseTrailers.getIfNotWritten()!
  89. )
  90. )
  91. }
  92. @inlinable
  93. mutating func cancel() -> Self.NextStateAndOutput<CancelAction> {
  94. return .init(nextState: .finished(from: self), output: .cancelAndNilOutHandlerComponents)
  95. }
  96. }
  97. }