Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a20721e

Browse files
committedSep 22, 2021
[HTTPConnectionPool] Move Connections down in the file
1 parent 64fbfda commit a20721e

File tree

1 file changed

+109
-106
lines changed

1 file changed

+109
-106
lines changed
 

‎Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool.swift

+109-106
Original file line numberDiff line numberDiff line change
@@ -22,112 +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-
}
13125

13226
private let stateLock = Lock()
13327
private var _state: StateMachine
@@ -530,6 +424,115 @@ extension HTTPConnectionPool: HTTPRequestScheduler {
530424
}
531425
}
532426

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

0 commit comments

Comments
 (0)
Please sign in to comment.