Skip to content

Commit f310f88

Browse files
authored
Support connection pool timeout configuration (#161)
* expose connection pool timeout via postgres connection creation functions. * Address PR feedback by explicitly requiring the version of async-kit that introduced connection pool timeouts and by renaming the timeout property and initializer argument label.
1 parent 7b5970c commit f310f88

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ let package = Package(
1010
.library(name: "FluentPostgresDriver", targets: ["FluentPostgresDriver"]),
1111
],
1212
dependencies: [
13+
.package(url: "https://github.com/vapor/async-kit.git", from: "1.2.0"),
1314
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0"),
1415
.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.0.0"),
1516
],
1617
targets: [
1718
.target(name: "FluentPostgresDriver", dependencies: [
19+
.product(name: "AsyncKit", package: "async-kit"),
1820
.product(name: "FluentKit", package: "fluent-kit"),
1921
.product(name: "FluentSQL", package: "fluent-kit"),
2022
.product(name: "PostgresKit", package: "postgres-kit"),

Sources/FluentPostgresDriver/FluentPostgresConfiguration.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extension DatabaseConfigurationFactory {
22
public static func postgres(
33
url urlString: String,
44
maxConnectionsPerEventLoop: Int = 1,
5+
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
56
encoder: PostgresDataEncoder = .init(),
67
decoder: PostgresDataDecoder = .init()
78
) throws -> DatabaseConfigurationFactory {
@@ -11,6 +12,7 @@ extension DatabaseConfigurationFactory {
1112
return try .postgres(
1213
url: url,
1314
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
15+
connectionPoolTimeout: connectionPoolTimeout,
1416
encoder: encoder,
1517
decoder: decoder
1618
)
@@ -19,6 +21,7 @@ extension DatabaseConfigurationFactory {
1921
public static func postgres(
2022
url: URL,
2123
maxConnectionsPerEventLoop: Int = 1,
24+
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
2225
encoder: PostgresDataEncoder = .init(),
2326
decoder: PostgresDataDecoder = .init()
2427
) throws -> DatabaseConfigurationFactory {
@@ -27,7 +30,8 @@ extension DatabaseConfigurationFactory {
2730
}
2831
return .postgres(
2932
configuration: configuration,
30-
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop
33+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
34+
connectionPoolTimeout: connectionPoolTimeout
3135
)
3236
}
3337

@@ -39,6 +43,7 @@ extension DatabaseConfigurationFactory {
3943
database: String? = nil,
4044
tlsConfiguration: TLSConfiguration? = nil,
4145
maxConnectionsPerEventLoop: Int = 1,
46+
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
4247
encoder: PostgresDataEncoder = .init(),
4348
decoder: PostgresDataDecoder = .init()
4449
) -> DatabaseConfigurationFactory {
@@ -51,13 +56,15 @@ extension DatabaseConfigurationFactory {
5156
database: database,
5257
tlsConfiguration: tlsConfiguration
5358
),
54-
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop
59+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
60+
connectionPoolTimeout: connectionPoolTimeout
5561
)
5662
}
5763

5864
public static func postgres(
5965
configuration: PostgresConfiguration,
6066
maxConnectionsPerEventLoop: Int = 1,
67+
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
6168
encoder: PostgresDataEncoder = .init(),
6269
decoder: PostgresDataDecoder = .init()
6370
) -> DatabaseConfigurationFactory {
@@ -66,6 +73,7 @@ extension DatabaseConfigurationFactory {
6673
middleware: [],
6774
configuration: configuration,
6875
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
76+
connectionPoolTimeout: connectionPoolTimeout,
6977
encoder: encoder,
7078
decoder: decoder
7179
)
@@ -77,6 +85,9 @@ struct FluentPostgresConfiguration: DatabaseConfiguration {
7785
var middleware: [AnyModelMiddleware]
7886
let configuration: PostgresConfiguration
7987
let maxConnectionsPerEventLoop: Int
88+
/// The amount of time to wait for a connection from
89+
/// the connection pool before timing out.
90+
let connectionPoolTimeout: NIO.TimeAmount
8091
let encoder: PostgresDataEncoder
8192
let decoder: PostgresDataDecoder
8293

@@ -87,6 +98,7 @@ struct FluentPostgresConfiguration: DatabaseConfiguration {
8798
let pool = EventLoopGroupConnectionPool(
8899
source: db,
89100
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
101+
requestTimeout: connectionPoolTimeout,
90102
on: databases.eventLoopGroup
91103
)
92104
return _FluentPostgresDriver(

0 commit comments

Comments
 (0)