From 77dc77b9b5cdddfb3c385c5ee6cb74153d284967 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 24 May 2018 12:53:51 +0100 Subject: [PATCH] Don't provide promises when we don't care about results. (#437) 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. --- Sources/NIO/Bootstrap.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Sources/NIO/Bootstrap.swift b/Sources/NIO/Bootstrap.swift index dccd87e51d..6fb0123ad4 100644 --- a/Sources/NIO/Bootstrap.swift +++ b/Sources/NIO/Bootstrap.swift @@ -262,9 +262,7 @@ public final class ServerBootstrap { future.then { (_) -> EventLoopFuture 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: ()) @@ -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 { @@ -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 {