|
12 | 12 | //
|
13 | 13 | //===----------------------------------------------------------------------===//
|
14 | 14 |
|
| 15 | +import NIO |
| 16 | + |
15 | 17 | enum HTTPConnectionPool {
|
16 |
| - struct Connection { |
| 18 | + struct Connection: Hashable { |
17 | 19 | 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))) |
18 | 122 | }
|
| 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) } |
19 | 135 | }
|
0 commit comments