Skip to content

Commit 8fbf8ff

Browse files
authored
Add PooledConnection protocol (vapor#417)
1 parent c6c28a6 commit 8fbf8ff

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/// A connection that can be pooled in a ``ConnectionPool``
2+
public protocol PooledConnection: AnyObject, Sendable {
3+
/// The connections identifier type.
4+
associatedtype ID: Hashable & Sendable
5+
6+
/// The connections identifier. The identifier is passed to
7+
/// the connection factory method and must stay attached to
8+
/// the connection at all times. It must not change during
9+
/// the connections lifetime.
10+
var id: ID { get }
11+
12+
/// A method to register closures that are invoked when the
13+
/// connection is closed. If the connection closed unexpectedly
14+
/// the closure shall be called with the underlying error.
15+
/// In most NIO clients this can be easily implemented by
16+
/// attaching to the `channel.closeFuture`:
17+
/// ```
18+
/// func onClose(
19+
/// _ closure: @escaping @Sendable ((any Error)?) -> ()
20+
/// ) {
21+
/// channel.closeFuture.whenComplete { _ in
22+
/// closure(previousError)
23+
/// }
24+
/// }
25+
/// ```
26+
func onClose(_ closure: @escaping @Sendable ((any Error)?) -> ())
27+
28+
/// Close the running connection. Once the close has completed
29+
/// closures that were registered in `onClose` must be
30+
/// invoked.
31+
func close()
32+
}
33+
34+
/// A connection id generator. Its returned connection IDs will
35+
/// be used when creating new ``PooledConnection``s
36+
public protocol ConnectionIDGeneratorProtocol: Sendable {
37+
/// The connections identifier type.
38+
associatedtype ID: Hashable & Sendable
39+
40+
/// The next connection ID that shall be used.
41+
func next() -> ID
42+
}
43+
44+
/// A keep alive behavior for connections maintained by the pool
45+
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
46+
public protocol ConnectionKeepAliveBehavior: Sendable {
47+
/// the connection type
48+
associatedtype Connection: PooledConnection
49+
50+
/// The time after which a keep-alive shall
51+
/// be triggered.
52+
/// If nil is returned, keep-alive is deactivated
53+
var keepAliveFrequency: Duration? { get }
54+
55+
/// This method is invoked when the keep-alive shall be
56+
/// run.
57+
func runKeepAlive(for connection: Connection) async throws
58+
}

0 commit comments

Comments
 (0)