@@ -2,6 +2,7 @@ import NIOCore
2
2
import NIOSSL
3
3
import Atomics
4
4
import Logging
5
+ import ServiceLifecycle
5
6
import _ConnectionPoolModule
6
7
7
8
/// A Postgres client that is backed by an underlying connection pool. Use ``Configuration`` to change the client's
@@ -17,23 +18,22 @@ import _ConnectionPoolModule
17
18
/// client.run() // !important
18
19
/// }
19
20
///
20
- /// taskGroup.addTask {
21
- /// client.withConnection { connection in
22
- /// do {
23
- /// let rows = try await connection.query("SELECT userID, name, age FROM users;")
24
- /// for try await (userID, name, age) in rows.decode((UUID, String, Int).self) {
25
- /// // do something with the values
26
- /// }
27
- /// } catch {
28
- /// // handle errors
29
- /// }
21
+ /// do {
22
+ /// let rows = try await connection.query("SELECT userID, name, age FROM users;")
23
+ /// for try await (userID, name, age) in rows.decode((UUID, String, Int).self) {
24
+ /// // do something with the values
30
25
/// }
26
+ /// } catch {
27
+ /// // handle errors
31
28
/// }
29
+ ///
30
+ /// // shutdown the client
31
+ /// taskGroup.cancelAll()
32
32
/// }
33
33
/// ```
34
34
@available ( macOS 13 . 0 , iOS 16 . 0 , tvOS 16 . 0 , watchOS 9 . 0 , * )
35
35
@_spi ( ConnectionPool)
36
- public final class PostgresClient : Sendable {
36
+ public final class PostgresClient : Sendable , ServiceLifecycle . Service {
37
37
public struct Configuration : Sendable {
38
38
public struct TLS : Sendable {
39
39
enum Base {
@@ -391,7 +391,10 @@ public final class PostgresClient: Sendable {
391
391
public func run( ) async {
392
392
let atomicOp = self . runningAtomic. compareExchange ( expected: false , desired: true , ordering: . relaxed)
393
393
precondition ( !atomicOp. original, " PostgresClient.run() should just be called once! " )
394
- await self . pool. run ( )
394
+
395
+ await cancelOnGracefulShutdown {
396
+ await self . pool. run ( )
397
+ }
395
398
}
396
399
397
400
// MARK: - Private Methods -
0 commit comments