forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoolStateMachine+ConnectionGroup.swift
640 lines (529 loc) · 24.9 KB
/
PoolStateMachine+ConnectionGroup.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import Atomics
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
extension PoolStateMachine {
@usableFromInline
struct LeaseResult {
@usableFromInline
var connection: Connection
@usableFromInline
var timersToCancel: Max2Sequence<TimerCancellationToken>
@usableFromInline
var wasIdle: Bool
@usableFromInline
var use: ConnectionGroup.ConnectionUse
@inlinable
init(
connection: Connection,
timersToCancel: Max2Sequence<TimerCancellationToken>,
wasIdle: Bool,
use: ConnectionGroup.ConnectionUse
) {
self.connection = connection
self.timersToCancel = timersToCancel
self.wasIdle = wasIdle
self.use = use
}
}
@usableFromInline
struct ConnectionGroup: Sendable {
@usableFromInline
struct Stats: Hashable, Sendable {
@usableFromInline var connecting: UInt16 = 0
@usableFromInline var backingOff: UInt16 = 0
@usableFromInline var idle: UInt16 = 0
@usableFromInline var leased: UInt16 = 0
@usableFromInline var runningKeepAlive: UInt16 = 0
@usableFromInline var closing: UInt16 = 0
@usableFromInline var availableStreams: UInt16 = 0
@usableFromInline var leasedStreams: UInt16 = 0
@usableFromInline var soonAvailable: UInt16 {
self.connecting + self.backingOff + self.runningKeepAlive
}
@usableFromInline var active: UInt16 {
self.idle + self.leased + self.connecting + self.backingOff
}
}
/// The minimum number of connections
@usableFromInline
let minimumConcurrentConnections: Int
/// The maximum number of preserved connections
@usableFromInline
let maximumConcurrentConnectionSoftLimit: Int
/// The absolute maximum number of connections
@usableFromInline
let maximumConcurrentConnectionHardLimit: Int
@usableFromInline
let keepAlive: Bool
@usableFromInline
let keepAliveReducesAvailableStreams: Bool
/// A connectionID generator.
@usableFromInline
let generator: ConnectionIDGenerator
/// The connections states
@usableFromInline
private(set) var connections: [ConnectionState]
@usableFromInline
private(set) var stats = Stats()
@inlinable
init(
generator: ConnectionIDGenerator,
minimumConcurrentConnections: Int,
maximumConcurrentConnectionSoftLimit: Int,
maximumConcurrentConnectionHardLimit: Int,
keepAlive: Bool,
keepAliveReducesAvailableStreams: Bool
) {
self.generator = generator
self.connections = []
self.minimumConcurrentConnections = minimumConcurrentConnections
self.maximumConcurrentConnectionSoftLimit = maximumConcurrentConnectionSoftLimit
self.maximumConcurrentConnectionHardLimit = maximumConcurrentConnectionHardLimit
self.keepAlive = keepAlive
self.keepAliveReducesAvailableStreams = keepAliveReducesAvailableStreams
}
var isEmpty: Bool {
self.connections.isEmpty
}
@usableFromInline
var canGrow: Bool {
self.stats.active < self.maximumConcurrentConnectionHardLimit
}
@usableFromInline
var soonAvailableConnections: UInt16 {
self.stats.soonAvailable
}
// MARK: - Mutations -
/// A connection's use. Is it persisted or an overflow connection?
@usableFromInline
enum ConnectionUse: Equatable {
case persisted
case demand
case overflow
}
/// Information around an idle connection.
@usableFromInline
struct AvailableConnectionContext {
/// The connection's use. Either general purpose or for requests with `EventLoop`
/// requirements.
@usableFromInline
var use: ConnectionUse
@usableFromInline
var info: ConnectionAvailableInfo
}
/// Information around the failed/closed connection.
@usableFromInline
struct FailedConnectionContext {
/// Connections that are currently starting
@usableFromInline
var connectionsStarting: Int
@inlinable
init(connectionsStarting: Int) {
self.connectionsStarting = connectionsStarting
}
}
mutating func refillConnections() -> [ConnectionRequest] {
let existingConnections = self.stats.active
let missingConnection = self.minimumConcurrentConnections - Int(existingConnections)
guard missingConnection > 0 else {
return []
}
var requests = [ConnectionRequest]()
requests.reserveCapacity(missingConnection)
for _ in 0..<missingConnection {
requests.append(self.createNewConnection())
}
return requests
}
// MARK: Connection creation
@inlinable
mutating func createNewDemandConnectionIfPossible() -> ConnectionRequest? {
precondition(self.minimumConcurrentConnections <= self.stats.active)
guard self.maximumConcurrentConnectionSoftLimit > self.stats.active else {
return nil
}
return self.createNewConnection()
}
@inlinable
mutating func createNewOverflowConnectionIfPossible() -> ConnectionRequest? {
precondition(self.maximumConcurrentConnectionSoftLimit <= self.stats.active)
guard self.maximumConcurrentConnectionHardLimit > self.stats.active else {
return nil
}
return self.createNewConnection()
}
@inlinable
/*private*/ mutating func createNewConnection() -> ConnectionRequest {
precondition(self.canGrow)
self.stats.connecting += 1
let connectionID = self.generator.next()
let connection = ConnectionState(id: connectionID)
self.connections.append(connection)
return ConnectionRequest(connectionID: connectionID)
}
/// A new ``Connection`` was established.
///
/// This will put the connection into the idle state.
///
/// - Parameter connection: The new established connection.
/// - Returns: An index and an IdleConnectionContext to determine the next action for the now idle connection.
/// Call ``parkConnection(at:)``, ``leaseConnection(at:)`` or ``closeConnection(at:)``
/// with the supplied index after this.
@inlinable
mutating func newConnectionEstablished(_ connection: Connection, maxStreams: UInt16) -> (Int, AvailableConnectionContext) {
guard let index = self.connections.firstIndex(where: { $0.id == connection.id }) else {
preconditionFailure("There is a new connection that we didn't request!")
}
self.stats.connecting -= 1
self.stats.idle += 1
self.stats.availableStreams += maxStreams
let connectionInfo = self.connections[index].connected(connection, maxStreams: maxStreams)
// TODO: If this is an overflow connection, but we are currently also creating a
// persisted connection, we might want to swap those.
let context = self.makeAvailableConnectionContextForConnection(at: index, info: connectionInfo)
return (index, context)
}
@inlinable
mutating func backoffNextConnectionAttempt(_ connectionID: Connection.ID) -> ConnectionTimer {
guard let index = self.connections.firstIndex(where: { $0.id == connectionID }) else {
preconditionFailure("We tried to create a new connection that we know nothing about?")
}
self.stats.connecting -= 1
self.stats.backingOff += 1
return self.connections[index].failedToConnect()
}
@usableFromInline
enum BackoffDoneAction {
case createConnection(ConnectionRequest, TimerCancellationToken?)
case cancelTimers(Max2Sequence<TimerCancellationToken>)
}
@inlinable
mutating func backoffDone(_ connectionID: Connection.ID, retry: Bool) -> BackoffDoneAction {
guard let index = self.connections.firstIndex(where: { $0.id == connectionID }) else {
preconditionFailure("We tried to create a new connection that we know nothing about?")
}
self.stats.backingOff -= 1
if retry || self.stats.active < self.minimumConcurrentConnections {
self.stats.connecting += 1
let backoffTimerCancellation = self.connections[index].retryConnect()
return .createConnection(.init(connectionID: connectionID), backoffTimerCancellation)
}
let backoffTimerCancellation = self.connections[index].destroyBackingOffConnection()
var timerCancellations = Max2Sequence(backoffTimerCancellation)
if let timerCancellationToken = self.swapForDeletion(index: index) {
timerCancellations.append(timerCancellationToken)
}
return .cancelTimers(timerCancellations)
}
@inlinable
mutating func timerScheduled(
_ timer: ConnectionTimer,
cancelContinuation: TimerCancellationToken
) -> TimerCancellationToken? {
guard let index = self.connections.firstIndex(where: { $0.id == timer.connectionID }) else {
return cancelContinuation
}
return self.connections[index].timerScheduled(timer, cancelContinuation: cancelContinuation)
}
// MARK: Leasing and releasing
/// Lease a connection, if an idle connection is available.
///
/// - Returns: A connection to execute a request on.
@inlinable
mutating func leaseConnection() -> LeaseResult? {
if self.stats.availableStreams == 0 {
return nil
}
guard let index = self.findAvailableConnection() else {
preconditionFailure("Stats and actual count are of.")
}
return self.leaseConnection(at: index, streams: 1)
}
@usableFromInline
enum LeasedConnectionOrStartingCount {
case leasedConnection(LeaseResult)
case startingCount(UInt16)
}
@inlinable
mutating func leaseConnectionOrSoonAvailableConnectionCount() -> LeasedConnectionOrStartingCount {
if let result = self.leaseConnection() {
return .leasedConnection(result)
}
return .startingCount(self.stats.soonAvailable)
}
@inlinable
mutating func leaseConnection(at index: Int, streams: UInt16) -> LeaseResult {
let leaseResult = self.connections[index].lease(streams: streams)
let use = self.getConnectionUse(index: index)
if leaseResult.wasIdle {
self.stats.idle -= 1
self.stats.leased += 1
}
self.stats.leasedStreams += streams
self.stats.availableStreams -= streams
return LeaseResult(
connection: leaseResult.connection,
timersToCancel: leaseResult.timersToCancel,
wasIdle: leaseResult.wasIdle,
use: use
)
}
@inlinable
mutating func parkConnection(at index: Int) -> Max2Sequence<ConnectionTimer> {
let scheduleIdleTimeoutTimer: Bool
switch index {
case 0..<self.minimumConcurrentConnections:
// if a connection is a minimum connection, it doesn't need to create an idle
// timeout timer
scheduleIdleTimeoutTimer = false
case self.minimumConcurrentConnections..<self.maximumConcurrentConnectionSoftLimit:
// if a connection is a demand connection, we want a timeout timer
scheduleIdleTimeoutTimer = true
case self.maximumConcurrentConnectionSoftLimit..<self.maximumConcurrentConnectionHardLimit:
preconditionFailure("Overflow connections should never be parked.")
default:
preconditionFailure("A connection index must not be equal or larger `self.maximumConcurrentConnectionHardLimit`")
}
return self.connections[index].parkConnection(
scheduleKeepAliveTimer: self.keepAlive,
scheduleIdleTimeoutTimer: scheduleIdleTimeoutTimer
)
}
/// A connection was released.
///
/// This will put the position into the idle state.
///
/// - Parameter connectionID: The released connection's id.
/// - Returns: An index and an IdleConnectionContext to determine the next action for the now idle connection.
/// Call ``leaseConnection(at:)`` or ``closeConnection(at:)`` with the supplied index after
/// this. If you want to park the connection no further call is required.
@inlinable
mutating func releaseConnection(_ connectionID: Connection.ID, streams: UInt16) -> (Int, AvailableConnectionContext) {
guard let index = self.connections.firstIndex(where: { $0.id == connectionID }) else {
preconditionFailure("A connection that we don't know was released? Something is very wrong...")
}
let connectionInfo = self.connections[index].release(streams: streams)
self.stats.availableStreams += streams
self.stats.leasedStreams -= streams
switch connectionInfo {
case .idle:
self.stats.idle += 1
self.stats.leased -= 1
case .leased:
break
}
let context = self.makeAvailableConnectionContextForConnection(at: index, info: connectionInfo)
return (index, context)
}
@inlinable
mutating func keepAliveIfIdle(_ connectionID: Connection.ID) -> KeepAliveAction? {
guard let index = self.connections.firstIndex(where: { $0.id == connectionID }) else {
// because of a race this connection (connection close runs against trigger of ping pong)
// was already removed from the state machine.
return nil
}
guard let action = self.connections[index].runKeepAliveIfIdle(reducesAvailableStreams: self.keepAliveReducesAvailableStreams) else {
return nil
}
self.stats.runningKeepAlive += 1
if self.keepAliveReducesAvailableStreams {
self.stats.availableStreams -= 1
}
return action
}
@inlinable
mutating func keepAliveSucceeded(_ connectionID: Connection.ID) -> (Int, AvailableConnectionContext)? {
guard let index = self.connections.firstIndex(where: { $0.id == connectionID }) else {
preconditionFailure("A connection that we don't know was released? Something is very wrong...")
}
guard let connectionInfo = self.connections[index].keepAliveSucceeded() else {
// if we don't get connection info here this means, that the connection already was
// transitioned to closing. when we did this we already decremented the
// runningKeepAlive timer.
return nil
}
self.stats.runningKeepAlive -= 1
if self.keepAliveReducesAvailableStreams {
self.stats.availableStreams += 1
}
let context = self.makeAvailableConnectionContextForConnection(at: index, info: connectionInfo)
return (index, context)
}
// MARK: Connection close/removal
@usableFromInline
struct CloseAction {
@usableFromInline
private(set) var connection: Connection
@usableFromInline
private(set) var timersToCancel: Max2Sequence<TimerCancellationToken>
@inlinable
init(connection: Connection, timersToCancel: Max2Sequence<TimerCancellationToken>) {
self.connection = connection
self.timersToCancel = timersToCancel
}
}
/// Closes the connection at the given index.
@inlinable
mutating func closeConnectionIfIdle(at index: Int) -> CloseAction {
guard let closeAction = self.connections[index].closeIfIdle() else {
preconditionFailure("Invalid state: \(self)")
}
self.stats.idle -= 1
self.stats.closing += 1
// if idleState.runningKeepAlive {
// self.stats.runningKeepAlive -= 1
// if self.keepAliveReducesAvailableStreams {
// self.stats.availableStreams += 1
// }
// }
self.stats.availableStreams -= closeAction.maxStreams
return CloseAction(
connection: closeAction.connection!,
timersToCancel: closeAction.cancelTimers
)
}
@inlinable
mutating func closeConnectionIfIdle(_ connectionID: Connection.ID) -> CloseAction? {
guard let index = self.connections.firstIndex(where: { $0.id == connectionID }) else {
// because of a race this connection (connection close runs against trigger of timeout)
// was already removed from the state machine.
return nil
}
if index < self.minimumConcurrentConnections {
// because of a race a connection might receive a idle timeout after it was moved into
// the persisted connections. If a connection is now persisted, we now need to ignore
// the trigger
return nil
}
return self.closeConnectionIfIdle(at: index)
}
/// Connection closed. Call this method, if a connection is closed.
///
/// This will put the position into the closed state.
///
/// - Parameter connectionID: The failed connection's id.
/// - Returns: An optional index and an IdleConnectionContext to determine the next action for the closed connection.
/// You must call ``removeConnection(at:)`` or ``replaceConnection(at:)`` with the
/// supplied index after this. If nil is returned the connection was closed by the state machine and was
/// therefore already removed.
@inlinable
mutating func connectionClosed(_ connectionID: Connection.ID) -> FailedConnectionContext? {
guard let index = self.connections.firstIndex(where: { $0.id == connectionID }) else {
return nil
}
let closedAction = self.connections[index].closed()
if closedAction.wasRunningKeepAlive {
self.stats.runningKeepAlive -= 1
}
self.stats.leasedStreams -= closedAction.usedStreams
self.stats.availableStreams -= closedAction.maxStreams - closedAction.usedStreams
switch closedAction.previousConnectionState {
case .idle:
self.stats.idle -= 1
case .leased:
self.stats.leased -= 1
case .closing:
self.stats.closing -= 1
}
let lastIndex = self.connections.index(before: self.connections.endIndex)
if index == lastIndex {
self.connections.remove(at: index)
} else {
self.connections.swapAt(index, lastIndex)
self.connections.remove(at: lastIndex)
}
return FailedConnectionContext(connectionsStarting: 0)
}
// MARK: Shutdown
mutating func triggerForceShutdown(_ cleanup: inout ConnectionAction.Shutdown) {
for var connectionState in self.connections {
guard let closeAction = connectionState.close() else {
continue
}
if let connection = closeAction.connection {
cleanup.connections.append(connection)
}
cleanup.timersToCancel.append(contentsOf: closeAction.cancelTimers)
}
self.connections = []
}
// MARK: - Private functions -
@usableFromInline
/*private*/ func getConnectionUse(index: Int) -> ConnectionUse {
switch index {
case 0..<self.minimumConcurrentConnections:
return .persisted
case self.minimumConcurrentConnections..<self.maximumConcurrentConnectionSoftLimit:
return .demand
case self.maximumConcurrentConnectionSoftLimit...:
return .overflow
default:
preconditionFailure("We do not allow negative connection indexes but got: \(index)")
}
}
@usableFromInline
/*private*/ func makeAvailableConnectionContextForConnection(at index: Int, info: ConnectionAvailableInfo) -> AvailableConnectionContext {
precondition(self.connections[index].isAvailable)
let use = self.getConnectionUse(index: index)
return AvailableConnectionContext(use: use, info: info)
}
@inlinable
/*private*/ func findAvailableConnection() -> Int? {
return self.connections.firstIndex(where: { $0.isAvailable })
}
@inlinable
/*private*/ mutating func swapForDeletion(index indexToDelete: Int) -> TimerCancellationToken? {
let maybeLastConnectedIndex = self.connections.lastIndex(where: { $0.isConnected })
if maybeLastConnectedIndex == nil || maybeLastConnectedIndex! < indexToDelete {
self.removeO1(indexToDelete)
return nil
}
// if maybeLastConnectedIndex == nil, we return early in the above if case.
let lastConnectedIndex = maybeLastConnectedIndex!
switch indexToDelete {
case 0..<self.minimumConcurrentConnections:
// the connection to be removed is a persisted connection
self.connections.swapAt(indexToDelete, lastConnectedIndex)
self.removeO1(lastConnectedIndex)
switch lastConnectedIndex {
case 0..<self.minimumConcurrentConnections:
// a persisted connection was moved within the persisted connections. thats fine.
return nil
case self.minimumConcurrentConnections..<self.maximumConcurrentConnectionSoftLimit:
// a demand connection was moved to a persisted connection. If it currently idle
// or ping ponging, we must cancel its idle timeout timer
return self.connections[indexToDelete].cancelIdleTimer()
case self.maximumConcurrentConnectionSoftLimit..<self.maximumConcurrentConnectionHardLimit:
// an overflow connection was moved to a demand connection. It has to be currently leased
precondition(self.connections[indexToDelete].isLeased)
return nil
default:
preconditionFailure("A connection index must not be equal or larger `self.maximumConcurrentConnectionHardLimit`")
}
case self.minimumConcurrentConnections..<self.maximumConcurrentConnectionSoftLimit:
// the connection to be removed is a demand connection
switch lastConnectedIndex {
case self.minimumConcurrentConnections..<self.maximumConcurrentConnectionSoftLimit:
// an overflow connection was moved to a demand connection. It has to be currently leased
precondition(self.connections[indexToDelete].isLeased)
return nil
default:
return nil
}
default:
return nil
}
}
@inlinable
/*private*/ mutating func removeO1(_ indexToDelete: Int) {
let lastIndex = self.connections.index(before: self.connections.endIndex)
if indexToDelete == lastIndex {
self.connections.remove(at: indexToDelete)
} else {
self.connections.swapAt(indexToDelete, lastIndex)
self.connections.remove(at: lastIndex)
}
}
}
}
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
extension PoolStateMachine.ConnectionGroup.BackoffDoneAction: Equatable where TimerCancellationToken: Equatable {}