Skip to content

Commit

Permalink
Don't provide promises when we don't care about results. (#437)
Browse files Browse the repository at this point in the history
Motivation:

When we don't care about the result of an operation, we shouldn't provide
a promise.

While I'm here I also removed an unnecessary close() call in the
AcceptHandler: the close() would be called again almost immediately.

Modifications:

Removed a close() call.
Set `promise: nil` on a pair of close() calls.

Result:

Fewer promise allocations.
  • Loading branch information
Lukasa authored and weissi committed May 24, 2018
1 parent d53ee6d commit 77dc77b
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions Sources/NIO/Bootstrap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ public final class ServerBootstrap {
future.then { (_) -> EventLoopFuture<Void> in
assert(ctxEventLoop.inEventLoop)
guard !ctx.pipeline.destroyed else {
return accepted.close().thenThrowing {
throw ChannelError.ioOnClosedChannel
}
return ctx.eventLoop.newFailedFuture(error: ChannelError.ioOnClosedChannel)
}
ctx.fireChannelRead(data)
return ctx.eventLoop.newSucceededFuture(result: ())
Expand All @@ -284,7 +282,7 @@ public final class ServerBootstrap {
}

private func closeAndFire(ctx: ChannelHandlerContext, accepted: SocketChannel, err: Error) {
_ = accepted.close()
accepted.close(promise: nil)
if ctx.eventLoop.inEventLoop {
ctx.fireErrorCaught(err)
} else {
Expand Down Expand Up @@ -418,7 +416,7 @@ public final class ClientBootstrap {
channel.connect(to: address, promise: connectPromise)
let cancelTask = channel.eventLoop.scheduleTask(in: self.connectTimeout) {
connectPromise.fail(error: ChannelError.connectTimeout(self.connectTimeout))
_ = channel.close()
channel.close(promise: nil)
}

connectPromise.futureResult.whenComplete {
Expand Down

0 comments on commit 77dc77b

Please sign in to comment.