@@ -6,7 +6,7 @@ import Foundation
66import PostgresNIO
77
88/// Settings for the pool like connection parameters.
9- public struct PoolConfiguration {
9+ public struct PoolConfiguration : Sendable {
1010
1111 /// PostgreSQL connection parameters.
1212 @available ( * , deprecated, message: " Use `PostgresConnection.Configuration` etc. instead. " )
@@ -57,15 +57,15 @@ public struct PoolConfiguration {
5757 /// Called when new connections to the database are openend.
5858 ///
5959 /// Use this to set extra connection options or override the defaults.
60- public var onOpenConnection : ( ( PostgresConnection , Logger ) async throws -> Void ) ?
60+ public let onOpenConnection : ( @ Sendable ( PostgresConnection, Logger) async throws -> Void ) ?
6161
6262 /// Called before a connection is given to a client.
6363 ///
6464 /// Default is to do a quick connection check with "SELECT 1" and close the connection on errors.
65- public var onReturnConnection : ( ( PostgresConnection , Logger ) async throws -> Void ) ?
65+ public let onReturnConnection : ( @ Sendable ( PostgresConnection, Logger) async throws -> Void ) ?
6666
6767 /// Called just before a connection is being closed.
68- public var onCloseConnection : ( ( PostgresConnection , Logger ) async throws -> Void ) ?
68+ public let onCloseConnection : ( @ Sendable ( PostgresConnection, Logger) async throws -> Void ) ?
6969
7070 /// Pool configuation.
7171 ///
@@ -76,14 +76,20 @@ public struct PoolConfiguration {
7676 /// - queryTimeout: TImeout for individual database queries
7777 /// - poolSize: The maximum number of open connections
7878 /// - maxIdleConnections: The maximum number of idle connections
79+ /// - onOpenConnection: Called when new connections to the database are openend
80+ /// - onReturnConnection: Called before a connection is given to a client
81+ /// - onCloseConnection: Called just before a connection is being closed
7982 public init (
8083 applicationName: String ,
8184 postgresConfiguration: PostgresConnection . Configuration ,
8285 connectTimeout: TimeInterval = 5.0 ,
8386 connectionRetryInterval: TimeInterval = 0.5 ,
8487 queryTimeout: TimeInterval ? = nil ,
8588 poolSize: Int = 10 ,
86- maxIdleConnections: Int ? = nil )
89+ maxIdleConnections: Int ? = nil ,
90+ onOpenConnection: ( @Sendable ( PostgresConnection, Logger) async throws -> Void ) ? = nil ,
91+ onReturnConnection: ( @Sendable ( PostgresConnection, Logger) async throws -> Void ) ? = nil ,
92+ onCloseConnection: ( @Sendable ( PostgresConnection, Logger) async throws -> Void ) ? = nil )
8793 {
8894 self . applicationName = applicationName
8995 self . postgresConfiguration = postgresConfiguration
@@ -93,9 +99,11 @@ public struct PoolConfiguration {
9399 self . poolSize = poolSize. atLeast ( 1 )
94100 self . maxIdleConnections = maxIdleConnections? . atLeast ( 0 )
95101
96- self . onReturnConnection = { connection, logger in
102+ self . onOpenConnection = onOpenConnection
103+ self . onReturnConnection = onReturnConnection ?? { connection, logger in
97104 try await connection. query ( " SELECT 1 " , logger: logger)
98105 }
106+ self . onCloseConnection = onCloseConnection
99107 }
100108
101109 @available ( * , deprecated, message: " Use `init(applicationName:postgresConfiguration:connectTimeout:queryTimeout:poolSize:maxIdleConnections:)` instead. " )
0 commit comments