Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#273: Expose connectTimeout as a configuration option #276

Merged
merged 3 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Sources/PostgresNIO/Connection/PostgresConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ public final class PostgresConnection {
/// - Default: 5432
public var port: Int

/// Specifies a timeout to apply to a connection attempt.
///
/// - Default: 10 seconds
public var connectTimeout: TimeAmount

public init(host: String, port: Int = 5432) {
self.host = host
self.port = port
self.connectTimeout = .seconds(10)
}
}

Expand Down Expand Up @@ -281,12 +287,12 @@ public final class PostgresConnection {
) -> NIOClientTCPBootstrapProtocol {
#if canImport(Network)
if let tsBootstrap = NIOTSConnectionBootstrap(validatingGroup: eventLoop) {
return tsBootstrap
return tsBootstrap.connectTimeout(configuration.connectTimeout)
}
#endif

if let nioBootstrap = ClientBootstrap(validatingGroup: eventLoop) {
return nioBootstrap
return nioBootstrap.connectTimeout(configuration.connectTimeout)
}

fatalError("No matching bootstrap found")
Expand Down Expand Up @@ -393,6 +399,7 @@ extension PostgresConnection {
return tlsFuture.flatMap { tls in
let configuration = PostgresConnection.InternalConfiguration(
connection: .resolved(address: socketAddress, serverName: serverHostname),
connectTimeout: .seconds(10),
authentication: nil,
tls: tls
)
Expand Down Expand Up @@ -752,6 +759,7 @@ extension PostgresConnection {
}

var connection: Connection
var connectTimeout: TimeAmount

var authentication: Configuration.Authentication?

Expand All @@ -763,6 +771,7 @@ extension PostgresConnection.InternalConfiguration {
init(_ config: PostgresConnection.Configuration) {
self.authentication = config.authentication
self.connection = .unresolved(host: config.connection.host, port: config.connection.port)
self.connectTimeout = config.connection.connectTimeout
self.tls = config.tls
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class PostgresChannelHandlerTests: XCTestCase {
username: String = "test",
database: String = "postgres",
password: String = "password",
tls: PostgresConnection.Configuration.TLS = .disable
tls: PostgresConnection.Configuration.TLS = .disable,
connectTimeout: TimeAmount = .seconds(10)
) -> PostgresConnection.InternalConfiguration {
let authentication = PostgresConnection.Configuration.Authentication(
username: username,
Expand All @@ -183,6 +184,7 @@ class PostgresChannelHandlerTests: XCTestCase {

return PostgresConnection.InternalConfiguration(
connection: .unresolved(host: host, port: port),
connectTimeout: connectTimeout,
authentication: authentication,
tls: tls
)
Expand Down