@@ -301,11 +301,11 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
301
301
/// - Returns: The closure's return value.
302
302
@_disfavoredOverload
303
303
public func withConnection< Result> ( _ closure: ( PostgresConnection ) async throws -> Result ) async throws -> Result {
304
- let connection = try await self . leaseConnection ( )
304
+ let lease = try await self . leaseConnection ( )
305
305
306
- defer { self . pool . releaseConnection ( connection ) }
306
+ defer { lease . release ( ) }
307
307
308
- return try await closure ( connection)
308
+ return try await closure ( lease . connection)
309
309
}
310
310
311
311
#if compiler(>=6.0)
@@ -319,11 +319,11 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
319
319
// DO NOT FIX THE WHITESPACE IN THE NEXT LINE UNTIL 5.10 IS UNSUPPORTED
320
320
// https://github.com/swiftlang/swift/issues/79285
321
321
_ closure: ( PostgresConnection ) async throws -> sending Result) async throws -> sending Result {
322
- let connection = try await self . leaseConnection ( )
322
+ let lease = try await self . leaseConnection ( )
323
323
324
- defer { self . pool . releaseConnection ( connection ) }
324
+ defer { lease . release ( ) }
325
325
326
- return try await closure ( connection)
326
+ return try await closure ( lease . connection)
327
327
}
328
328
329
329
/// Lease a connection, which is in an open transaction state, for the provided `closure`'s lifetime.
@@ -404,7 +404,8 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
404
404
throw PSQLError ( code: . tooManyParameters, query: query, file: file, line: line)
405
405
}
406
406
407
- let connection = try await self . leaseConnection ( )
407
+ let lease = try await self . leaseConnection ( )
408
+ let connection = lease. connection
408
409
409
410
var logger = logger
410
411
logger [ postgresMetadataKey: . connectionID] = " \( connection. id) "
@@ -419,12 +420,12 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
419
420
connection. channel. write ( HandlerTask . extendedQuery ( context) , promise: nil )
420
421
421
422
promise. futureResult. whenFailure { _ in
422
- self . pool . releaseConnection ( connection )
423
+ lease . release ( )
423
424
}
424
425
425
426
return try await promise. futureResult. map {
426
427
$0. asyncSequence ( onFinish: {
427
- self . pool . releaseConnection ( connection )
428
+ lease . release ( )
428
429
} )
429
430
} . get ( )
430
431
} catch var error as PSQLError {
@@ -446,7 +447,8 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
446
447
let logger = logger ?? Self . loggingDisabled
447
448
448
449
do {
449
- let connection = try await self . leaseConnection ( )
450
+ let lease = try await self . leaseConnection ( )
451
+ let connection = lease. connection
450
452
451
453
let promise = connection. channel. eventLoop. makePromise ( of: PSQLRowStream . self)
452
454
let task = HandlerTask . executePreparedStatement ( . init(
@@ -460,11 +462,11 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
460
462
connection. channel. write ( task, promise: nil )
461
463
462
464
promise. futureResult. whenFailure { _ in
463
- self . pool . releaseConnection ( connection )
465
+ lease . release ( )
464
466
}
465
467
466
468
return try await promise. futureResult
467
- . map { $0. asyncSequence ( onFinish: { self . pool . releaseConnection ( connection ) } ) }
469
+ . map { $0. asyncSequence ( onFinish: { lease . release ( ) } ) }
468
470
. get ( )
469
471
. map { try preparedStatement. decodeRow ( $0) }
470
472
} catch var error as PSQLError {
@@ -504,7 +506,7 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
504
506
505
507
// MARK: - Private Methods -
506
508
507
- private func leaseConnection( ) async throws -> PostgresConnection {
509
+ private func leaseConnection( ) async throws -> ConnectionLease < PostgresConnection > {
508
510
if !self . runningAtomic. load ( ordering: . relaxed) {
509
511
self . backgroundLogger. warning ( " Trying to lease connection from `PostgresClient`, but `PostgresClient.run()` hasn't been called yet. " )
510
512
}
0 commit comments