ServerInterceptorStateMachine.swift 9.4 KB

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