@@ -62,8 +62,6 @@ extension HTTPConnectionPool {
62
62
63
63
enum HTTPTypeStateMachine {
64
64
case http1( HTTP1StateMachine )
65
-
66
- case _modifying
67
65
}
68
66
69
67
var state : HTTPTypeStateMachine
@@ -85,59 +83,39 @@ extension HTTPConnectionPool {
85
83
mutating func executeRequest( _ request: Request ) -> Action {
86
84
switch self . state {
87
85
case . http1( var http1StateMachine) :
88
- return self . state. modify { state -> Action in
89
- let action = http1StateMachine. executeRequest ( request)
90
- state = . http1( http1StateMachine)
91
- return action
92
- }
93
-
94
- case . _modifying:
95
- preconditionFailure ( " Invalid state: \( self . state) " )
86
+ let action = http1StateMachine. executeRequest ( request)
87
+ self . state = . http1( http1StateMachine)
88
+ return action
96
89
}
97
90
}
98
91
99
92
mutating func newHTTP1ConnectionCreated( _ connection: Connection ) -> Action {
100
93
switch self . state {
101
- case . http1( var httpStateMachine) :
102
- return self . state. modify { state -> Action in
103
- let action = httpStateMachine. newHTTP1ConnectionEstablished ( connection)
104
- state = . http1( httpStateMachine)
105
- return action
106
- }
107
-
108
- case . _modifying:
109
- preconditionFailure ( " Invalid state: \( self . state) " )
94
+ case . http1( var http1StateMachine) :
95
+ let action = http1StateMachine. newHTTP1ConnectionEstablished ( connection)
96
+ self . state = . http1( http1StateMachine)
97
+ return action
110
98
}
111
99
}
112
100
113
101
mutating func failedToCreateNewConnection( _ error: Error , connectionID: Connection . ID ) -> Action {
114
102
switch self . state {
115
103
case . http1( var http1StateMachine) :
116
- return self . state. modify { state -> Action in
117
- let action = http1StateMachine. failedToCreateNewConnection (
118
- error,
119
- connectionID: connectionID
120
- )
121
- state = . http1( http1StateMachine)
122
- return action
123
- }
124
-
125
- case . _modifying:
126
- preconditionFailure ( " Invalid state: \( self . state) " )
104
+ let action = http1StateMachine. failedToCreateNewConnection (
105
+ error,
106
+ connectionID: connectionID
107
+ )
108
+ self . state = . http1( http1StateMachine)
109
+ return action
127
110
}
128
111
}
129
112
130
113
mutating func connectionCreationBackoffDone( _ connectionID: Connection . ID ) -> Action {
131
114
switch self . state {
132
115
case . http1( var http1StateMachine) :
133
- return self . state. modify { state -> Action in
134
- let action = http1StateMachine. connectionCreationBackoffDone ( connectionID)
135
- state = . http1( http1StateMachine)
136
- return action
137
- }
138
-
139
- case . _modifying:
140
- preconditionFailure ( " Invalid state: \( self . state) " )
116
+ let action = http1StateMachine. connectionCreationBackoffDone ( connectionID)
117
+ self . state = . http1( http1StateMachine)
118
+ return action
141
119
}
142
120
}
143
121
@@ -149,14 +127,9 @@ extension HTTPConnectionPool {
149
127
mutating func timeoutRequest( _ requestID: Request . ID ) -> Action {
150
128
switch self . state {
151
129
case . http1( var http1StateMachine) :
152
- return self . state. modify { state -> Action in
153
- let action = http1StateMachine. timeoutRequest ( requestID)
154
- state = . http1( http1StateMachine)
155
- return action
156
- }
157
-
158
- case . _modifying:
159
- preconditionFailure ( " Invalid state: \( self . state) " )
130
+ let action = http1StateMachine. timeoutRequest ( requestID)
131
+ self . state = . http1( http1StateMachine)
132
+ return action
160
133
}
161
134
}
162
135
@@ -168,54 +141,36 @@ extension HTTPConnectionPool {
168
141
mutating func cancelRequest( _ requestID: Request . ID ) -> Action {
169
142
switch self . state {
170
143
case . http1( var http1StateMachine) :
171
- return self . state. modify { state -> Action in
172
- let action = http1StateMachine. cancelRequest ( requestID)
173
- state = . http1( http1StateMachine)
174
- return action
175
- }
176
-
177
- case . _modifying:
178
- preconditionFailure ( " Invalid state: \( self . state) " )
144
+ let action = http1StateMachine. cancelRequest ( requestID)
145
+ self . state = . http1( http1StateMachine)
146
+ return action
179
147
}
180
148
}
181
149
182
150
mutating func connectionIdleTimeout( _ connectionID: Connection . ID ) -> Action {
183
151
switch self . state {
184
152
case . http1( var http1StateMachine) :
185
- return self . state. modify { state -> Action in
186
- let action = http1StateMachine. connectionIdleTimeout ( connectionID)
187
- state = . http1( http1StateMachine)
188
- return action
189
- }
190
-
191
- case . _modifying:
192
- preconditionFailure ( " Invalid state: \( self . state) " )
153
+ let action = http1StateMachine. connectionIdleTimeout ( connectionID)
154
+ self . state = . http1( http1StateMachine)
155
+ return action
193
156
}
194
157
}
195
158
196
159
/// A connection has been closed
197
160
mutating func connectionClosed( _ connectionID: Connection . ID ) -> Action {
198
161
switch self . state {
199
162
case . http1( var http1StateMachine) :
200
- return self . state. modify { state -> Action in
201
- let action = http1StateMachine. connectionClosed ( connectionID)
202
- state = . http1( http1StateMachine)
203
- return action
204
- }
205
-
206
- case . _modifying:
207
- preconditionFailure ( " Invalid state: \( self . state) " )
163
+ let action = http1StateMachine. connectionClosed ( connectionID)
164
+ self . state = . http1( http1StateMachine)
165
+ return action
208
166
}
209
167
}
210
168
211
169
mutating func http1ConnectionReleased( _ connectionID: Connection . ID ) -> Action {
212
- guard case . http1( var http1StateMachine) = self . state else {
213
- preconditionFailure ( " Invalid state: \( self . state) " )
214
- }
215
-
216
- return self . state. modify { state -> Action in
170
+ switch self . state {
171
+ case . http1( var http1StateMachine) :
217
172
let action = http1StateMachine. http1ConnectionReleased ( connectionID)
218
- state = . http1( http1StateMachine)
173
+ self . state = . http1( http1StateMachine)
219
174
return action
220
175
}
221
176
}
@@ -229,14 +184,9 @@ extension HTTPConnectionPool {
229
184
230
185
switch self . state {
231
186
case . http1( var http1StateMachine) :
232
- return self . state. modify { state -> Action in
233
- let action = http1StateMachine. shutdown ( )
234
- state = . http1( http1StateMachine)
235
- return action
236
- }
237
-
238
- case . _modifying:
239
- preconditionFailure ( " Invalid state: \( self . state) " )
187
+ let action = http1StateMachine. shutdown ( )
188
+ self . state = . http1( http1StateMachine)
189
+ return action
240
190
}
241
191
}
242
192
}
@@ -262,26 +212,11 @@ extension HTTPConnectionPool {
262
212
}
263
213
}
264
214
265
- extension HTTPConnectionPool . StateMachine . HTTPTypeStateMachine {
266
- mutating func modify< T> ( _ closure: ( inout Self ) throws -> ( T ) ) rethrows -> T {
267
- self = . _modifying
268
- defer {
269
- if case . _modifying = self {
270
- preconditionFailure ( " Invalid state. Use closure to modify state " )
271
- }
272
- }
273
- return try closure ( & self )
274
- }
275
- }
276
-
277
215
extension HTTPConnectionPool . StateMachine : CustomStringConvertible {
278
216
var description : String {
279
217
switch self . state {
280
218
case . http1( let http1) :
281
219
return " .http1( \( http1) ) "
282
-
283
- case . _modifying:
284
- preconditionFailure ( " Invalid state: \( self . state) " )
285
220
}
286
221
}
287
222
}
0 commit comments