Skip to content

Commit e403140

Browse files
authored
Fix connection sendability issues (#357)
1 parent 45be183 commit e403140

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Sources/MongoClient/Connection.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ public final actor MongoConnection: Sendable {
111111

112112
/// Registers MongoKitten's handlers on the channel
113113
public static func addHandlers(to channel: Channel, context: MongoClientContext) -> EventLoopFuture<Void> {
114-
let parser = ClientConnectionParser(context: context)
115-
return channel.pipeline.addHandler(ByteToMessageHandler(parser))
114+
channel.eventLoop.makeCompletedFuture {
115+
let parser = ClientConnectionParser(context: context)
116+
return try channel.pipeline.syncOperations.addHandler(ByteToMessageHandler(parser))
117+
}
116118
}
117119

118120
public func ping() async throws {
@@ -137,7 +139,7 @@ public final actor MongoConnection: Sendable {
137139
public static func connect(
138140
settings: ConnectionSettings,
139141
logger: Logger = Logger(label: "org.orlandos-nl.mongokitten.connection"),
140-
resolver: Resolver? = nil,
142+
resolver: (Resolver & Sendable)? = nil,
141143
clientDetails: MongoClientDetails? = nil
142144
) async throws -> MongoConnection {
143145
#if canImport(NIOTransportServices) && os(iOS)
@@ -151,7 +153,7 @@ public final actor MongoConnection: Sendable {
151153
settings: ConnectionSettings,
152154
logger: Logger = Logger(label: "org.orlandos-nl.mongokitten.connection"),
153155
onGroup group: _MongoPlatformEventLoopGroup,
154-
resolver: Resolver? = nil,
156+
resolver: (Resolver & Sendable)? = nil,
155157
clientDetails: MongoClientDetails? = nil,
156158
sessionManager: MongoSessionManager = .init()
157159
) async throws -> MongoConnection {
@@ -187,21 +189,19 @@ public final actor MongoConnection: Sendable {
187189
#if canImport(NIOTransportServices) && os(iOS)
188190
#else
189191
if settings.useSSL {
190-
do {
191-
var configuration = TLSConfiguration.clientDefault
192+
var configuration = TLSConfiguration.clientDefault
192193

193-
if let caCert = settings.sslCaCertificate {
194-
configuration.trustRoots = NIOSSLTrustRoots.certificates([caCert])
195-
} else if let caCertPath = settings.sslCaCertificatePath {
196-
configuration.trustRoots = NIOSSLTrustRoots.file(caCertPath)
197-
}
194+
if let caCert = settings.sslCaCertificate {
195+
configuration.trustRoots = NIOSSLTrustRoots.certificates([caCert])
196+
} else if let caCertPath = settings.sslCaCertificatePath {
197+
configuration.trustRoots = NIOSSLTrustRoots.file(caCertPath)
198+
}
198199

200+
return channel.eventLoop.makeCompletedFuture {
199201
let handler = try NIOSSLClientHandler(context: NIOSSLContext(configuration: configuration), serverHostname: host.hostname)
200-
return channel.pipeline.addHandler(handler).flatMap {
201-
return MongoConnection.addHandlers(to: channel, context: context)
202-
}
203-
} catch {
204-
return channel.eventLoop.makeFailedFuture(error)
202+
try channel.pipeline.syncOperations.addHandler(handler)
203+
}.flatMap {
204+
return MongoConnection.addHandlers(to: channel, context: context)
205205
}
206206
}
207207
#endif

0 commit comments

Comments
 (0)