Skip to content

Commit 7632411

Browse files
authored
Make PostgresClient API (#460)
1 parent c75349f commit 7632411

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

Sources/PostgresNIO/Pool/PostgresClient.swift

+23-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import _ConnectionPoolModule
88
/// A Postgres client that is backed by an underlying connection pool. Use ``Configuration`` to change the client's
99
/// behavior.
1010
///
11-
/// > Important:
11+
/// > Warning:
1212
/// The client can only lease connections if the user is running the client's ``run()`` method in a long running task:
1313
///
1414
/// ```swift
15-
/// let client = PostgresClient(configuration: configuration, logger: logger)
15+
/// let client = PostgresClient(configuration: configuration)
1616
/// await withTaskGroup(of: Void.self) {
1717
/// taskGroup.addTask {
1818
/// client.run() // !important
@@ -32,7 +32,6 @@ import _ConnectionPoolModule
3232
/// }
3333
/// ```
3434
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
35-
@_spi(ConnectionPool)
3635
public final class PostgresClient: Sendable, ServiceLifecycle.Service {
3736
public struct Configuration: Sendable {
3837
public struct TLS: Sendable {
@@ -246,8 +245,22 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
246245
let factory: ConnectionFactory
247246
let runningAtomic = ManagedAtomic(false)
248247
let backgroundLogger: Logger
249-
248+
249+
/// Creates a new ``PostgresClient``, that does not log any background information.
250+
/// Don't forget to run ``run()`` the client in a long running task.
251+
///
252+
/// - Parameters:
253+
/// - configuration: The client's configuration. See ``Configuration`` for details.
254+
/// - eventLoopGroup: The underlying NIO `EventLoopGroup`. Defaults to ``defaultEventLoopGroup``.
255+
public convenience init(
256+
configuration: Configuration,
257+
eventLoopGroup: any EventLoopGroup = PostgresClient.defaultEventLoopGroup
258+
) {
259+
self.init(configuration: configuration, eventLoopGroup: eventLoopGroup, backgroundLogger: Self.loggingDisabled)
260+
}
261+
250262
/// Creates a new ``PostgresClient``. Don't forget to run ``run()`` the client in a long running task.
263+
///
251264
/// - Parameters:
252265
/// - configuration: The client's configuration. See ``Configuration`` for details.
253266
/// - eventLoopGroup: The underlying NIO `EventLoopGroup`. Defaults to ``defaultEventLoopGroup``.
@@ -302,10 +315,11 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
302315
@discardableResult
303316
public func query(
304317
_ query: PostgresQuery,
305-
logger: Logger,
318+
logger: Logger? = nil,
306319
file: String = #fileID,
307320
line: Int = #line
308321
) async throws -> PostgresRowSequence {
322+
let logger = logger ?? Self.loggingDisabled
309323
do {
310324
guard query.binds.count <= Int(UInt16.max) else {
311325
throw PSQLError(code: .tooManyParameters, query: query, file: file, line: line)
@@ -345,11 +359,12 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
345359
/// Execute a prepared statement, taking care of the preparation when necessary
346360
public func execute<Statement: PostgresPreparedStatement, Row>(
347361
_ preparedStatement: Statement,
348-
logger: Logger,
362+
logger: Logger? = nil,
349363
file: String = #fileID,
350364
line: Int = #line
351365
) async throws -> AsyncThrowingMapSequence<PostgresRowSequence, Row> where Row == Statement.Row {
352366
let bindings = try preparedStatement.makeBindings()
367+
let logger = logger ?? Self.loggingDisabled
353368

354369
do {
355370
let connection = try await self.leaseConnection()
@@ -412,6 +427,8 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
412427
public static var defaultEventLoopGroup: EventLoopGroup {
413428
PostgresConnection.defaultEventLoopGroup
414429
}
430+
431+
static let loggingDisabled = Logger(label: "Postgres-do-not-log", factory: { _ in SwiftLogNoOpLogHandler() })
415432
}
416433

417434
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@@ -444,7 +461,6 @@ extension ConnectionPoolConfiguration {
444461
}
445462
}
446463

447-
@_spi(ConnectionPool)
448464
extension PostgresConnection: PooledConnection {
449465
public func close() {
450466
self.channel.close(mode: .all, promise: nil)

Tests/IntegrationTests/PostgresClientTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class PostgresClientTests: XCTestCase {
2727

2828
let iterations = 1000
2929

30-
for i in 0..<iterations {
30+
for _ in 0..<iterations {
3131
taskGroup.addTask {
3232
try await client.withConnection() { connection in
3333
_ = try await connection.query("SELECT 1", logger: logger)

0 commit comments

Comments
 (0)