Skip to content

Commit

Permalink
Merge pull request #83 from nethraravindran/runOnEventLoop
Browse files Browse the repository at this point in the history
Execute tasks on the event loop - refactor
  • Loading branch information
Pushkar N Kulkarni authored Aug 30, 2018
2 parents 3303ba2 + 9a1a479 commit bcc5bc9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
23 changes: 13 additions & 10 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
53 changes: 29 additions & 24 deletions Sources/KituraNet/HTTP/HTTPServerResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand All @@ -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()
}
}
}
Expand All @@ -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)")
}
}
}
Expand Down Expand Up @@ -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)")
}
}
}
Expand Down

0 comments on commit bcc5bc9

Please sign in to comment.