Skip to content

Commit 6a8b2e9

Browse files
committed
nio 2 updates
1 parent 12a09ed commit 6a8b2e9

File tree

8 files changed

+17
-19
lines changed

8 files changed

+17
-19
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
.executable(name: "NIOPostgresBenchmark", targets: ["NIOPostgresBenchmark"]),
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),
11+
.package(url: "https://github.com/apple/swift-nio.git", .branch("master")),
1212
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "1.0.0"),
1313
],
1414
targets: [

Sources/NIOPostgres/Connection/PostgresConnection+Connect.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extension PostgresConnection {
88
return bootstrap.connect(to: socketAddress).then { channel in
99
let handler = PostgresConnection.InboundHandler(channel)
1010
return channel.pipeline.addHandlers([
11-
PostgresMessage.InboundHandler(),
11+
ByteToMessageHandler(PostgresMessage.InboundHandler()),
1212
PostgresMessage.OutboundHandler(),
1313
handler,
1414
], first: false).map {

Sources/NIOPostgres/Connection/PostgresConnection+InboundHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension PostgresConnection {
2121

2222
func send(_ messages: [PostgresMessage], _ callback: @escaping (PostgresMessage) throws -> Bool) -> EventLoopFuture<Void> {
2323
// print("PostgresConnection.ChannelHandler.send(\(messages))")
24-
let promise: EventLoopPromise<Void> = channel.eventLoop.newPromise()
24+
let promise: EventLoopPromise<Void> = channel.eventLoop.makePromise()
2525
waiters.append(Request(promise: promise, callback: callback))
2626
messages.forEach { channel.write(wrapOutboundOut($0)).cascadeFailure(promise: promise) }
2727
channel.flush()

Sources/NIOPostgres/Connection/PostgresConnection+Query.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension PostgresConnection {
1111
do {
1212
data = try binds.serialize(allocator: self.handler.channel.allocator)
1313
} catch {
14-
return self.eventLoop.newFailedFuture(error: error)
14+
return self.eventLoop.makeFailedFuture(error: error)
1515
}
1616
print("[NIOPostgres] \(string) \(data)")
1717
let parse = PostgresMessage.Parse(

Sources/NIOPostgres/Connection/PostgresConnection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension PostgresConnection {
4949
let handler = try! OpenSSLClientHandler(context: sslContext)
5050
return self.handler.channel.pipeline.add(handler: handler, first: true).map { true }
5151
case .unsupported:
52-
return self.eventLoop.newSucceededFuture(result: false)
52+
return self.eventLoop.makeSucceededFuture(result: false)
5353
}
5454
}
5555
}

Sources/NIOPostgres/Message/PostgresMessage+InboundHandler.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,37 @@ extension PostgresMessage {
2020
/// See `ByteToMessageDecoder`.
2121
func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState {
2222
// special check for SSL response
23+
var sslBuffer = buffer
2324
if
2425
!self.hasRequestedAuthentication,
25-
let sslResponse = buffer.getInteger(
26-
at: buffer.readerIndex, as: UInt8.self
27-
).flatMap(PostgresMessage.SSLResponse.init)
26+
let sslResponse = sslBuffer.readInteger(as: UInt8.self)
27+
.flatMap(PostgresMessage.SSLResponse.init)
2828
{
29-
buffer.moveReaderIndex(forwardBy: 1)
29+
buffer = sslBuffer
3030
ctx.fireChannelRead(wrapInboundOut(.sslResponse(sslResponse)))
3131
return .continue
3232
}
3333

34+
var peekBuffer = buffer
3435
// peek at the message identifier
3536
// the message identifier is always the first byte of a message
36-
guard let messageIdentifier = buffer.getInteger(at: buffer.readerIndex, as: UInt8.self).map(PostgresMessage.Identifier.init) else {
37+
guard let messageIdentifier = peekBuffer.readInteger(as: UInt8.self).map(PostgresMessage.Identifier.init) else {
3738
return .needMoreData
3839
}
39-
// print("PostgresMessage.ChannelDecoder.decode(\(messageIdentifier))")
40-
41-
#warning("check for TLS support")
4240

4341
// peek at the message size
4442
// the message size is always a 4 byte integer appearing immediately after the message identifier
45-
guard let messageSize = buffer.getInteger(at: buffer.readerIndex + 1, as: Int32.self).flatMap(Int.init) else {
43+
guard let messageSize = peekBuffer.readInteger(as: Int32.self).flatMap(Int.init) else {
4644
return .needMoreData
4745
}
4846

4947
// ensure message is large enough (skipping message type) or reject
50-
guard buffer.readableBytes - 1 >= messageSize else {
48+
guard peekBuffer.readableBytes >= messageSize - 4 else {
5149
return .needMoreData
5250
}
5351

54-
// skip message identifier and message size
55-
buffer.moveReaderIndex(forwardBy: 1 + 4)
52+
// there is sufficient data, use this buffer
53+
buffer = peekBuffer
5654

5755
let message: PostgresMessage
5856
switch messageIdentifier {

Tests/NIOPostgresTests/NIOPostgresTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ final class NIOPostgresTests: XCTestCase {
241241
defer { try! elg.syncShutdownGracefully() }
242242

243243
let conn = try PostgresConnection.connect(
244-
to: SocketAddress.newAddressResolving(host: "elmer.db.elephantsql.com", port: 5432),
244+
to: SocketAddress.makeAddressResolvingHost("elmer.db.elephantsql.com", port: 5432),
245245
on: elg.next()
246246
).wait()
247247
let upgraded = try conn.requestTLS(using: .forClient(certificateVerification: .none)).wait()

Tests/NIOPostgresTests/Utilities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension PostgresConnection {
1616
.map { conn }
1717
}
1818
} catch {
19-
return eventLoop.newFailedFuture(error: error)
19+
return eventLoop.makeFailedFuture(error: error)
2020
}
2121
}
2222
}

0 commit comments

Comments
 (0)