Skip to content

Commit 2825829

Browse files
traschfabianfett
andauthored
Expose connectTimeout as a configuration option (#276)
fixes: #273 Expose connectTimeout as a configuration option Co-authored-by: Fabian Fett <fabianfett@apple.com>
1 parent 2ddc2e1 commit 2825829

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Sources/PostgresNIO/Connection/PostgresConnection.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ public final class PostgresConnection {
7878
/// - Default: 5432
7979
public var port: Int
8080

81+
/// Specifies a timeout to apply to a connection attempt.
82+
///
83+
/// - Default: 10 seconds
84+
public var connectTimeout: TimeAmount
85+
8186
public init(host: String, port: Int = 5432) {
8287
self.host = host
8388
self.port = port
89+
self.connectTimeout = .seconds(10)
8490
}
8591
}
8692

@@ -281,12 +287,12 @@ public final class PostgresConnection {
281287
) -> NIOClientTCPBootstrapProtocol {
282288
#if canImport(Network)
283289
if let tsBootstrap = NIOTSConnectionBootstrap(validatingGroup: eventLoop) {
284-
return tsBootstrap
290+
return tsBootstrap.connectTimeout(configuration.connectTimeout)
285291
}
286292
#endif
287293

288294
if let nioBootstrap = ClientBootstrap(validatingGroup: eventLoop) {
289-
return nioBootstrap
295+
return nioBootstrap.connectTimeout(configuration.connectTimeout)
290296
}
291297

292298
fatalError("No matching bootstrap found")
@@ -393,6 +399,7 @@ extension PostgresConnection {
393399
return tlsFuture.flatMap { tls in
394400
let configuration = PostgresConnection.InternalConfiguration(
395401
connection: .resolved(address: socketAddress, serverName: serverHostname),
402+
connectTimeout: .seconds(10),
396403
authentication: nil,
397404
tls: tls
398405
)
@@ -752,6 +759,7 @@ extension PostgresConnection {
752759
}
753760

754761
var connection: Connection
762+
var connectTimeout: TimeAmount
755763

756764
var authentication: Configuration.Authentication?
757765

@@ -763,6 +771,7 @@ extension PostgresConnection.InternalConfiguration {
763771
init(_ config: PostgresConnection.Configuration) {
764772
self.authentication = config.authentication
765773
self.connection = .unresolved(host: config.connection.host, port: config.connection.port)
774+
self.connectTimeout = config.connection.connectTimeout
766775
self.tls = config.tls
767776
}
768777
}

Tests/PostgresNIOTests/New/PostgresChannelHandlerTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ class PostgresChannelHandlerTests: XCTestCase {
173173
username: String = "test",
174174
database: String = "postgres",
175175
password: String = "password",
176-
tls: PostgresConnection.Configuration.TLS = .disable
176+
tls: PostgresConnection.Configuration.TLS = .disable,
177+
connectTimeout: TimeAmount = .seconds(10)
177178
) -> PostgresConnection.InternalConfiguration {
178179
let authentication = PostgresConnection.Configuration.Authentication(
179180
username: username,
@@ -183,6 +184,7 @@ class PostgresChannelHandlerTests: XCTestCase {
183184

184185
return PostgresConnection.InternalConfiguration(
185186
connection: .unresolved(host: host, port: port),
187+
connectTimeout: connectTimeout,
186188
authentication: authentication,
187189
tls: tls
188190
)

0 commit comments

Comments
 (0)