Skip to content

Commit cedb5ad

Browse files
authored
[BufferMessageEncoder] Reduce the number of force unwraps (#234)
1 parent 4cd1567 commit cedb5ad

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

Sources/PostgresNIO/New/BufferedMessageEncoder.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ struct BufferedMessageEncoder {
2828
self.encoder.encode(data: message, out: &self.buffer)
2929
}
3030

31-
mutating func flush() -> ByteBuffer? {
32-
guard self.buffer.readableBytes > 0 else {
33-
return nil
34-
}
35-
31+
mutating func flush() -> ByteBuffer {
3632
self.state = .flushed
3733
return self.buffer
3834
}

Sources/PostgresNIO/New/PSQLChannelHandler.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,18 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
229229
break
230230
case .sendStartupMessage(let authContext):
231231
self.encoder.encode(.startup(.versionThree(parameters: authContext.toStartupParameters())))
232-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
232+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
233233
case .sendSSLRequest:
234234
self.encoder.encode(.sslRequest(.init()))
235-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
235+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
236236
case .sendPasswordMessage(let mode, let authContext):
237237
self.sendPasswordMessage(mode: mode, authContext: authContext, context: context)
238238
case .sendSaslInitialResponse(let name, let initialResponse):
239239
self.encoder.encode(.saslInitialResponse(.init(saslMechanism: name, initialData: initialResponse)))
240-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
240+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
241241
case .sendSaslResponse(let bytes):
242242
self.encoder.encode(.saslResponse(.init(data: bytes)))
243-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
243+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
244244
case .closeConnectionAndCleanup(let cleanupContext):
245245
self.closeConnectionAndCleanup(cleanupContext, context: context)
246246
case .fireChannelInactive:
@@ -304,7 +304,7 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
304304
// message and immediately closes the connection. On receipt of this message, the
305305
// backend closes the connection and terminates.
306306
self.encoder.encode(.terminate)
307-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
307+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
308308
}
309309
context.close(mode: .all, promise: promise)
310310
case .succeedPreparedStatementCreation(let preparedContext, with: let rowDescription):
@@ -369,11 +369,11 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
369369
let hash = Insecure.MD5.hash(data: hash2).md5PrefixHexdigest()
370370

371371
self.encoder.encode(.password(.init(value: hash)))
372-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
372+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
373373

374374
case .cleartext:
375375
self.encoder.encode(.password(.init(value: authContext.password ?? "")))
376-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
376+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
377377
}
378378
}
379379

@@ -382,12 +382,12 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
382382
case .preparedStatement(let name):
383383
self.encoder.encode(.close(.preparedStatement(name)))
384384
self.encoder.encode(.sync)
385-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
385+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
386386

387387
case .portal(let name):
388388
self.encoder.encode(.close(.portal(name)))
389389
self.encoder.encode(.sync)
390-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
390+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
391391
}
392392
}
393393

@@ -405,7 +405,7 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
405405
self.encoder.encode(.parse(parse))
406406
self.encoder.encode(.describe(.preparedStatement(statementName)))
407407
self.encoder.encode(.sync)
408-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
408+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
409409
}
410410

411411
private func sendBindExecuteAndSyncMessage(
@@ -420,7 +420,7 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
420420
self.encoder.encode(.bind(bind))
421421
self.encoder.encode(.execute(.init(portalName: "")))
422422
self.encoder.encode(.sync)
423-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
423+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
424424
}
425425

426426
private func sendParseDescribeBindExecuteAndSyncMessage(
@@ -443,7 +443,7 @@ final class PSQLChannelHandler: ChannelDuplexHandler {
443443
self.encoder.encode(.bind(bind))
444444
self.encoder.encode(.execute(.init(portalName: "")))
445445
self.encoder.encode(.sync)
446-
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()!), promise: nil)
446+
context.writeAndFlush(self.wrapOutboundOut(self.encoder.flush()), promise: nil)
447447
}
448448

449449
private func succeedQueryWithRowStream(

0 commit comments

Comments
 (0)