@@ -18,89 +18,103 @@ enum HTTPConnectionPool {
18
18
struct Connection : Hashable {
19
19
typealias ID = Int
20
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
21
private enum Reference {
29
- // case http1_1(HTTP1Connection)
30
-
22
+ case http1_1( HTTP1Connection )
23
+ case http2 ( HTTP2Connection )
31
24
case __testOnly_connection( ID , EventLoop )
32
25
}
33
26
34
27
private let _ref : Reference
35
28
36
- // fileprivate static func http1_1(_ conn: HTTP1Connection) -> Self {
37
- // Connection(_ref: .http1_1(conn))
38
- // }
29
+ fileprivate static func http1_1( _ conn: HTTP1Connection ) -> Self {
30
+ Connection ( _ref: . http1_1( conn) )
31
+ }
32
+
33
+ fileprivate static func http2( _ conn: HTTP2Connection ) -> Self {
34
+ Connection ( _ref: . http2( conn) )
35
+ }
39
36
40
37
static func __testOnly_connection( id: ID , eventLoop: EventLoop ) -> Self {
41
38
Connection ( _ref: . __testOnly_connection( id, eventLoop) )
42
39
}
43
40
44
41
var id : ID {
45
42
switch self . _ref {
46
- // case .http1_1(let connection):
47
- // return connection.id
43
+ case . http1_1( let connection) :
44
+ return connection. id
45
+ case . http2( let connection) :
46
+ return connection. id
48
47
case . __testOnly_connection( let id, _) :
49
48
return id
50
49
}
51
50
}
52
51
53
52
var eventLoop : EventLoop {
54
53
switch self . _ref {
55
- // case .http1_1(let connection):
56
- // return connection.channel.eventLoop
54
+ case . http1_1( let connection) :
55
+ return connection. channel. eventLoop
56
+ case . http2( let connection) :
57
+ return connection. channel. eventLoop
57
58
case . __testOnly_connection( _, let eventLoop) :
58
59
return eventLoop
59
60
}
60
61
}
61
62
62
- @discardableResult
63
- fileprivate func close( ) -> EventLoopFuture < Void > {
63
+ fileprivate func executeRequest( _ request: HTTPExecutableRequest ) {
64
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 ( ( ) )
65
+ case . http1_1( let connection) :
66
+ return connection. executeRequest ( request)
67
+ case . http2( let connection) :
68
+ return connection. executeRequest ( request)
69
+ case . __testOnly_connection:
70
+ break
70
71
}
71
72
}
72
73
73
- fileprivate func execute( request: HTTPExecutableRequest ) {
74
+ /// Shutdown cancels any running requests on the connection and then closes the connection
75
+ fileprivate func shutdown( ) {
74
76
switch self . _ref {
75
- // case .http1_1(let connection):
76
- // return connection.execute(request: request)
77
+ case . http1_1( let connection) :
78
+ return connection. shutdown ( )
79
+ case . http2( let connection) :
80
+ return connection. shutdown ( )
77
81
case . __testOnly_connection:
78
82
break
79
83
}
80
84
}
81
85
82
- fileprivate func cancel( ) {
86
+ /// Closes the connection without cancelling running requests. Use this when you are sure, that the
87
+ /// connection is currently idle.
88
+ fileprivate func close( ) -> EventLoopFuture < Void > {
83
89
switch self . _ref {
84
- // case .http1_1(let connection):
85
- // return connection.cancel()
86
- case . __testOnly_connection:
87
- break
90
+ case . http1_1( let connection) :
91
+ return connection. close ( )
92
+ case . http2( let connection) :
93
+ return connection. close ( )
94
+ case . __testOnly_connection( _, let eventLoop) :
95
+ return eventLoop. makeSucceededFuture ( ( ) )
88
96
}
89
97
}
90
98
91
99
static func == ( lhs: HTTPConnectionPool . Connection , rhs: HTTPConnectionPool . Connection ) -> Bool {
92
100
switch ( lhs. _ref, rhs. _ref) {
93
- // case (.http1_1(let lhsConn), .http1_1(let rhsConn)):
94
- // return lhsConn === rhsConn
101
+ case ( . http1_1( let lhsConn) , . http1_1( let rhsConn) ) :
102
+ return lhsConn. id == rhsConn. id
103
+ case ( . http2( let lhsConn) , . http2( let rhsConn) ) :
104
+ return lhsConn. id == rhsConn. id
95
105
case ( . __testOnly_connection( let lhsID, let lhsEventLoop) , . __testOnly_connection( let rhsID, let rhsEventLoop) ) :
96
106
return lhsID == rhsID && lhsEventLoop === rhsEventLoop
97
- // default:
98
- // return false
107
+ default :
108
+ return false
99
109
}
100
110
}
101
111
102
112
func hash( into hasher: inout Hasher ) {
103
113
switch self . _ref {
114
+ case . http1_1( let conn) :
115
+ hasher. combine ( conn. id)
116
+ case . http2( let conn) :
117
+ hasher. combine ( conn. id)
104
118
case . __testOnly_connection( let id, let eventLoop) :
105
119
hasher. combine ( id)
106
120
hasher. combine ( eventLoop. id)
0 commit comments