forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionFactory.swift
206 lines (170 loc) · 7.36 KB
/
ConnectionFactory.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import Logging
import NIOConcurrencyHelpers
import NIOCore
import NIOSSL
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
final class ConnectionFactory: Sendable {
struct ConfigCache: Sendable {
var config: PostgresClient.Configuration
}
let configBox: NIOLockedValueBox<ConfigCache>
struct SSLContextCache: Sendable {
enum State {
case none
case producing(TLSConfiguration, [CheckedContinuation<NIOSSLContext, any Error>])
case cached(TLSConfiguration, NIOSSLContext)
case failed(TLSConfiguration, any Error)
}
var state: State = .none
}
let sslContextBox = NIOLockedValueBox(SSLContextCache())
let eventLoopGroup: any EventLoopGroup
let logger: Logger
init(config: PostgresClient.Configuration, eventLoopGroup: any EventLoopGroup, logger: Logger) {
self.eventLoopGroup = eventLoopGroup
self.configBox = NIOLockedValueBox(ConfigCache(config: config))
self.logger = logger
}
func makeConnection(_ connectionID: PostgresConnection.ID, pool: PostgresClient.Pool) async throws -> PostgresConnection {
let config = try await self.makeConnectionConfig()
var connectionLogger = self.logger
connectionLogger[postgresMetadataKey: .connectionID] = "\(connectionID)"
return try await PostgresConnection.connect(
on: self.eventLoopGroup.any(),
configuration: config,
id: connectionID,
logger: connectionLogger
).get()
}
func makeConnectionConfig() async throws -> PostgresConnection.Configuration {
let config = self.configBox.withLockedValue { $0.config }
let tls: PostgresConnection.Configuration.TLS
switch config.tls.base {
case .prefer(let tlsConfiguration):
let sslContext = try await self.getSSLContext(for: tlsConfiguration)
tls = .prefer(sslContext)
case .require(let tlsConfiguration):
let sslContext = try await self.getSSLContext(for: tlsConfiguration)
tls = .require(sslContext)
case .disable:
tls = .disable
}
var connectionConfig: PostgresConnection.Configuration
switch config.endpointInfo {
case .bindUnixDomainSocket(let path):
connectionConfig = PostgresConnection.Configuration(
unixSocketPath: path,
username: config.username,
password: config.password,
database: config.database
)
case .connectTCP(let host, let port):
connectionConfig = PostgresConnection.Configuration(
host: host,
port: port,
username: config.username,
password: config.password,
database: config.database,
tls: tls
)
}
connectionConfig.options.connectTimeout = TimeAmount(config.options.connectTimeout)
connectionConfig.options.tlsServerName = config.options.tlsServerName
connectionConfig.options.requireBackendKeyData = config.options.requireBackendKeyData
return connectionConfig
}
private func getSSLContext(for tlsConfiguration: TLSConfiguration) async throws -> NIOSSLContext {
enum Action {
case produce
case succeed(NIOSSLContext)
case fail(any Error)
case wait
}
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<NIOSSLContext, any Error>) in
let action = self.sslContextBox.withLockedValue { cache -> Action in
switch cache.state {
case .none:
cache.state = .producing(tlsConfiguration, [continuation])
return .produce
case .cached(let cachedTLSConfiguration, let context):
if cachedTLSConfiguration.bestEffortEquals(tlsConfiguration) {
return .succeed(context)
} else {
cache.state = .producing(tlsConfiguration, [continuation])
return .produce
}
case .failed(let cachedTLSConfiguration, let error):
if cachedTLSConfiguration.bestEffortEquals(tlsConfiguration) {
return .fail(error)
} else {
cache.state = .producing(tlsConfiguration, [continuation])
return .produce
}
case .producing(let cachedTLSConfiguration, var continuations):
continuations.append(continuation)
if cachedTLSConfiguration.bestEffortEquals(tlsConfiguration) {
cache.state = .producing(cachedTLSConfiguration, continuations)
return .wait
} else {
cache.state = .producing(tlsConfiguration, continuations)
return .produce
}
}
}
switch action {
case .wait:
break
case .produce:
// TBD: we might want to consider moving this off the concurrent executor
self.reportProduceSSLContextResult(
Result(catching: {try NIOSSLContext(configuration: tlsConfiguration)}),
for: tlsConfiguration
)
case .succeed(let context):
continuation.resume(returning: context)
case .fail(let error):
continuation.resume(throwing: error)
}
}
}
private func reportProduceSSLContextResult(_ result: Result<NIOSSLContext, any Error>, for tlsConfiguration: TLSConfiguration) {
enum Action {
case fail(any Error, [CheckedContinuation<NIOSSLContext, any Error>])
case succeed(NIOSSLContext, [CheckedContinuation<NIOSSLContext, any Error>])
case none
}
let action = self.sslContextBox.withLockedValue { cache -> Action in
switch cache.state {
case .none:
preconditionFailure("Invalid state: \(cache.state)")
case .cached, .failed:
return .none
case .producing(let cachedTLSConfiguration, let continuations):
if cachedTLSConfiguration.bestEffortEquals(tlsConfiguration) {
switch result {
case .success(let context):
cache.state = .cached(cachedTLSConfiguration, context)
return .succeed(context, continuations)
case .failure(let failure):
cache.state = .failed(cachedTLSConfiguration, failure)
return .fail(failure, continuations)
}
} else {
return .none
}
}
}
switch action {
case .none:
break
case .succeed(let context, let continuations):
for continuation in continuations {
continuation.resume(returning: context)
}
case .fail(let error, let continuations):
for continuation in continuations {
continuation.resume(throwing: error)
}
}
}
}