Skip to content

Commit c75349f

Browse files
authored
PostgresClient implements ServiceLifecycle's Service (vapor#457)
1 parent 17b23b1 commit c75349f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

Diff for: Package.swift

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ let package = Package(
2222
.package(url: "https://github.com/apple/swift-crypto.git", "2.0.0" ..< "4.0.0"),
2323
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.4.1"),
2424
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
25+
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.4.1"),
2526
],
2627
targets: [
2728
.target(
@@ -39,6 +40,7 @@ let package = Package(
3940
.product(name: "NIOTLS", package: "swift-nio"),
4041
.product(name: "NIOSSL", package: "swift-nio-ssl"),
4142
.product(name: "NIOFoundationCompat", package: "swift-nio"),
43+
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
4244
]
4345
),
4446
.target(

Diff for: Sources/PostgresNIO/Pool/PostgresClient.swift

+15-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import NIOCore
22
import NIOSSL
33
import Atomics
44
import Logging
5+
import ServiceLifecycle
56
import _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

Comments
 (0)