@@ -19,10 +19,10 @@ extension HTTPConnectionPool {
19
19
private enum State {
20
20
/// the pool is establishing a connection. Valid transitions are to: .backingOff, .active and .closed
21
21
case starting
22
- /// the connection is waiting to retry to establish a connection. Transition to .closed. From .closed
23
- /// a new connection state must be created for a retry.
22
+ /// the connection is waiting to retry to establish a connection. Valid transitions are to .closed.
23
+ /// From .closed a new connection state must be created for a retry.
24
24
case backingOff
25
- /// the connection is active and is able to run requests. Valid transitions to: .draining and .closed
25
+ /// the connection is active and is able to run requests. Valid transitions are to: .draining and .closed
26
26
case active( Connection , maxStreams: Int , usedStreams: Int , lastIdle: NIODeadline )
27
27
/// the connection is active and is running requests. No new requests must be scheduled.
28
28
/// Valid transitions to: .draining and .closed
@@ -31,15 +31,6 @@ extension HTTPConnectionPool {
31
31
case closed
32
32
}
33
33
34
- var isActive : Bool {
35
- switch self . state {
36
- case . starting, . backingOff, . draining, . closed:
37
- return false
38
- case . active:
39
- return true
40
- }
41
- }
42
-
43
34
var isStartingOrBackingOff : Bool {
44
35
switch self . state {
45
36
case . starting, . backingOff:
@@ -182,17 +173,17 @@ extension HTTPConnectionPool {
182
173
preconditionFailure ( " Invalid state: \( self . state) " )
183
174
184
175
case . active( let conn, let maxStreams, var usedStreams, var lastIdle) :
185
- usedStreams -= 1
186
- assert ( usedStreams >= 0 )
176
+ precondition ( usedStreams > 0 , " we cannot release more streams than we have leased " )
177
+ usedStreams &-= 1
187
178
if usedStreams == 0 {
188
179
lastIdle = . now( )
189
180
}
190
181
self . state = . active( conn, maxStreams: maxStreams, usedStreams: usedStreams, lastIdle: lastIdle)
191
- return max ( maxStreams - usedStreams, 0 )
182
+ return max ( maxStreams & - usedStreams, 0 )
192
183
193
184
case . draining( let conn, let maxStreams, var usedStreams) :
194
- usedStreams -= 1
195
- assert ( usedStreams >= 0 )
185
+ precondition ( usedStreams > 0 , " we cannot release more streams than we have leased " )
186
+ usedStreams &-= 1
196
187
self . state = . draining( conn, maxStreams: maxStreams, usedStreams: usedStreams)
197
188
return 0
198
189
}
@@ -238,7 +229,8 @@ extension HTTPConnectionPool {
238
229
return . removeConnection
239
230
240
231
case . active( let connection, _, let usedStreams, _) :
241
- if usedStreams <= 0 {
232
+ precondition ( usedStreams >= 0 )
233
+ if usedStreams == 0 {
242
234
context. close. append ( connection)
243
235
return . removeConnection
244
236
} else {
@@ -256,9 +248,6 @@ extension HTTPConnectionPool {
256
248
}
257
249
258
250
func addStats( into stats: inout HTTP2Connections . Stats ) {
259
- if self . isIdle {
260
- stats. idleConnections &+= 1
261
- }
262
251
switch self . state {
263
252
case . starting:
264
253
stats. startingConnections &+= 1
@@ -270,11 +259,17 @@ extension HTTPConnectionPool {
270
259
stats. availableStreams += max ( maxStreams - usedStreams, 0 )
271
260
stats. leasedStreams += usedStreams
272
261
stats. availableConnections &+= 1
273
-
262
+ precondition ( usedStreams >= 0 )
263
+ if usedStreams == 0 {
264
+ stats. idleConnections &+= 1
265
+ }
274
266
case . draining( _, _, let usedStreams) :
275
267
stats. drainingConnections &+= 1
276
268
stats. leasedStreams += usedStreams
277
-
269
+ precondition ( usedStreams >= 0 )
270
+ if usedStreams == 0 {
271
+ stats. idleConnections &+= 1
272
+ }
278
273
case . closed:
279
274
break
280
275
}
@@ -359,7 +354,7 @@ extension HTTPConnectionPool {
359
354
return connection. connectionID
360
355
}
361
356
362
- /// A new HTTP/2.0 connection was established.
357
+ /// A new HTTP/2 connection was established.
363
358
///
364
359
/// This will put the connection into the idle state.
365
360
///
0 commit comments