Skip to content

Commit abdabf1

Browse files
committed
Code review
1 parent 322fd45 commit abdabf1

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

Diff for: Tests/AsyncHTTPClientTests/Mocks/MockConnectionPool.swift

+21-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ struct MockConnectionPool {
4242
typealias ID = HTTPConnectionPool.Connection.ID
4343

4444
private enum State {
45+
// A note about idle vs. parked connections
46+
//
47+
// In our state machine we differentiate the concept of a connection being idle vs. it
48+
// being parked. An idle connection is a connection that we are not executing any
49+
// request on. A parked connection is an idle connection that will remain in this state
50+
// for a longer period of time. For parked connections we create idle timeout timers.
51+
//
52+
// Consider those two flows to better understand the difference:
53+
//
54+
// 1. A connection becomes `idle` and there is more work queued. This will lead to a new
55+
// request being executed on the connection. It will switch back to be in use without
56+
// having been parked in the meantime.
57+
//
58+
// 2. A connection becomes `idle` and there is no more work queued. We don't want to get
59+
// rid of the connection right away. For this reason we create an idle timeout timer
60+
// for the connection. After having created the timer we consider the connection
61+
// being parked. If a new request arrives, before the connection timed out, we need
62+
// to cancel the idle timeout timer.
63+
4564
enum HTTP1State {
4665
case inUse
4766
case idle(parked: Bool, idleSince: NIODeadline)
@@ -102,7 +121,7 @@ struct MockConnectionPool {
102121
}
103122
}
104123

105-
/// Is the connection idle and did we create a timeout timer for it?
124+
/// Is the connection idle and did we create an idle timeout timer for it?
106125
var isParked: Bool {
107126
switch self.state {
108127
case .starting, .closed, .http1(.inUse), .http2(.inUse):
@@ -192,9 +211,6 @@ struct MockConnectionPool {
192211
if let required = request.requiredEventLoop, required !== self.eventLoop {
193212
throw Errors.connectionDoesNotFulfillEventLoopRequirement
194213
}
195-
if maxStreams < 1 {
196-
throw Errors.connectionDoesNotHaveHTTP2StreamAvailable
197-
}
198214
self.state = .http2(.inUse(maxConcurrentStreams: maxStreams, used: 1))
199215

200216
case .http2(.inUse(let maxStreams, let used)):
@@ -223,7 +239,7 @@ struct MockConnectionPool {
223239
if used == 1 {
224240
self.state = .http2(.idle(maxConcurrentStreams: maxStreams, parked: false, lastIdle: .now()))
225241
} else {
226-
self.state = .http2(.inUse(maxConcurrentStreams: maxStreams, used: used))
242+
self.state = .http2(.inUse(maxConcurrentStreams: maxStreams, used: used - 1))
227243
}
228244

229245
case .closed:

0 commit comments

Comments
 (0)