-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathEventLoopGroupConnectionPool.swift
More file actions
431 lines (402 loc) · 17 KB
/
EventLoopGroupConnectionPool.swift
File metadata and controls
431 lines (402 loc) · 17 KB
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
#if canImport(Dispatch)
import Dispatch
#endif
import NIOConcurrencyHelpers
import NIOCore
import struct Logging.Logger
/// Holds a collection of connection pools for each `EventLoop` on an `EventLoopGroup`.
///
/// Connection pools are used to offset the overhead of creating new connections. Newly
/// opened connections are returned back to the pool and can be re-used until they
/// close.
///
/// New connections are created as needed until the maximum configured connection limit
/// is reached. After the maximum is reached, no new connections will be created unless
/// existing connections are closed.
///
/// ```swift
/// let pool = EventLoopGroupConnectionPool(...)
/// pool.withConnection { conn in
/// // use conn
/// }
/// ```
public final class EventLoopGroupConnectionPool<Source> where Source: ConnectionPoolSource {
/// Creates new connections when needed. See ``ConnectionPoolSource``.
public let source: Source
/// Limits the maximum number of connections that can be open at a given time
/// for a single connection pool.
public let maxConnectionsPerEventLoop: Int
/// Event loop source when not specified.
public let eventLoopGroup: any EventLoopGroup
// MARK: Private
/// For lifecycle logs.
private let logger: Logger
/// Synchronize access.
private let lock: NIOLock
/// If `true`, this connection pool has been closed.
private var didShutdown: Bool
/// Actual connection pool storage.
private let storage: [ObjectIdentifier: EventLoopConnectionPool<Source>]
/// Creates a new ``EventLoopGroupConnectionPool``.
///
/// ```swift
/// let pool = EventLoopGroupConnectionPool(...)
/// pool.withConnection(...) { conn in
/// // use conn
/// }
/// ```
///
/// - Parameters:
/// - source: Creates new connections when needed.
/// - maxConnectionsPerEventLoop: Limits the number of connections that can be open per event loop.
/// Defaults to 1.
/// - requestTimeout: Timeout for requesting a new connection. Defaults to 10 seconds.
/// - logger: For lifecycle logs.
/// - eventLoopGroup: Event loop group.
public convenience init(
source: Source,
maxConnectionsPerEventLoop: Int = 1,
requestTimeout: TimeAmount = .seconds(10),
logger: Logger = .init(label: "codes.vapor.pool"),
on eventLoopGroup: any EventLoopGroup
) {
self.init(
source: source,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
requestTimeout: requestTimeout,
pruneInterval: nil,
logger: logger,
on: eventLoopGroup
)
}
/// Creates a new ``EventLoopGroupConnectionPool``.
///
/// ```swift
/// let pool = EventLoopGroupConnectionPool(...)
/// pool.withConnection(...) { conn in
/// // use conn
/// }
/// ```
///
/// - Parameters:
/// - source: Creates new connections when needed.
/// - maxConnectionsPerEventLoop: Limits the number of connections that can be open per event loop.
/// Defaults to 1.
/// - requestTimeout: Timeout for requesting a new connection. Defaults to 10 seconds.
/// - pruneInterval: How often to check for and prune idle database connections. If `nil` (the default),
/// no pruning is performed.
/// - maxIdleTimeBeforePruning: How long a connection may remain idle before being pruned, if pruning is enabled.
/// Defaults to 2 minutes. Ignored if `pruneInterval` is `nil`.
/// - logger: For lifecycle logs.
/// - eventLoopGroup: Event loop group.
public init(
source: Source,
maxConnectionsPerEventLoop: Int = 1,
requestTimeout: TimeAmount = .seconds(10),
pruneInterval: TimeAmount?,
maxIdleTimeBeforePruning: TimeAmount = .seconds(120),
logger: Logger = .init(label: "codes.vapor.pool"),
on eventLoopGroup: any EventLoopGroup
) {
self.source = source
self.maxConnectionsPerEventLoop = maxConnectionsPerEventLoop
self.logger = logger
self.lock = .init()
self.eventLoopGroup = eventLoopGroup
self.didShutdown = false
self.storage = .init(uniqueKeysWithValues: eventLoopGroup.makeIterator().map { (.init($0), .init(
source: source,
maxConnections: maxConnectionsPerEventLoop,
requestTimeout: requestTimeout,
pruneInterval: pruneInterval,
maxIdleTimeBeforePruning: maxIdleTimeBeforePruning,
logger: logger,
on: $0
)) })
}
/// Fetches a pooled connection for the lifetime of the closure.
///
/// The connection is provided to the supplied callback and will be automatically released when the
/// future returned by the callback is completed.
///
/// ```swift
/// pool.withConnection(...) { conn in
/// // use the connection
/// }
/// ```
///
/// See ``EventLoopGroupConnectionPool/requestConnection(logger:on:)`` to request a pooled connection without
/// using a callback.
///
/// - Parameters:
/// - logger: For trace and debug logs.
/// - eventLoop: Preferred event loop for the new connection.
/// - closure: Callback that accepts the pooled connection.
/// - Returns: A future containing the result of the closure.
public func withConnection<Result>(
logger: Logger? = nil,
on eventLoop: (any EventLoop)? = nil,
_ closure: @escaping (Source.Connection) -> EventLoopFuture<Result>
) -> EventLoopFuture<Result> {
guard !self.lock.withLock({ self.didShutdown }) else {
return (eventLoop ?? self.eventLoopGroup).future(error: ConnectionPoolError.shutdown)
}
return self.pool(for: eventLoop ?? self.eventLoopGroup.any())
.withConnection(logger: logger ?? self.logger, closure)
}
/// Requests a pooled connection.
///
/// The connection returned by this method should be released when you are finished using it.
///
/// ```swift
/// let conn = try pool.requestConnection(...).wait()
/// defer { pool.releaseConnection(conn) }
/// // use the connection
/// ```
///
/// See ``EventLoopGroupConnectionPool/withConnection(logger:on:_:)`` for a callback-based method that automatically
/// releases the connection.
///
/// - Parameters:
/// - logger: For trace and debug logs.
/// - eventLoop: Preferred event loop for the new connection.
/// - Returns: A future containing the requested connection.
public func requestConnection(
logger: Logger? = nil,
on eventLoop: (any EventLoop)? = nil
) -> EventLoopFuture<Source.Connection> {
guard !self.lock.withLock({ self.didShutdown }) else {
return (eventLoop ?? self.eventLoopGroup).future(error: ConnectionPoolError.shutdown)
}
return self.pool(for: eventLoop ?? self.eventLoopGroup.any())
.requestConnection(logger: logger ?? self.logger)
}
/// Releases a connection back to the pool. Use with ``EventLoopGroupConnectionPool/requestConnection(logger:on:)``.
///
/// ```swift
/// let conn = try pool.requestConnection(...).wait()
/// defer { pool.releaseConnection(conn) }
/// // use the connection
/// ```
///
/// - Parameters:
/// - connection: Connection to release back to the pool.
/// - logger: For trace and debug logs.
public func releaseConnection(
_ connection: Source.Connection,
logger: Logger? = nil
) {
self.pool(for: connection.eventLoop)
.releaseConnection(connection, logger: logger ?? self.logger)
}
/// Returns the ``EventLoopConnectionPool`` for a specific event loop.
public func pool(for eventLoop: any EventLoop) -> EventLoopConnectionPool<Source> {
self.storage[.init(eventLoop)]!
}
/// Closes the connection pool.
///
/// All available connections will be closed immediately.
/// Any connections currently in use will be closed when they are returned to the pool.
///
/// Once closed, the connection pool cannot be used to create new connections.
///
/// Connection pools must be closed before they deinitialize.
///
/// > Warning: This method is soft-deprecated. Use ``EventLoopGroupConnectionPool/syncShutdownGracefully()`` or
/// > ``EventLoopGroupConnectionPool/shutdownGracefully(_:)`` instead.
@available(*, noasync, message: "This calls wait() and should not be used in an async context", renamed: "shutdownAsync()")
public func shutdown() {
// synchronize access to closing
guard self.lock.withLock({
// check to make sure we aren't double closing
guard !self.didShutdown else {
return false
}
self.didShutdown = true
self.logger.debug("Connection pool shutting down, closing each event loop's storage")
return true
}) else {
return
}
// shutdown all pools
for pool in self.storage.values {
do {
try pool.close().wait()
} catch {
self.logger.error("Failed shutting down event loop pool: \(error)")
}
}
}
/// Closes the connection pool.
///
/// All available connections will be closed immediately. Any connections still in use will be
/// closed as soon as they are returned to the pool. Once closed, the pool can not be used to
/// create new connections.
///
/// Connection pools must be closed before they deinitialize.
///
/// This method shuts down asynchronously, waiting for all connection closures to complete before
/// returning.
///
/// > Warning: The pool is always fully shut down once this method returns, even if an error is
/// > thrown. All errors are purely advisory.
public func shutdownAsync() async throws {
// synchronize access to closing
guard self.lock.withLock({
// check to make sure we aren't double closing
guard !self.didShutdown else {
return false
}
self.didShutdown = true
self.logger.debug("Connection pool shutting down, closing each event loop's storage")
return true
}) else {
self.logger.debug("Cannot shutdown the connection pool more than once")
throw ConnectionPoolError.shutdown
}
// shutdown all pools
for pool in self.storage.values {
do {
try await pool.close().get()
} catch {
self.logger.error("Failed shutting down event loop pool: \(error)")
}
}
}
#if canImport(Dispatch)
/// Closes the connection pool.
///
/// All available connections will be closed immediately. Any connections still in use will be
/// closed as soon as they are returned to the pool. Once closed, the pool can not be used to
/// create new connections.
///
/// Connection pools must be closed before they deinitialize.
///
/// This method shuts down synchronously, waiting for all connection closures to complete before
/// returning.
///
/// > Warning: The pool is always fully shut down once this method returns, even if an error is
/// > thrown. All errors are purely advisory.
@available(*, noasync, message: "This calls wait() and should not be used in an async context", renamed: "shutdownAsync()")
public func syncShutdownGracefully() throws {
var possibleError: (any Error)? = nil
let waiter = DispatchSemaphore(value: 0)
let errorLock = NIOLock()
self.shutdownGracefully {
if let error = $0 {
errorLock.withLock { possibleError = error }
}
waiter.signal()
}
waiter.wait()
try errorLock.withLock {
if let error = possibleError {
throw error
}
}
}
#endif // canImport(Dispatch)
/// Closes the connection pool.
///
/// All available connections will be closed immediately. Any connections still in use will be
/// closed as soon as they are returned to the pool. Once closed, the pool can not be used to
/// create new connections.
///
/// Connection pools must be closed before they deinitialize.
///
/// This method shuts the pool down asynchronously. It may be invoked on any event loop. The
/// provided callback will be notified when shutdown is complete. It is invalid to allow a pool
/// to deinitialize before it has fully shut down.
///
/// This method promises explicitly as API contract not to invoke the callback before returning
/// to its caller. It further promises the callback will not be invoked on any event loop
/// belonging to the pool.
///
/// > Warning: Any invocation of the callback represents a signal that the pool has fully shut
/// > down. This is true even if the error parameter is non-`nil`; errors are purely advisory.
public func shutdownGracefully(_ callback: @escaping ((any Error)?) -> Void) {
// Protect access to shared state.
guard self.lock.withLock({
// Do not initiate shutdown multiple times.
guard !self.didShutdown else {
#if canImport(Dispatch)
DispatchQueue.global().async {
self.logger.warning("Connection pool can not be shut down more than once.")
callback(ConnectionPoolError.shutdown)
}
#else
Task {
self.logger.warning("Connection pool can not be shut down more than once.")
callback(ConnectionPoolError.shutdown)
}
#endif
return false
}
// Set the flag as soon as we know a shutdown is needed.
self.didShutdown = true
self.logger.trace("Connection group pool shutdown start - telling the loop pools what's up.")
// Don't need to hold the lock anymore; the shutdown can proceed without blocking anything else, though
// it's also true there's nothing else to block that we care about after shutdown begin.
return true
}) else { return }
// Tell each pool to shut down and take note of any errors if they show up. Use the dispatch
// queue to manage synchronization to avoid being trapped on any of our own event loops. When
// all pools are closed, invoke the callback and provide it the first encountered error, if
// any. By design, this loosely matches the general flow used by `MultiThreadedEventLoopGroup`'s
// `shutdownGracefully(queue:_:)` implementation.
#if canImport(Dispatch)
let shutdownQueue = DispatchQueue(label: "codes.vapor.async-kit.poolShutdownGracefullyQueue")
let shutdownGroup = DispatchGroup()
var outcome: Result<Void, any Error> = .success(())
for pool in self.storage.values {
shutdownGroup.enter()
pool.close().whenComplete { result in
shutdownQueue.async {
outcome = outcome.flatMap { result }
shutdownGroup.leave()
}
}
}
shutdownGroup.notify(queue: shutdownQueue) {
switch outcome {
case .success:
self.logger.debug("Connection group pool finished shutdown.")
callback(nil)
case .failure(let error):
self.logger.error("Connection group pool got shutdown error (and then shut down anyway): \(error)")
callback(error)
}
}
#else
Task {
await withTaskGroup(of: Result<Void, any Error>.self) { group in
for pool in self.storage.values {
group.addTask {
let singleResult: Result<Void, any Error> = await withCheckedContinuation { continuation in
pool.close().whenComplete { result in
continuation.resume(returning: result)
}
}
return singleResult
}
}
var outcome: Result<Void, any Error> = .success(())
for await singleResult in group {
outcome = outcome.flatMap { singleResult }
}
switch outcome {
case .success:
self.logger.debug("Connection group pool finished shutdown.")
callback(nil)
case .failure(let error):
self.logger.error("Connection group pool got shutdown error (and then shut down anyway): \(error)")
callback(error)
}
}
}
#endif
}
deinit {
assert(self.lock.withLock { self.didShutdown }, "ConnectionPool.shutdown() was not called before deinit.")
}
}