From 9a1a47906c71d168f105f23cf85b6b77016fcb77 Mon Sep 17 00:00:00 2001 From: Nethra Ravindran Date: Thu, 30 Aug 2018 06:32:38 -0700 Subject: [PATCH] Execute tasks on event loop --- Sources/KituraNet/ClientRequest.swift | 23 ++++---- Sources/KituraNet/HTTP/HTTPServer.swift | 2 +- .../KituraNet/HTTP/HTTPServerResponse.swift | 53 ++++++++++--------- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/Sources/KituraNet/ClientRequest.swift b/Sources/KituraNet/ClientRequest.swift index 75e95d0a..25f72ff6 100644 --- a/Sources/KituraNet/ClientRequest.swift +++ b/Sources/KituraNet/ClientRequest.swift @@ -404,23 +404,26 @@ public class ClientRequest { // Make the HTTP request, the response callbacks will be received on the HTTPClientHandler. // We are mostly not running on the event loop. Let's make sure we send the request over the event loop. - if channel.eventLoop.inEventLoop { + execute(on: channel.eventLoop) { self.sendRequest(request: request, on: self.channel) - } else { - channel.eventLoop.execute { - self.sendRequest(request: request, on: self.channel) - } } waitSemaphore.wait() // We are now free to close the connection if asked for. if closeConnection { - if channel.eventLoop.inEventLoop { + execute(on: channel.eventLoop) { self.channel.close(promise: nil) - } else { - channel.eventLoop.execute { - self.channel.close(promise: nil) - } + } + } + } + + /// Executes task on event loop + private func execute(on eventLoop: EventLoop, _ task: @escaping () -> Void) { + if eventLoop.inEventLoop { + task() + } else { + eventLoop.execute { + task() } } } diff --git a/Sources/KituraNet/HTTP/HTTPServer.swift b/Sources/KituraNet/HTTP/HTTPServer.swift index ecc93a72..1b94bf62 100644 --- a/Sources/KituraNet/HTTP/HTTPServer.swift +++ b/Sources/KituraNet/HTTP/HTTPServer.swift @@ -157,7 +157,7 @@ public class HTTPServer : Server { } catch let error { self.state = .failed self.lifecycleListener.performFailCallbacks(with: error) - Log.error("Error trying to bing to \(port): \(error)") + Log.error("Error trying to bind to \(port): \(error)") throw error } diff --git a/Sources/KituraNet/HTTP/HTTPServerResponse.swift b/Sources/KituraNet/HTTP/HTTPServerResponse.swift index 37f74b00..8ad2d739 100644 --- a/Sources/KituraNet/HTTP/HTTPServerResponse.swift +++ b/Sources/KituraNet/HTTP/HTTPServerResponse.swift @@ -70,15 +70,11 @@ public class HTTPServerResponse: ServerResponse { guard let channel = channel else { fatalError("No channel available to write.") } + if buffer == nil { - if channel.eventLoop.inEventLoop { + execute(on: channel.eventLoop) { self.buffer = channel.allocator.buffer(capacity: string.utf8.count) self.buffer!.write(string: string) - } else { - channel.eventLoop.execute { - self.buffer = channel.allocator.buffer(capacity: string.utf8.count) - self.buffer!.write(string: string) - } } } } @@ -90,15 +86,22 @@ public class HTTPServerResponse: ServerResponse { guard let channel = channel else { fatalError("No channel available to write.") } + if buffer == nil { - if channel.eventLoop.inEventLoop { + execute(on: channel.eventLoop) { self.buffer = channel.allocator.buffer(capacity: data.count) self.buffer!.write(bytes: data) - } else { - channel.eventLoop.execute { - self.buffer = channel.allocator.buffer(capacity: data.count) - self.buffer!.write(bytes: data) - } + } + } + } + + /// Executes task on event loop + private func execute(on eventLoop: EventLoop, _ task: @escaping () -> Void) { + if eventLoop.inEventLoop { + task() + } else { + eventLoop.execute { + task() } } } @@ -115,13 +118,14 @@ public class HTTPServerResponse: ServerResponse { /// public func end() throws { guard let channel = self.channel else { - fatalError("No channel handler context available.") + fatalError("No channel available.") } - if channel.eventLoop.inEventLoop { - try end0(channel: channel) - } else { - channel.eventLoop.execute { - try! self.end0(channel: channel) + + execute(on: channel.eventLoop) { + do { + try self.end0(channel: channel) + } catch let error { + fatalError("Error: \(error)") } } } @@ -156,13 +160,14 @@ public class HTTPServerResponse: ServerResponse { /// End sending the response on an HTTP error private func end(with errorCode: HTTPStatusCode, withBody: Bool = false) throws { guard let channel = self.channel else { - fatalError("No channel handler context available.") + fatalError("No channel available.") } - if channel.eventLoop.inEventLoop { - try end0(with: errorCode, channel: channel, withBody: withBody) - } else { - channel.eventLoop.execute { - try! self.end0(with: errorCode, channel: channel, withBody: withBody) + + execute(on: channel.eventLoop) { + do { + try self.end0(with: errorCode, channel: channel, withBody: withBody) + } catch let error { + fatalError("Error: \(error)") } } }