forked from swift-server/async-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPConnectionPool.swift
192 lines (163 loc) · 6.17 KB
/
HTTPConnectionPool.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
enum HTTPConnectionPool {
struct Connection: Hashable {
typealias ID = Int
private enum Reference {
case http1_1(HTTP1Connection)
case http2(HTTP2Connection)
case __testOnly_connection(ID, EventLoop)
}
private let _ref: Reference
fileprivate static func http1_1(_ conn: HTTP1Connection) -> Self {
Connection(_ref: .http1_1(conn))
}
fileprivate static func http2(_ conn: HTTP2Connection) -> Self {
Connection(_ref: .http2(conn))
}
static func __testOnly_connection(id: ID, eventLoop: EventLoop) -> Self {
Connection(_ref: .__testOnly_connection(id, eventLoop))
}
var id: ID {
switch self._ref {
case .http1_1(let connection):
return connection.id
case .http2(let connection):
return connection.id
case .__testOnly_connection(let id, _):
return id
}
}
var eventLoop: EventLoop {
switch self._ref {
case .http1_1(let connection):
return connection.channel.eventLoop
case .http2(let connection):
return connection.channel.eventLoop
case .__testOnly_connection(_, let eventLoop):
return eventLoop
}
}
fileprivate func executeRequest(_ request: HTTPExecutableRequest) {
switch self._ref {
case .http1_1(let connection):
return connection.executeRequest(request)
case .http2(let connection):
return connection.executeRequest(request)
case .__testOnly_connection:
break
}
}
/// Shutdown cancels any running requests on the connection and then closes the connection
fileprivate func shutdown() {
switch self._ref {
case .http1_1(let connection):
return connection.shutdown()
case .http2(let connection):
return connection.shutdown()
case .__testOnly_connection:
break
}
}
/// Closes the connection without cancelling running requests. Use this when you are sure, that the
/// connection is currently idle.
fileprivate func close() -> EventLoopFuture<Void> {
switch self._ref {
case .http1_1(let connection):
return connection.close()
case .http2(let connection):
return connection.close()
case .__testOnly_connection(_, let eventLoop):
return eventLoop.makeSucceededFuture(())
}
}
static func == (lhs: HTTPConnectionPool.Connection, rhs: HTTPConnectionPool.Connection) -> Bool {
switch (lhs._ref, rhs._ref) {
case (.http1_1(let lhsConn), .http1_1(let rhsConn)):
return lhsConn.id == rhsConn.id
case (.http2(let lhsConn), .http2(let rhsConn)):
return lhsConn.id == rhsConn.id
case (.__testOnly_connection(let lhsID, let lhsEventLoop), .__testOnly_connection(let rhsID, let rhsEventLoop)):
return lhsID == rhsID && lhsEventLoop === rhsEventLoop
default:
return false
}
}
func hash(into hasher: inout Hasher) {
switch self._ref {
case .http1_1(let conn):
hasher.combine(conn.id)
case .http2(let conn):
hasher.combine(conn.id)
case .__testOnly_connection(let id, let eventLoop):
hasher.combine(id)
hasher.combine(eventLoop.id)
}
}
}
}
extension HTTPConnectionPool {
/// This is a wrapper that we use inside the connection pool state machine to ensure that
/// the actual request can not be accessed at any time. Further it exposes all that is needed within
/// the state machine. A request ID and the `EventLoop` requirement.
struct Request {
struct ID: Hashable {
let objectIdentifier: ObjectIdentifier
let eventLoopID: EventLoopID?
fileprivate init(_ request: HTTPSchedulableRequest, eventLoopRequirement eventLoopID: EventLoopID?) {
self.objectIdentifier = ObjectIdentifier(request)
self.eventLoopID = eventLoopID
}
}
fileprivate let req: HTTPSchedulableRequest
init(_ request: HTTPSchedulableRequest) {
self.req = request
}
var id: HTTPConnectionPool.Request.ID {
HTTPConnectionPool.Request.ID(self.req, eventLoopRequirement: self.requiredEventLoop?.id)
}
var requiredEventLoop: EventLoop? {
self.req.requiredEventLoop
}
var preferredEventLoop: EventLoop {
self.req.preferredEventLoop
}
var connectionDeadline: NIODeadline? {
self.req.connectionDeadline
}
func __testOnly_wrapped_request() -> HTTPSchedulableRequest {
self.req
}
}
}
struct EventLoopID: Hashable {
private var id: Identifier
private enum Identifier: Hashable {
case objectIdentifier(ObjectIdentifier)
case __testOnly_fakeID(Int)
}
init(_ eventLoop: EventLoop) {
self.init(.objectIdentifier(ObjectIdentifier(eventLoop)))
}
private init(_ id: Identifier) {
self.id = id
}
static func __testOnly_fakeID(_ id: Int) -> EventLoopID {
return EventLoopID(.__testOnly_fakeID(id))
}
}
extension EventLoop {
var id: EventLoopID { EventLoopID(self) }
}