Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute tasks on the event loop - refactor #83

Merged
merged 1 commit into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pushkarnk should we crash here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should crash at these places. We must simply Log.error() and return. We need to do all of that in a separate PR though.

}
}
}
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