forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoolStateMachine.swift
93 lines (78 loc) · 2.65 KB
/
PoolStateMachine.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
#if canImport(Darwin)
import Darwin
#else
import Glibc
#endif
@usableFromInline
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
struct PoolConfiguration {
/// The minimum number of connections to preserve in the pool.
///
/// If the pool is mostly idle and the remote servers closes idle connections,
/// the `ConnectionPool` will initiate new outbound connections proactively
/// to avoid the number of available connections dropping below this number.
@usableFromInline
var minimumConnectionCount: Int = 0
/// The maximum number of connections to for this pool, to be preserved.
@usableFromInline
var maximumConnectionSoftLimit: Int = 10
@usableFromInline
var maximumConnectionHardLimit: Int = 10
@usableFromInline
var keepAliveDuration: Duration?
@usableFromInline
var idleTimeoutDuration: Duration = .seconds(30)
}
@usableFromInline
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
struct PoolStateMachine<
Connection: PooledConnection,
ConnectionIDGenerator: ConnectionIDGeneratorProtocol,
ConnectionID: Hashable & Sendable,
Request: ConnectionRequestProtocol,
RequestID,
TimerCancellationToken
> where Connection.ID == ConnectionID, ConnectionIDGenerator.ID == ConnectionID, RequestID == Request.ID {
@usableFromInline
struct ConnectionRequest: Equatable {
@usableFromInline var connectionID: ConnectionID
@inlinable
init(connectionID: ConnectionID) {
self.connectionID = connectionID
}
}
@usableFromInline
enum ConnectionAction {
@usableFromInline
struct Shutdown {
@usableFromInline
var connections: [Connection]
@usableFromInline
var timersToCancel: [TimerCancellationToken]
@inlinable
init() {
self.connections = []
self.timersToCancel = []
}
}
case scheduleTimers(Max2Sequence<Timer>)
case makeConnection(ConnectionRequest, TimerCancellationToken?)
case runKeepAlive(Connection, TimerCancellationToken?)
case cancelTimers(Max2Sequence<TimerCancellationToken>)
case closeConnection(Connection)
case shutdown(Shutdown)
case none
}
@usableFromInline
struct Timer: Hashable, Sendable {
@usableFromInline
var underlying: ConnectionTimer
@usableFromInline
var duration: Duration
@inlinable
init(_ connectionTimer: ConnectionTimer, duration: Duration) {
self.underlying = connectionTimer
self.duration = duration
}
}
}