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