@@ -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}
0 commit comments