Skip to content

Commit 152c21e

Browse files
authored
Add HTTPConnectionPool Connection as a box type (#398)
Add a `HTTPConnectionPool.Connection` type that is a box around an actual connection. The purpose of the box is to ensure no actions are invoked on the connection within the state machine. Further the box can be used for testing the state machine without creating actual connections.
1 parent 7311c0e commit 152c21e

File tree

1 file changed

+117
-1
lines changed

1 file changed

+117
-1
lines changed

Diff for: Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool.swift

+117-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,124 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import NIO
16+
1517
enum HTTPConnectionPool {
16-
struct Connection {
18+
struct Connection: Hashable {
1719
typealias ID = Int
20+
21+
// PLEASE NOTE:
22+
// The HTTP/1.1 connection code here is commented out, for a sad and simple reason: We
23+
// don't have a HTTP1Connection yet. As soon as the HTTP1Connection has landed
24+
// (https://github.com/swift-server/async-http-client/pull/400) we will enable
25+
// HTTP1Connections here. Landing the connection box now enables us to already review the
26+
// ConnectionPool StateMachines.
27+
28+
private enum Reference {
29+
// case http1_1(HTTP1Connection)
30+
31+
case __testOnly_connection(ID, EventLoop)
32+
}
33+
34+
private let _ref: Reference
35+
36+
// fileprivate static func http1_1(_ conn: HTTP1Connection) -> Self {
37+
// Connection(_ref: .http1_1(conn))
38+
// }
39+
40+
static func __testOnly_connection(id: ID, eventLoop: EventLoop) -> Self {
41+
Connection(_ref: .__testOnly_connection(id, eventLoop))
42+
}
43+
44+
var id: ID {
45+
switch self._ref {
46+
// case .http1_1(let connection):
47+
// return connection.id
48+
case .__testOnly_connection(let id, _):
49+
return id
50+
}
51+
}
52+
53+
var eventLoop: EventLoop {
54+
switch self._ref {
55+
// case .http1_1(let connection):
56+
// return connection.channel.eventLoop
57+
case .__testOnly_connection(_, let eventLoop):
58+
return eventLoop
59+
}
60+
}
61+
62+
@discardableResult
63+
fileprivate func close() -> EventLoopFuture<Void> {
64+
switch self._ref {
65+
// case .http1_1(let connection):
66+
// return connection.close()
67+
68+
case .__testOnly_connection(_, let eventLoop):
69+
return eventLoop.makeSucceededFuture(())
70+
}
71+
}
72+
73+
fileprivate func execute(request: HTTPExecutingRequest) {
74+
switch self._ref {
75+
// case .http1_1(let connection):
76+
// return connection.execute(request: request)
77+
case .__testOnly_connection:
78+
break
79+
}
80+
}
81+
82+
fileprivate func cancel() {
83+
switch self._ref {
84+
// case .http1_1(let connection):
85+
// return connection.cancel()
86+
case .__testOnly_connection:
87+
break
88+
}
89+
}
90+
91+
static func == (lhs: HTTPConnectionPool.Connection, rhs: HTTPConnectionPool.Connection) -> Bool {
92+
switch (lhs._ref, rhs._ref) {
93+
// case (.http1_1(let lhsConn), .http1_1(let rhsConn)):
94+
// return lhsConn === rhsConn
95+
case (.__testOnly_connection(let lhsID, let lhsEventLoop), .__testOnly_connection(let rhsID, let rhsEventLoop)):
96+
return lhsID == rhsID && lhsEventLoop === rhsEventLoop
97+
// default:
98+
// return false
99+
}
100+
}
101+
102+
func hash(into hasher: inout Hasher) {
103+
switch self._ref {
104+
case .__testOnly_connection(let id, let eventLoop):
105+
hasher.combine(id)
106+
hasher.combine(eventLoop.id)
107+
}
108+
}
109+
}
110+
}
111+
112+
struct EventLoopID: Hashable {
113+
private var id: Identifier
114+
115+
private enum Identifier: Hashable {
116+
case objectIdentifier(ObjectIdentifier)
117+
case __testOnly_fakeID(Int)
118+
}
119+
120+
init(_ eventLoop: EventLoop) {
121+
self.init(.objectIdentifier(ObjectIdentifier(eventLoop)))
18122
}
123+
124+
private init(_ id: Identifier) {
125+
self.id = id
126+
}
127+
128+
static func __testOnly_fakeID(_ id: Int) -> EventLoopID {
129+
return EventLoopID(.__testOnly_fakeID(id))
130+
}
131+
}
132+
133+
extension EventLoop {
134+
var id: EventLoopID { EventLoopID(self) }
19135
}

0 commit comments

Comments
 (0)