Skip to content

Commit d45fa9a

Browse files
authored
[HTTPConnectionPool] Move Connections down in the file (#433)
1 parent 64fbfda commit d45fa9a

File tree

1 file changed

+109
-107
lines changed

1 file changed

+109
-107
lines changed

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

+109-107
Original file line numberDiff line numberDiff line change
@@ -22,113 +22,6 @@ protocol HTTPConnectionPoolDelegate {
2222
}
2323

2424
final class HTTPConnectionPool {
25-
struct Connection: Hashable {
26-
typealias ID = Int
27-
28-
private enum Reference {
29-
case http1_1(HTTP1Connection)
30-
case http2(HTTP2Connection)
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-
fileprivate static func http2(_ conn: HTTP2Connection) -> Self {
41-
Connection(_ref: .http2(conn))
42-
}
43-
44-
static func __testOnly_connection(id: ID, eventLoop: EventLoop) -> Self {
45-
Connection(_ref: .__testOnly_connection(id, eventLoop))
46-
}
47-
48-
var id: ID {
49-
switch self._ref {
50-
case .http1_1(let connection):
51-
return connection.id
52-
case .http2(let connection):
53-
return connection.id
54-
case .__testOnly_connection(let id, _):
55-
return id
56-
}
57-
}
58-
59-
var eventLoop: EventLoop {
60-
switch self._ref {
61-
case .http1_1(let connection):
62-
return connection.channel.eventLoop
63-
case .http2(let connection):
64-
return connection.channel.eventLoop
65-
case .__testOnly_connection(_, let eventLoop):
66-
return eventLoop
67-
}
68-
}
69-
70-
fileprivate func executeRequest(_ request: HTTPExecutableRequest) {
71-
switch self._ref {
72-
case .http1_1(let connection):
73-
return connection.executeRequest(request)
74-
case .http2(let connection):
75-
return connection.executeRequest(request)
76-
case .__testOnly_connection:
77-
break
78-
}
79-
}
80-
81-
/// Shutdown cancels any running requests on the connection and then closes the connection
82-
fileprivate func shutdown() {
83-
switch self._ref {
84-
case .http1_1(let connection):
85-
return connection.shutdown()
86-
case .http2(let connection):
87-
return connection.shutdown()
88-
case .__testOnly_connection:
89-
break
90-
}
91-
}
92-
93-
/// Closes the connection without cancelling running requests. Use this when you are sure, that the
94-
/// connection is currently idle.
95-
fileprivate func close(promise: EventLoopPromise<Void>?) {
96-
switch self._ref {
97-
case .http1_1(let connection):
98-
return connection.close(promise: promise)
99-
case .http2(let connection):
100-
return connection.close(promise: promise)
101-
case .__testOnly_connection:
102-
promise?.succeed(())
103-
}
104-
}
105-
106-
static func == (lhs: HTTPConnectionPool.Connection, rhs: HTTPConnectionPool.Connection) -> Bool {
107-
switch (lhs._ref, rhs._ref) {
108-
case (.http1_1(let lhsConn), .http1_1(let rhsConn)):
109-
return lhsConn.id == rhsConn.id
110-
case (.http2(let lhsConn), .http2(let rhsConn)):
111-
return lhsConn.id == rhsConn.id
112-
case (.__testOnly_connection(let lhsID, let lhsEventLoop), .__testOnly_connection(let rhsID, let rhsEventLoop)):
113-
return lhsID == rhsID && lhsEventLoop === rhsEventLoop
114-
default:
115-
return false
116-
}
117-
}
118-
119-
func hash(into hasher: inout Hasher) {
120-
switch self._ref {
121-
case .http1_1(let conn):
122-
hasher.combine(conn.id)
123-
case .http2(let conn):
124-
hasher.combine(conn.id)
125-
case .__testOnly_connection(let id, let eventLoop):
126-
hasher.combine(id)
127-
hasher.combine(eventLoop.id)
128-
}
129-
}
130-
}
131-
13225
private let stateLock = Lock()
13326
private var _state: StateMachine
13427

@@ -530,6 +423,115 @@ extension HTTPConnectionPool: HTTPRequestScheduler {
530423
}
531424
}
532425

426+
extension HTTPConnectionPool {
427+
struct Connection: Hashable {
428+
typealias ID = Int
429+
430+
private enum Reference {
431+
case http1_1(HTTP1Connection)
432+
case http2(HTTP2Connection)
433+
case __testOnly_connection(ID, EventLoop)
434+
}
435+
436+
private let _ref: Reference
437+
438+
fileprivate static func http1_1(_ conn: HTTP1Connection) -> Self {
439+
Connection(_ref: .http1_1(conn))
440+
}
441+
442+
fileprivate static func http2(_ conn: HTTP2Connection) -> Self {
443+
Connection(_ref: .http2(conn))
444+
}
445+
446+
static func __testOnly_connection(id: ID, eventLoop: EventLoop) -> Self {
447+
Connection(_ref: .__testOnly_connection(id, eventLoop))
448+
}
449+
450+
var id: ID {
451+
switch self._ref {
452+
case .http1_1(let connection):
453+
return connection.id
454+
case .http2(let connection):
455+
return connection.id
456+
case .__testOnly_connection(let id, _):
457+
return id
458+
}
459+
}
460+
461+
var eventLoop: EventLoop {
462+
switch self._ref {
463+
case .http1_1(let connection):
464+
return connection.channel.eventLoop
465+
case .http2(let connection):
466+
return connection.channel.eventLoop
467+
case .__testOnly_connection(_, let eventLoop):
468+
return eventLoop
469+
}
470+
}
471+
472+
fileprivate func executeRequest(_ request: HTTPExecutableRequest) {
473+
switch self._ref {
474+
case .http1_1(let connection):
475+
return connection.executeRequest(request)
476+
case .http2(let connection):
477+
return connection.executeRequest(request)
478+
case .__testOnly_connection:
479+
break
480+
}
481+
}
482+
483+
/// Shutdown cancels any running requests on the connection and then closes the connection
484+
fileprivate func shutdown() {
485+
switch self._ref {
486+
case .http1_1(let connection):
487+
return connection.shutdown()
488+
case .http2(let connection):
489+
return connection.shutdown()
490+
case .__testOnly_connection:
491+
break
492+
}
493+
}
494+
495+
/// Closes the connection without cancelling running requests. Use this when you are sure, that the
496+
/// connection is currently idle.
497+
fileprivate func close(promise: EventLoopPromise<Void>?) {
498+
switch self._ref {
499+
case .http1_1(let connection):
500+
return connection.close(promise: promise)
501+
case .http2(let connection):
502+
return connection.close(promise: promise)
503+
case .__testOnly_connection:
504+
promise?.succeed(())
505+
}
506+
}
507+
508+
static func == (lhs: HTTPConnectionPool.Connection, rhs: HTTPConnectionPool.Connection) -> Bool {
509+
switch (lhs._ref, rhs._ref) {
510+
case (.http1_1(let lhsConn), .http1_1(let rhsConn)):
511+
return lhsConn.id == rhsConn.id
512+
case (.http2(let lhsConn), .http2(let rhsConn)):
513+
return lhsConn.id == rhsConn.id
514+
case (.__testOnly_connection(let lhsID, let lhsEventLoop), .__testOnly_connection(let rhsID, let rhsEventLoop)):
515+
return lhsID == rhsID && lhsEventLoop === rhsEventLoop
516+
default:
517+
return false
518+
}
519+
}
520+
521+
func hash(into hasher: inout Hasher) {
522+
switch self._ref {
523+
case .http1_1(let conn):
524+
hasher.combine(conn.id)
525+
case .http2(let conn):
526+
hasher.combine(conn.id)
527+
case .__testOnly_connection(let id, let eventLoop):
528+
hasher.combine(id)
529+
hasher.combine(eventLoop.id)
530+
}
531+
}
532+
}
533+
}
534+
533535
extension HTTPConnectionPool {
534536
/// This is a wrapper that we use inside the connection pool state machine to ensure that
535537
/// the actual request can not be accessed at any time. Further it exposes all that is needed within

0 commit comments

Comments
 (0)