1+ import NIOConcurrencyHelpers
2+
13public struct PostgresConnectionSource : ConnectionPoolSource {
24 public let configuration : PostgresConfiguration
5+ private static let idGenerator = NIOAtomic . makeAtomic ( value: 0 )
36
47 public init ( configuration: PostgresConfiguration ) {
58 self . configuration = configuration
@@ -9,36 +12,59 @@ public struct PostgresConnectionSource: ConnectionPoolSource {
912 logger: Logger ,
1013 on eventLoop: EventLoop
1114 ) -> EventLoopFuture < PostgresConnection > {
12- let address : SocketAddress
13- do {
14- address = try self . configuration. address ( )
15- } catch {
16- return eventLoop. makeFailedFuture ( error)
17- }
18- return PostgresConnection . connect (
19- to: address,
20- tlsConfiguration: self . configuration. tlsConfiguration,
21- serverHostname: self . configuration. _hostname,
22- logger: logger,
23- on: eventLoop
24- ) . flatMap { conn in
25- return conn. authenticate (
26- username: self . configuration. username,
27- database: self . configuration. database,
28- password: self . configuration. password,
15+ if let hostname = self . configuration. _hostname {
16+ let future = PostgresConnection . connect (
17+ on: eventLoop,
18+ configuration: . init(
19+ connection: . init( host: hostname, port: self . configuration. _port ?? PostgresConfiguration . ianaPortNumber) ,
20+ authentication: . init( username: self . configuration. username, database: self . configuration. database, password: self . configuration. password) ,
21+ tls: self . configuration. tlsConfiguration. map { . require( try ! . init( configuration: $0) ) } ?? . disable
22+ ) ,
23+ id: Self . idGenerator. add ( 1 ) ,
2924 logger: logger
30- ) . flatMap {
31- if let searchPath = self . configuration. searchPath {
32- let string = searchPath. map { " \" " + $0 + " \" " } . joined ( separator: " , " )
33- return conn. simpleQuery ( " SET search_path = \( string) " )
34- . map { _ in }
35- } else {
36- return eventLoop. makeSucceededFuture ( ( ) )
25+ )
26+
27+ if let searchPath = self . configuration. searchPath {
28+ return future. flatMap { conn in
29+ let string = searchPath. map { #"" \#( $0) ""# } . joined ( separator: " , " )
30+ return conn. simpleQuery ( " SET search_path = \( string) " ) . map { _ in conn }
31+ }
32+ } else {
33+ return future
34+ }
35+ } else {
36+ let address : SocketAddress
37+ do {
38+ address = try self . configuration. address ( )
39+ } catch {
40+ return eventLoop. makeFailedFuture ( error)
41+ }
42+
43+ // Legacy code path until PostgresNIO regains support for connecting directly to a SocketAddress.
44+ return PostgresConnection . connect (
45+ to: address,
46+ tlsConfiguration: self . configuration. tlsConfiguration,
47+ serverHostname: self . configuration. _hostname,
48+ logger: logger,
49+ on: eventLoop
50+ ) . flatMap { conn in
51+ return conn. authenticate (
52+ username: self . configuration. username,
53+ database: self . configuration. database,
54+ password: self . configuration. password,
55+ logger: logger
56+ ) . flatMap {
57+ if let searchPath = self . configuration. searchPath {
58+ let string = searchPath. map { " \" " + $0 + " \" " } . joined ( separator: " , " )
59+ return conn. simpleQuery ( " SET search_path = \( string) " ) . map { _ in conn }
60+ } else {
61+ return eventLoop. makeSucceededFuture ( conn)
62+ }
63+ } . flatMapErrorThrowing { error in
64+ _ = conn. close ( )
65+ throw error
3766 }
38- } . flatMapErrorThrowing { error in
39- _ = conn. close ( )
40- throw error
41- } . map { conn }
67+ }
4268 }
4369 }
4470}
0 commit comments