@@ -301,11 +301,11 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
301301 /// - Returns: The closure's return value.
302302 @_disfavoredOverload
303303 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 ( )
305305
306- defer { self . pool . releaseConnection ( connection ) }
306+ defer { lease . release ( ) }
307307
308- return try await closure ( connection)
308+ return try await closure ( lease . connection)
309309 }
310310
311311 #if compiler(>=6.0)
@@ -319,11 +319,11 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
319319 // DO NOT FIX THE WHITESPACE IN THE NEXT LINE UNTIL 5.10 IS UNSUPPORTED
320320 // https://github.com/swiftlang/swift/issues/79285
321321 _ closure: ( PostgresConnection ) async throws -> sending Result) async throws -> sending Result {
322- let connection = try await self . leaseConnection ( )
322+ let lease = try await self . leaseConnection ( )
323323
324- defer { self . pool . releaseConnection ( connection ) }
324+ defer { lease . release ( ) }
325325
326- return try await closure ( connection)
326+ return try await closure ( lease . connection)
327327 }
328328
329329 /// 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 {
404404 throw PSQLError ( code: . tooManyParameters, query: query, file: file, line: line)
405405 }
406406
407- let connection = try await self . leaseConnection ( )
407+ let lease = try await self . leaseConnection ( )
408+ let connection = lease. connection
408409
409410 var logger = logger
410411 logger [ postgresMetadataKey: . connectionID] = " \( connection. id) "
@@ -419,12 +420,12 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
419420 connection. channel. write ( HandlerTask . extendedQuery ( context) , promise: nil )
420421
421422 promise. futureResult. whenFailure { _ in
422- self . pool . releaseConnection ( connection )
423+ lease . release ( )
423424 }
424425
425426 return try await promise. futureResult. map {
426427 $0. asyncSequence ( onFinish: {
427- self . pool . releaseConnection ( connection )
428+ lease . release ( )
428429 } )
429430 } . get ( )
430431 } catch var error as PSQLError {
@@ -446,7 +447,8 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
446447 let logger = logger ?? Self . loggingDisabled
447448
448449 do {
449- let connection = try await self . leaseConnection ( )
450+ let lease = try await self . leaseConnection ( )
451+ let connection = lease. connection
450452
451453 let promise = connection. channel. eventLoop. makePromise ( of: PSQLRowStream . self)
452454 let task = HandlerTask . executePreparedStatement ( . init(
@@ -460,11 +462,11 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
460462 connection. channel. write ( task, promise: nil )
461463
462464 promise. futureResult. whenFailure { _ in
463- self . pool . releaseConnection ( connection )
465+ lease . release ( )
464466 }
465467
466468 return try await promise. futureResult
467- . map { $0. asyncSequence ( onFinish: { self . pool . releaseConnection ( connection ) } ) }
469+ . map { $0. asyncSequence ( onFinish: { lease . release ( ) } ) }
468470 . get ( )
469471 . map { try preparedStatement. decodeRow ( $0) }
470472 } catch var error as PSQLError {
@@ -504,7 +506,7 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
504506
505507 // MARK: - Private Methods -
506508
507- private func leaseConnection( ) async throws -> PostgresConnection {
509+ private func leaseConnection( ) async throws -> ConnectionLease < PostgresConnection > {
508510 if !self . runningAtomic. load ( ordering: . relaxed) {
509511 self . backgroundLogger. warning ( " Trying to lease connection from `PostgresClient`, but `PostgresClient.run()` hasn't been called yet. " )
510512 }
0 commit comments