@@ -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,31 @@ 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
+ return http1StateMachine. executeRequest ( request)
96
87
}
97
88
}
98
89
99
90
mutating func newHTTP1ConnectionCreated( _ connection: Connection ) -> Action {
100
91
switch self . state {
101
92
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) " )
93
+ return httpStateMachine. newHTTP1ConnectionEstablished ( connection)
110
94
}
111
95
}
112
96
113
97
mutating func failedToCreateNewConnection( _ error: Error , connectionID: Connection . ID ) -> Action {
114
98
switch self . state {
115
99
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) " )
100
+ return http1StateMachine. failedToCreateNewConnection (
101
+ error,
102
+ connectionID: connectionID
103
+ )
127
104
}
128
105
}
129
106
130
107
mutating func connectionCreationBackoffDone( _ connectionID: Connection . ID ) -> Action {
131
108
switch self . state {
132
109
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) " )
110
+ return http1StateMachine. connectionCreationBackoffDone ( connectionID)
141
111
}
142
112
}
143
113
@@ -149,14 +119,7 @@ extension HTTPConnectionPool {
149
119
mutating func timeoutRequest( _ requestID: Request . ID ) -> Action {
150
120
switch self . state {
151
121
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) " )
122
+ return http1StateMachine. timeoutRequest ( requestID)
160
123
}
161
124
}
162
125
@@ -168,55 +131,29 @@ extension HTTPConnectionPool {
168
131
mutating func cancelRequest( _ requestID: Request . ID ) -> Action {
169
132
switch self . state {
170
133
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) " )
134
+ return http1StateMachine. cancelRequest ( requestID)
179
135
}
180
136
}
181
137
182
138
mutating func connectionIdleTimeout( _ connectionID: Connection . ID ) -> Action {
183
139
switch self . state {
184
140
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) " )
141
+ return http1StateMachine. connectionIdleTimeout ( connectionID)
193
142
}
194
143
}
195
144
196
145
/// A connection has been closed
197
146
mutating func connectionClosed( _ connectionID: Connection . ID ) -> Action {
198
147
switch self . state {
199
148
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) " )
149
+ return http1StateMachine. connectionClosed ( connectionID)
208
150
}
209
151
}
210
152
211
153
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
217
- let action = http1StateMachine. http1ConnectionReleased ( connectionID)
218
- state = . http1( http1StateMachine)
219
- return action
154
+ switch self . state {
155
+ case . http1( var http1StateMachine) :
156
+ return http1StateMachine. http1ConnectionReleased ( connectionID)
220
157
}
221
158
}
222
159
@@ -229,14 +166,7 @@ extension HTTPConnectionPool {
229
166
230
167
switch self . state {
231
168
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) " )
169
+ return http1StateMachine. shutdown ( )
240
170
}
241
171
}
242
172
}
@@ -262,26 +192,11 @@ extension HTTPConnectionPool {
262
192
}
263
193
}
264
194
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
195
extension HTTPConnectionPool . StateMachine : CustomStringConvertible {
278
196
var description : String {
279
197
switch self . state {
280
198
case . http1( let http1) :
281
199
return " .http1( \( http1) ) "
282
-
283
- case . _modifying:
284
- preconditionFailure ( " Invalid state: \( self . state) " )
285
200
}
286
201
}
287
202
}
0 commit comments