@@ -2,6 +2,7 @@ import NIOCore
22import NIOSSL
33import Atomics
44import Logging
5+ import ServiceLifecycle
56import _ConnectionPoolModule
67
78/// A Postgres client that is backed by an underlying connection pool. Use ``Configuration`` to change the client's
@@ -17,23 +18,22 @@ import _ConnectionPoolModule
1718/// client.run() // !important
1819/// }
1920///
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
3025/// }
26+ /// } catch {
27+ /// // handle errors
3128/// }
29+ ///
30+ /// // shutdown the client
31+ /// taskGroup.cancelAll()
3232/// }
3333/// ```
3434@available ( macOS 13 . 0 , iOS 16 . 0 , tvOS 16 . 0 , watchOS 9 . 0 , * )
3535@_spi ( ConnectionPool)
36- public final class PostgresClient : Sendable {
36+ public final class PostgresClient : Sendable , ServiceLifecycle . Service {
3737 public struct Configuration : Sendable {
3838 public struct TLS : Sendable {
3939 enum Base {
@@ -391,7 +391,10 @@ public final class PostgresClient: Sendable {
391391 public func run( ) async {
392392 let atomicOp = self . runningAtomic. compareExchange ( expected: false , desired: true , ordering: . relaxed)
393393 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+ }
395398 }
396399
397400 // MARK: - Private Methods -
0 commit comments