ServerInterceptorStateMachine.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. @usableFromInline
  17. internal struct ServerInterceptorStateMachine {
  18. @usableFromInline
  19. internal private(set) var state: Self.State
  20. @inlinable
  21. init() {
  22. self.state = .intercepting(.init())
  23. }
  24. @inlinable
  25. mutating func interceptRequestMetadata() -> InterceptAction {
  26. switch self.state {
  27. case var .intercepting(intercepting):
  28. let nextStateAndOutput = intercepting.interceptRequestMetadata()
  29. self.state = nextStateAndOutput.nextState.state
  30. return nextStateAndOutput.output
  31. case var .finished(finished):
  32. let nextStateAndOutput = finished.interceptRequestMetadata()
  33. self.state = nextStateAndOutput.nextState.state
  34. return nextStateAndOutput.output
  35. }
  36. }
  37. @inlinable
  38. mutating func interceptRequestMessage() -> InterceptAction {
  39. switch self.state {
  40. case var .intercepting(intercepting):
  41. let nextStateAndOutput = intercepting.interceptRequestMessage()
  42. self.state = nextStateAndOutput.nextState.state
  43. return nextStateAndOutput.output
  44. case var .finished(finished):
  45. let nextStateAndOutput = finished.interceptRequestMessage()
  46. self.state = nextStateAndOutput.nextState.state
  47. return nextStateAndOutput.output
  48. }
  49. }
  50. @inlinable
  51. mutating func interceptRequestEnd() -> InterceptAction {
  52. switch self.state {
  53. case var .intercepting(intercepting):
  54. let nextStateAndOutput = intercepting.interceptRequestEnd()
  55. self.state = nextStateAndOutput.nextState.state
  56. return nextStateAndOutput.output
  57. case var .finished(finished):
  58. let nextStateAndOutput = finished.interceptRequestEnd()
  59. self.state = nextStateAndOutput.nextState.state
  60. return nextStateAndOutput.output
  61. }
  62. }
  63. @inlinable
  64. mutating func interceptedRequestMetadata() -> InterceptedAction {
  65. switch self.state {
  66. case var .intercepting(intercepting):
  67. let nextStateAndOutput = intercepting.interceptedRequestMetadata()
  68. self.state = nextStateAndOutput.nextState.state
  69. return nextStateAndOutput.output
  70. case var .finished(finished):
  71. let nextStateAndOutput = finished.interceptedRequestMetadata()
  72. self.state = nextStateAndOutput.nextState.state
  73. return nextStateAndOutput.output
  74. }
  75. }
  76. @inlinable
  77. mutating func interceptedRequestMessage() -> InterceptedAction {
  78. switch self.state {
  79. case var .intercepting(intercepting):
  80. let nextStateAndOutput = intercepting.interceptedRequestMessage()
  81. self.state = nextStateAndOutput.nextState.state
  82. return nextStateAndOutput.output
  83. case var .finished(finished):
  84. let nextStateAndOutput = finished.interceptedRequestMessage()
  85. self.state = nextStateAndOutput.nextState.state
  86. return nextStateAndOutput.output
  87. }
  88. }
  89. @inlinable
  90. mutating func interceptedRequestEnd() -> InterceptedAction {
  91. switch self.state {
  92. case var .intercepting(intercepting):
  93. let nextStateAndOutput = intercepting.interceptedRequestEnd()
  94. self.state = nextStateAndOutput.nextState.state
  95. return nextStateAndOutput.output
  96. case var .finished(finished):
  97. let nextStateAndOutput = finished.interceptedRequestEnd()
  98. self.state = nextStateAndOutput.nextState.state
  99. return nextStateAndOutput.output
  100. }
  101. }
  102. @inlinable
  103. mutating func interceptResponseMetadata() -> InterceptAction {
  104. switch self.state {
  105. case var .intercepting(intercepting):
  106. let nextStateAndOutput = intercepting.interceptResponseMetadata()
  107. self.state = nextStateAndOutput.nextState.state
  108. return nextStateAndOutput.output
  109. case var .finished(finished):
  110. let nextStateAndOutput = finished.interceptResponseMetadata()
  111. self.state = nextStateAndOutput.nextState.state
  112. return nextStateAndOutput.output
  113. }
  114. }
  115. @inlinable
  116. mutating func interceptResponseMessage() -> InterceptAction {
  117. switch self.state {
  118. case var .intercepting(intercepting):
  119. let nextStateAndOutput = intercepting.interceptResponseMessage()
  120. self.state = nextStateAndOutput.nextState.state
  121. return nextStateAndOutput.output
  122. case var .finished(finished):
  123. let nextStateAndOutput = finished.interceptResponseMessage()
  124. self.state = nextStateAndOutput.nextState.state
  125. return nextStateAndOutput.output
  126. }
  127. }
  128. @inlinable
  129. mutating func interceptResponseStatus() -> InterceptAction {
  130. switch self.state {
  131. case var .intercepting(intercepting):
  132. let nextStateAndOutput = intercepting.interceptResponseStatus()
  133. self.state = nextStateAndOutput.nextState.state
  134. return nextStateAndOutput.output
  135. case var .finished(finished):
  136. let nextStateAndOutput = finished.interceptResponseStatus()
  137. self.state = nextStateAndOutput.nextState.state
  138. return nextStateAndOutput.output
  139. }
  140. }
  141. @inlinable
  142. mutating func interceptedResponseMetadata() -> InterceptedAction {
  143. switch self.state {
  144. case var .intercepting(intercepting):
  145. let nextStateAndOutput = intercepting.interceptedResponseMetadata()
  146. self.state = nextStateAndOutput.nextState.state
  147. return nextStateAndOutput.output
  148. case var .finished(finished):
  149. let nextStateAndOutput = finished.interceptedResponseMetadata()
  150. self.state = nextStateAndOutput.nextState.state
  151. return nextStateAndOutput.output
  152. }
  153. }
  154. @inlinable
  155. mutating func interceptedResponseMessage() -> InterceptedAction {
  156. switch self.state {
  157. case var .intercepting(intercepting):
  158. let nextStateAndOutput = intercepting.interceptedResponseMessage()
  159. self.state = nextStateAndOutput.nextState.state
  160. return nextStateAndOutput.output
  161. case var .finished(finished):
  162. let nextStateAndOutput = finished.interceptedResponseMessage()
  163. self.state = nextStateAndOutput.nextState.state
  164. return nextStateAndOutput.output
  165. }
  166. }
  167. @inlinable
  168. mutating func interceptedResponseStatus() -> InterceptedAction {
  169. switch self.state {
  170. case var .intercepting(intercepting):
  171. let nextStateAndOutput = intercepting.interceptedResponseStatus()
  172. self.state = nextStateAndOutput.nextState.state
  173. return nextStateAndOutput.output
  174. case var .finished(finished):
  175. let nextStateAndOutput = finished.interceptedResponseStatus()
  176. self.state = nextStateAndOutput.nextState.state
  177. return nextStateAndOutput.output
  178. }
  179. }
  180. @inlinable
  181. mutating func cancel() -> CancelAction {
  182. switch self.state {
  183. case var .intercepting(intercepting):
  184. let nextStateAndOutput = intercepting.cancel()
  185. self.state = nextStateAndOutput.nextState.state
  186. return nextStateAndOutput.output
  187. case var .finished(finished):
  188. let nextStateAndOutput = finished.cancel()
  189. self.state = nextStateAndOutput.nextState.state
  190. return nextStateAndOutput.output
  191. }
  192. }
  193. }
  194. extension ServerInterceptorStateMachine {
  195. /// The possible states the state machine may be in.
  196. @usableFromInline
  197. internal enum State {
  198. case intercepting(ServerInterceptorStateMachine.Intercepting)
  199. case finished(ServerInterceptorStateMachine.Finished)
  200. }
  201. }
  202. extension ServerInterceptorStateMachine {
  203. /// The next state to transition to and any output which may be produced as a
  204. /// result of a substate handling an action.
  205. @usableFromInline
  206. internal struct NextStateAndOutput<NextState, Output> {
  207. @usableFromInline
  208. internal var nextState: NextState
  209. @usableFromInline
  210. internal var output: Output
  211. @inlinable
  212. internal init(nextState: NextState, output: Output) {
  213. self.nextState = nextState
  214. self.output = output
  215. }
  216. }
  217. }
  218. extension ServerInterceptorStateMachine.NextStateAndOutput where Output == Void {
  219. internal init(nextState: NextState) {
  220. self.nextState = nextState
  221. self.output = ()
  222. }
  223. }
  224. extension ServerInterceptorStateMachine.Intercepting {
  225. /// States which can be reached directly from 'Intercepting'.
  226. @usableFromInline
  227. internal struct NextState {
  228. @usableFromInline
  229. let state: ServerInterceptorStateMachine.State
  230. @inlinable
  231. init(_state: ServerInterceptorStateMachine.State) {
  232. self.state = _state
  233. }
  234. @usableFromInline
  235. internal static func intercepting(_ state: ServerInterceptorStateMachine.Intercepting) -> Self {
  236. return Self(_state: .intercepting(state))
  237. }
  238. @usableFromInline
  239. internal static func finished(from: ServerInterceptorStateMachine.Intercepting) -> Self {
  240. return Self(_state: .finished(.init(from: from)))
  241. }
  242. }
  243. }
  244. extension ServerInterceptorStateMachine.Finished {
  245. /// States which can be reached directly from 'Finished'.
  246. @usableFromInline
  247. internal struct NextState {
  248. @usableFromInline
  249. let state: ServerInterceptorStateMachine.State
  250. @inlinable
  251. internal init(_state: ServerInterceptorStateMachine.State) {
  252. self.state = _state
  253. }
  254. @usableFromInline
  255. internal static func finished(_ state: ServerInterceptorStateMachine.Finished) -> Self {
  256. return Self(_state: .finished(state))
  257. }
  258. }
  259. }