Skip to content

Commit 6629d63

Browse files
authored
Use sync pipeline operations (#152)
1 parent 290f3c1 commit 6629d63

File tree

4 files changed

+45
-38
lines changed

4 files changed

+45
-38
lines changed

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ let package = Package(
1313
.library(name: "PostgresNIO", targets: ["PostgresNIO"]),
1414
],
1515
dependencies: [
16+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.28.0"),
17+
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.12.0"),
1618
.package(url: "https://github.com/apple/swift-crypto.git", from: "1.0.0"),
17-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
18-
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.0.0"),
1919
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.0.0"),
20-
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
20+
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
2121
],
2222
targets: [
2323
.target(name: "PostgresNIO", dependencies: [

Sources/PostgresNIO/New/PSQLChannelHandler.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
2020
}
2121
private var currentQuery: PSQLRows?
2222
private let authentificationConfiguration: PSQLConnection.Configuration.Authentication?
23-
private let enableSSLCallback: ((Channel) -> EventLoopFuture<Void>)?
23+
private let configureSSLCallback: ((Channel) throws -> Void)?
2424

2525
/// this delegate should only be accessed on the connections `EventLoop`
2626
weak var notificationDelegate: PSQLChannelHandlerNotificationDelegate?
2727

2828
init(authentification: PSQLConnection.Configuration.Authentication?,
2929
logger: Logger,
30-
enableSSLCallback: ((Channel) -> EventLoopFuture<Void>)? = nil)
30+
configureSSLCallback: ((Channel) throws -> Void)?)
3131
{
3232
self.state = ConnectionStateMachine()
3333
self.authentificationConfiguration = authentification
34-
self.enableSSLCallback = enableSSLCallback
34+
self.configureSSLCallback = configureSSLCallback
3535
self.logger = logger
3636
}
3737

@@ -40,11 +40,11 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
4040
init(authentification: PSQLConnection.Configuration.Authentication?,
4141
state: ConnectionStateMachine = .init(.initialized),
4242
logger: Logger = .psqlNoOpLogger,
43-
enableSSLCallback: ((Channel) -> EventLoopFuture<Void>)? = nil)
43+
configureSSLCallback: ((Channel) throws -> Void)?)
4444
{
4545
self.state = state
4646
self.authentificationConfiguration = authentification
47-
self.enableSSLCallback = enableSSLCallback
47+
self.configureSSLCallback = configureSSLCallback
4848
self.logger = logger
4949
}
5050
#endif
@@ -302,23 +302,21 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
302302
// MARK: - Private Methods -
303303

304304
private func connected(context: ChannelHandlerContext) {
305-
let action = self.state.connected(requireTLS: self.enableSSLCallback != nil)
305+
let action = self.state.connected(requireTLS: self.configureSSLCallback != nil)
306306

307307
self.run(action, with: context)
308308
}
309309

310310
private func establishSSLConnection(context: ChannelHandlerContext) {
311311
// This method must only be called, if we signalized the StateMachine before that we are
312312
// able to setup a SSL connection.
313-
self.enableSSLCallback!(context.channel).whenComplete { result in
314-
switch result {
315-
case .success:
316-
let action = self.state.sslHandlerAdded()
317-
self.run(action, with: context)
318-
case .failure(let error):
319-
let action = self.state.errorHappened(.failedToAddSSLHandler(underlying: error))
320-
self.run(action, with: context)
321-
}
313+
do {
314+
try self.configureSSLCallback!(context.channel)
315+
let action = self.state.sslHandlerAdded()
316+
self.run(action, with: context)
317+
} catch {
318+
let action = self.state.errorHappened(.failedToAddSSLHandler(underlying: error))
319+
self.run(action, with: context)
322320
}
323321
}
324322

Sources/PostgresNIO/New/PSQLConnection.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,16 @@ final class PSQLConnection {
215215
.channelInitializer { channel in
216216
let decoder = ByteToMessageHandler(PSQLBackendMessage.Decoder())
217217

218-
var enableSSLCallback: ((Channel) -> EventLoopFuture<Void>)? = nil
218+
var configureSSLCallback: ((Channel) throws -> ())? = nil
219219
if let tlsConfiguration = configuration.tlsConfiguration {
220-
enableSSLCallback = { channel in
221-
channel.eventLoop.submit {
222-
let sslContext = try NIOSSLContext(configuration: tlsConfiguration)
223-
return try NIOSSLClientHandler(
224-
context: sslContext,
225-
serverHostname: configuration.sslServerHostname)
226-
}.flatMap { sslHandler in
227-
channel.pipeline.addHandler(sslHandler, position: .before(decoder))
228-
}
220+
configureSSLCallback = { channel in
221+
channel.eventLoop.assertInEventLoop()
222+
223+
let sslContext = try NIOSSLContext(configuration: tlsConfiguration)
224+
let sslHandler = try NIOSSLClientHandler(
225+
context: sslContext,
226+
serverHostname: configuration.sslServerHostname)
227+
try channel.pipeline.syncOperations.addHandler(sslHandler, position: .before(decoder))
229228
}
230229
}
231230

@@ -235,7 +234,7 @@ final class PSQLConnection {
235234
PSQLChannelHandler(
236235
authentification: configuration.authentication,
237236
logger: logger,
238-
enableSSLCallback: enableSSLCallback),
237+
configureSSLCallback: configureSSLCallback),
239238
PSQLEventsHandler(logger: logger)
240239
])
241240
}

Tests/PostgresNIOTests/New/PSQLChannelHandlerTests.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import XCTest
22
import NIO
3+
import NIOTLS
34
@testable import PostgresNIO
45

56
class PSQLChannelHandlerTests: XCTestCase {
@@ -8,7 +9,7 @@ class PSQLChannelHandlerTests: XCTestCase {
89

910
func testHandlerAddedWithoutSSL() {
1011
let config = self.testConnectionConfiguration()
11-
let handler = PSQLChannelHandler(authentification: config.authentication)
12+
let handler = PSQLChannelHandler(authentification: config.authentication, configureSSLCallback: nil)
1213
let embedded = EmbeddedChannel(handler: handler)
1314
defer { XCTAssertNoThrow(try embedded.finish()) }
1415

@@ -35,7 +36,6 @@ class PSQLChannelHandlerTests: XCTestCase {
3536
var addSSLCallbackIsHit = false
3637
let handler = PSQLChannelHandler(authentification: config.authentication) { channel in
3738
addSSLCallbackIsHit = true
38-
return channel.eventLoop.makeSucceededFuture(())
3939
}
4040
let embedded = EmbeddedChannel(handler: handler)
4141

@@ -48,14 +48,24 @@ class PSQLChannelHandlerTests: XCTestCase {
4848

4949
XCTAssertEqual(request.code, 80877103)
5050

51-
// first we need to add an encoder, because NIOSSLHandler can only
52-
// operate on ByteBuffer
53-
let future = embedded.pipeline.addHandlers(MessageToByteHandler(PSQLFrontendMessage.Encoder.forTests), position: .first)
54-
XCTAssertNoThrow(try future.wait())
5551
XCTAssertNoThrow(try embedded.writeInbound(PSQLBackendMessage.sslSupported))
5652

5753
// a NIOSSLHandler has been added, after it SSL had been negotiated
5854
XCTAssertTrue(addSSLCallbackIsHit)
55+
56+
// signal that the ssl connection has been established
57+
embedded.pipeline.fireUserInboundEventTriggered(TLSUserEvent.handshakeCompleted(negotiatedProtocol: ""))
58+
59+
// startup message should be issued
60+
var maybeStartupMessage: PSQLFrontendMessage?
61+
XCTAssertNoThrow(maybeStartupMessage = try embedded.readOutbound(as: PSQLFrontendMessage.self))
62+
guard case .startup(let startupMessage) = maybeStartupMessage else {
63+
return XCTFail("Unexpected message")
64+
}
65+
66+
XCTAssertEqual(startupMessage.parameters.user, config.authentication?.username)
67+
XCTAssertEqual(startupMessage.parameters.database, config.authentication?.database)
68+
XCTAssertEqual(startupMessage.parameters.replication, .false)
5969
}
6070

6171
func testSSLUnsupportedClosesConnection() {
@@ -64,7 +74,7 @@ class PSQLChannelHandlerTests: XCTestCase {
6474

6575
let handler = PSQLChannelHandler(authentification: config.authentication) { channel in
6676
XCTFail("This callback should never be exectuded")
67-
return channel.eventLoop.makeFailedFuture(PSQLError.sslUnsupported)
77+
throw PSQLError.sslUnsupported
6878
}
6979
let embedded = EmbeddedChannel(handler: handler)
7080
let eventHandler = TestEventHandler()
@@ -94,7 +104,7 @@ class PSQLChannelHandlerTests: XCTestCase {
94104
database: config.authentication?.database
95105
)
96106
let state = ConnectionStateMachine(.waitingToStartAuthentication)
97-
let handler = PSQLChannelHandler(authentification: config.authentication, state: state)
107+
let handler = PSQLChannelHandler(authentification: config.authentication, state: state, configureSSLCallback: nil)
98108
let embedded = EmbeddedChannel(handler: handler)
99109

100110
embedded.triggerUserOutboundEvent(PSQLOutgoingEvent.authenticate(authContext), promise: nil)
@@ -119,7 +129,7 @@ class PSQLChannelHandlerTests: XCTestCase {
119129
database: config.authentication?.database
120130
)
121131
let state = ConnectionStateMachine(.waitingToStartAuthentication)
122-
let handler = PSQLChannelHandler(authentification: config.authentication, state: state)
132+
let handler = PSQLChannelHandler(authentification: config.authentication, state: state, configureSSLCallback: nil)
123133
let embedded = EmbeddedChannel(handler: handler)
124134

125135
embedded.triggerUserOutboundEvent(PSQLOutgoingEvent.authenticate(authContext), promise: nil)

0 commit comments

Comments
 (0)