@@ -15,13 +15,19 @@ extension DatabaseConfigurationFactory {
15
15
/// - urlString: The URL describing the connection, as a string.
16
16
/// - maxConnectionsPerEventLoop: Maximum number of connections to open per event loop.
17
17
/// - connectionPoolTimeout: Maximum time to wait for a connection to become available per request.
18
+ /// - pruneInterval: How often to check for and prune idle database connections. If `nil` (the default),
19
+ /// no pruning is performed.
20
+ /// - maxIdleTimeBeforePruning: How long a connection may remain idle before being pruned, if pruning is enabled.
21
+ /// Defaults to 2 minutes. Ignored if `pruneInterval` is `nil`.
18
22
/// - encodingContext: Encoding context to use for serializing data.
19
23
/// - decodingContext: Decoding context to use for deserializing data.
20
24
/// - sqlLogLevel: Level at which to log SQL queries.
21
25
public static func postgres(
22
26
url urlString: String ,
23
27
maxConnectionsPerEventLoop: Int = 1 ,
24
28
connectionPoolTimeout: TimeAmount = . seconds( 10 ) ,
29
+ pruneInterval: TimeAmount ? = nil ,
30
+ maxIdleTimeBeforePruning: TimeAmount = . seconds( 120 ) ,
25
31
encodingContext: PostgresEncodingContext < some PostgresJSONEncoder > = . default,
26
32
decodingContext: PostgresDecodingContext < some PostgresJSONDecoder > = . default,
27
33
sqlLogLevel: Logger . Level = . debug
@@ -30,6 +36,8 @@ extension DatabaseConfigurationFactory {
30
36
configuration: try . init( url: urlString) ,
31
37
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
32
38
connectionPoolTimeout: connectionPoolTimeout,
39
+ pruneInterval: pruneInterval,
40
+ maxIdleTimeBeforePruning: maxIdleTimeBeforePruning,
33
41
encodingContext: encodingContext,
34
42
decodingContext: decodingContext,
35
43
sqlLogLevel: sqlLogLevel
@@ -44,13 +52,19 @@ extension DatabaseConfigurationFactory {
44
52
/// - url: The URL describing the connection.
45
53
/// - maxConnectionsPerEventLoop: Maximum number of connections to open per event loop.
46
54
/// - connectionPoolTimeout: Maximum time to wait for a connection to become available per request.
55
+ /// - pruneInterval: How often to check for and prune idle database connections. If `nil` (the default),
56
+ /// no pruning is performed.
57
+ /// - maxIdleTimeBeforePruning: How long a connection may remain idle before being pruned, if pruning is enabled.
58
+ /// Defaults to 2 minutes. Ignored if `pruneInterval` is `nil`.
47
59
/// - encodingContext: Encoding context to use for serializing data.
48
60
/// - decodingContext: Decoding context to use for deserializing data.
49
61
/// - sqlLogLevel: Level at which to log SQL queries.
50
62
public static func postgres(
51
63
url: URL ,
52
64
maxConnectionsPerEventLoop: Int = 1 ,
53
65
connectionPoolTimeout: TimeAmount = . seconds( 10 ) ,
66
+ pruneInterval: TimeAmount ? = nil ,
67
+ maxIdleTimeBeforePruning: TimeAmount = . seconds( 120 ) ,
54
68
encodingContext: PostgresEncodingContext < some PostgresJSONEncoder > = . default,
55
69
decodingContext: PostgresDecodingContext < some PostgresJSONDecoder > = . default,
56
70
sqlLogLevel: Logger . Level = . debug
@@ -59,6 +73,8 @@ extension DatabaseConfigurationFactory {
59
73
configuration: try . init( url: url) ,
60
74
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
61
75
connectionPoolTimeout: connectionPoolTimeout,
76
+ pruneInterval: pruneInterval,
77
+ maxIdleTimeBeforePruning: maxIdleTimeBeforePruning,
62
78
encodingContext: encodingContext,
63
79
decodingContext: decodingContext,
64
80
sqlLogLevel: sqlLogLevel
@@ -71,13 +87,19 @@ extension DatabaseConfigurationFactory {
71
87
/// - configuration: A ``PostgresKit/SQLPostgresConfiguration`` describing the connection.
72
88
/// - maxConnectionsPerEventLoop: Maximum number of connections to open per event loop.
73
89
/// - connectionPoolTimeout: Maximum time to wait for a connection to become available per request.
90
+ /// - pruneInterval: How often to check for and prune idle database connections. If `nil` (the default),
91
+ /// no pruning is performed.
92
+ /// - maxIdleTimeBeforePruning: How long a connection may remain idle before being pruned, if pruning is enabled.
93
+ /// Defaults to 2 minutes. Ignored if `pruneInterval` is `nil`.
74
94
/// - encodingContext: Encoding context to use for serializing data.
75
95
/// - decodingContext: Decoding context to use for deserializing data.
76
96
/// - sqlLogLevel: Level at which to log SQL queries.
77
97
public static func postgres(
78
98
configuration: SQLPostgresConfiguration ,
79
99
maxConnectionsPerEventLoop: Int = 1 ,
80
100
connectionPoolTimeout: TimeAmount = . seconds( 10 ) ,
101
+ pruneInterval: TimeAmount ? = nil ,
102
+ maxIdleTimeBeforePruning: TimeAmount = . seconds( 120 ) ,
81
103
encodingContext: PostgresEncodingContext < some PostgresJSONEncoder > ,
82
104
decodingContext: PostgresDecodingContext < some PostgresJSONDecoder > ,
83
105
sqlLogLevel: Logger . Level = . debug
@@ -87,6 +109,8 @@ extension DatabaseConfigurationFactory {
87
109
configuration: configuration,
88
110
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
89
111
connectionPoolTimeout: connectionPoolTimeout,
112
+ pruningInterval: pruneInterval,
113
+ maxIdleTimeBeforePruning: maxIdleTimeBeforePruning,
90
114
encodingContext: encodingContext,
91
115
decodingContext: decodingContext,
92
116
sqlLogLevel: sqlLogLevel
@@ -108,56 +132,68 @@ extension DatabaseConfigurationFactory {
108
132
///
109
133
/// _ = DatabaseConfigurationFactory.postgres(configuration: .init(unixDomainSocketPath: "", username: ""))
110
134
extension DatabaseConfigurationFactory {
111
- /// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)``
135
+ /// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:pruneInterval:maxIdleTimeBeforePruning: encodingContext:decodingContext:sqlLogLevel:)``
112
136
/// with the `decodingContext` defaulted.
113
137
public static func postgres(
114
138
configuration: SQLPostgresConfiguration ,
115
139
maxConnectionsPerEventLoop: Int = 1 ,
116
140
connectionPoolTimeout: TimeAmount = . seconds( 10 ) ,
141
+ pruneInterval: TimeAmount ? = nil ,
142
+ maxIdleTimeBeforePruning: TimeAmount = . seconds( 120 ) ,
117
143
encodingContext: PostgresEncodingContext < some PostgresJSONEncoder > ,
118
144
sqlLogLevel: Logger . Level = . debug
119
145
) -> DatabaseConfigurationFactory {
120
146
. postgres(
121
147
configuration: configuration,
122
148
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
123
149
connectionPoolTimeout: connectionPoolTimeout,
150
+ pruneInterval: pruneInterval,
151
+ maxIdleTimeBeforePruning: maxIdleTimeBeforePruning,
124
152
encodingContext: encodingContext,
125
153
decodingContext: . default,
126
154
sqlLogLevel: sqlLogLevel
127
155
)
128
156
}
129
157
130
- /// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)``
158
+ /// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:pruneInterval:maxIdleTimeBeforePruning: encodingContext:decodingContext:sqlLogLevel:)``
131
159
/// with the `encodingContext` defaulted.
132
160
public static func postgres(
133
161
configuration: SQLPostgresConfiguration ,
134
162
maxConnectionsPerEventLoop: Int = 1 ,
135
163
connectionPoolTimeout: TimeAmount = . seconds( 10 ) ,
164
+ pruneInterval: TimeAmount ? = nil ,
165
+ maxIdleTimeBeforePruning: TimeAmount = . seconds( 120 ) ,
136
166
decodingContext: PostgresDecodingContext < some PostgresJSONDecoder > ,
137
167
sqlLogLevel: Logger . Level = . debug
138
168
) -> DatabaseConfigurationFactory {
139
169
. postgres(
140
170
configuration: configuration,
141
171
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
142
172
connectionPoolTimeout: connectionPoolTimeout,
173
+ pruneInterval: pruneInterval,
174
+ maxIdleTimeBeforePruning: maxIdleTimeBeforePruning,
143
175
encodingContext: . default,
144
176
decodingContext: decodingContext,
145
177
sqlLogLevel: sqlLogLevel
146
178
)
147
179
}
148
180
149
- /// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)``
181
+ /// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:pruneInterval:maxIdleTimeBeforePruning: encodingContext:decodingContext:sqlLogLevel:)``
150
182
/// with both `encodingContext` and `decodingContext` defaulted.
151
183
public static func postgres(
152
184
configuration: SQLPostgresConfiguration ,
153
185
maxConnectionsPerEventLoop: Int = 1 ,
154
186
connectionPoolTimeout: TimeAmount = . seconds( 10 ) ,
187
+ pruneInterval: TimeAmount ? = nil ,
188
+ maxIdleTimeBeforePruning: TimeAmount = . seconds( 120 ) ,
155
189
sqlLogLevel: Logger . Level = . debug
156
190
) -> DatabaseConfigurationFactory {
157
191
. postgres(
158
192
configuration: configuration,
159
193
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
160
194
connectionPoolTimeout: connectionPoolTimeout,
195
+ pruneInterval: pruneInterval,
196
+ maxIdleTimeBeforePruning: maxIdleTimeBeforePruning,
161
197
encodingContext: . default,
162
198
decodingContext: . default,
163
199
sqlLogLevel: sqlLogLevel
@@ -171,6 +207,8 @@ struct FluentPostgresConfiguration<E: PostgresJSONEncoder, D: PostgresJSONDecode
171
207
fileprivate let configuration : SQLPostgresConfiguration
172
208
let maxConnectionsPerEventLoop : Int
173
209
let connectionPoolTimeout : TimeAmount
210
+ let pruningInterval : TimeAmount ?
211
+ let maxIdleTimeBeforePruning : TimeAmount
174
212
let encodingContext : PostgresEncodingContext < E >
175
213
let decodingContext : PostgresDecodingContext < D >
176
214
let sqlLogLevel : Logger . Level
@@ -181,6 +219,8 @@ struct FluentPostgresConfiguration<E: PostgresJSONEncoder, D: PostgresJSONDecode
181
219
source: connectionSource,
182
220
maxConnectionsPerEventLoop: self . maxConnectionsPerEventLoop,
183
221
requestTimeout: self . connectionPoolTimeout,
222
+ pruneInterval: self . pruningInterval,
223
+ maxIdleTimeBeforePruning: self . maxIdleTimeBeforePruning,
184
224
on: databases. eventLoopGroup
185
225
)
186
226
0 commit comments