ServerHandlerStateMachine+Draining.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. @usableFromInline
  25. internal struct Draining {
  26. @usableFromInline
  27. typealias NextStateAndOutput<Output> =
  28. ServerHandlerStateMachine.NextStateAndOutput<
  29. ServerHandlerStateMachine.Draining.NextState,
  30. Output
  31. >
  32. /// The response headers.
  33. @usableFromInline
  34. internal private(set) var responseHeaders: ResponseMetadata
  35. /// The response trailers.
  36. @usableFromInline
  37. internal private(set) var responseTrailers: ResponseMetadata
  38. /// The request headers.
  39. @usableFromInline
  40. internal let requestHeaders: HPACKHeaders
  41. @inlinable
  42. init(from state: ServerHandlerStateMachine.Handling) {
  43. self.responseHeaders = state.responseHeaders
  44. self.responseTrailers = state.responseTrailers
  45. self.requestHeaders = state.requestHeaders
  46. }
  47. @inlinable
  48. mutating func setResponseHeaders(
  49. _ metadata: HPACKHeaders
  50. ) -> Self.NextStateAndOutput<Void> {
  51. self.responseHeaders.update(metadata)
  52. return .init(nextState: .draining(self))
  53. }
  54. @inlinable
  55. mutating func setResponseTrailers(
  56. _ metadata: HPACKHeaders
  57. ) -> Self.NextStateAndOutput<Void> {
  58. self.responseTrailers.update(metadata)
  59. return .init(nextState: .draining(self))
  60. }
  61. @inlinable
  62. mutating func handleMetadata() -> Self.NextStateAndOutput<HandleMetadataAction> {
  63. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  64. return .init(nextState: .draining(self), output: .cancel)
  65. }
  66. @inlinable
  67. mutating func handleMessage() -> Self.NextStateAndOutput<HandleMessageAction> {
  68. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  69. return .init(nextState: .draining(self), output: .cancel)
  70. }
  71. @inlinable
  72. mutating func handleEnd() -> Self.NextStateAndOutput<HandleEndAction> {
  73. // We're already draining, i.e. the inbound stream is closed, cancel the RPC.
  74. return .init(nextState: .draining(self), output: .cancel)
  75. }
  76. @inlinable
  77. mutating func sendMessage() -> Self.NextStateAndOutput<SendMessageAction> {
  78. let headers = self.responseHeaders.getIfNotWritten()
  79. return .init(nextState: .draining(self), output: .intercept(headers: headers))
  80. }
  81. @inlinable
  82. mutating func sendStatus() -> Self.NextStateAndOutput<SendStatusAction> {
  83. return .init(
  84. nextState: .finished(from: self),
  85. output: .intercept(
  86. requestHeaders: self.requestHeaders,
  87. // If trailers had been written we'd already be in the finished state so
  88. // the force unwrap is okay here.
  89. trailers: self.responseTrailers.getIfNotWritten()!
  90. )
  91. )
  92. }
  93. @inlinable
  94. mutating func cancel() -> Self.NextStateAndOutput<CancelAction> {
  95. return .init(nextState: .finished(from: self), output: .cancelAndNilOutHandlerComponents)
  96. }
  97. }
  98. }
  99. #endif // compiler(>=5.6)