Skip to content

Commit e69f32d

Browse files
committed
fix review comments from @Lukasa
1 parent dfd000d commit e69f32d

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+HTTP2Connections.swift

+19-24
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ extension HTTPConnectionPool {
1919
private enum State {
2020
/// the pool is establishing a connection. Valid transitions are to: .backingOff, .active and .closed
2121
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.
2424
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
2626
case active(Connection, maxStreams: Int, usedStreams: Int, lastIdle: NIODeadline)
2727
/// the connection is active and is running requests. No new requests must be scheduled.
2828
/// Valid transitions to: .draining and .closed
@@ -31,15 +31,6 @@ extension HTTPConnectionPool {
3131
case closed
3232
}
3333

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-
4334
var isStartingOrBackingOff: Bool {
4435
switch self.state {
4536
case .starting, .backingOff:
@@ -182,17 +173,17 @@ extension HTTPConnectionPool {
182173
preconditionFailure("Invalid state: \(self.state)")
183174

184175
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
187178
if usedStreams == 0 {
188179
lastIdle = .now()
189180
}
190181
self.state = .active(conn, maxStreams: maxStreams, usedStreams: usedStreams, lastIdle: lastIdle)
191-
return max(maxStreams - usedStreams, 0)
182+
return max(maxStreams &- usedStreams, 0)
192183

193184
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
196187
self.state = .draining(conn, maxStreams: maxStreams, usedStreams: usedStreams)
197188
return 0
198189
}
@@ -238,7 +229,8 @@ extension HTTPConnectionPool {
238229
return .removeConnection
239230

240231
case .active(let connection, _, let usedStreams, _):
241-
if usedStreams <= 0 {
232+
precondition(usedStreams >= 0)
233+
if usedStreams == 0 {
242234
context.close.append(connection)
243235
return .removeConnection
244236
} else {
@@ -256,9 +248,6 @@ extension HTTPConnectionPool {
256248
}
257249

258250
func addStats(into stats: inout HTTP2Connections.Stats) {
259-
if self.isIdle {
260-
stats.idleConnections &+= 1
261-
}
262251
switch self.state {
263252
case .starting:
264253
stats.startingConnections &+= 1
@@ -270,11 +259,17 @@ extension HTTPConnectionPool {
270259
stats.availableStreams += max(maxStreams - usedStreams, 0)
271260
stats.leasedStreams += usedStreams
272261
stats.availableConnections &+= 1
273-
262+
precondition(usedStreams >= 0)
263+
if usedStreams == 0 {
264+
stats.idleConnections &+= 1
265+
}
274266
case .draining(_, _, let usedStreams):
275267
stats.drainingConnections &+= 1
276268
stats.leasedStreams += usedStreams
277-
269+
precondition(usedStreams >= 0)
270+
if usedStreams == 0 {
271+
stats.idleConnections &+= 1
272+
}
278273
case .closed:
279274
break
280275
}
@@ -359,7 +354,7 @@ extension HTTPConnectionPool {
359354
return connection.connectionID
360355
}
361356

362-
/// A new HTTP/2.0 connection was established.
357+
/// A new HTTP/2 connection was established.
363358
///
364359
/// This will put the connection into the idle state.
365360
///

0 commit comments

Comments
 (0)