-
Notifications
You must be signed in to change notification settings - Fork 113
Use NIO in Single Threaded Mode #68
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
Conversation
a1067a6
to
a5b6dcc
Compare
lifecycle.shutdown() | ||
} | ||
|
||
_ = lifecycle.start().flatMap { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lifecycle.start()
runs in the eventLoop
now.
This means that the factory here is executed on the eventLoop
.
// for testing and internal use
@discardableResult
internal static func run(configuration: Configuration = .init(), factory: @escaping (EventLoop) throws -> Handler) -> Result<Int, Error> {
self.run(configuration: configuration, factory: { eventloop -> EventLoopFuture<Handler> in
do {
let handler = try factory(eventloop)
return eventloop.makeSucceededFuture(handler)
} catch {
return eventloop.makeFailedFuture(error)
}
})
}
We should probably offload that as well.
a5b6dcc
to
b7beddd
Compare
} catch { | ||
return eventloop.makeFailedFuture(error) | ||
let promise = eventloop.makePromise(of: Handler.self) | ||
Lambda.defaultOffloadQueue.async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addresses the problem stated here:
https://github.com/swift-server/swift-aws-lambda-runtime/pull/68/files#r419383593
I unsure though if using Lambda.defaultOffloadQueue
is a good solution to the problem.
b7beddd
to
bbd6382
Compare
ce91afb
to
380d8ce
Compare
380d8ce
to
4cc896e
Compare
let promise = eventloop.makePromise(of: Handler.self) | ||
// if we have a callback based handler factory, we offload the creation of the handler | ||
// onto the default offload queue, to ensure that the eventloop is never blocked. | ||
Lambda.defaultOffloadQueue.async { | ||
do { | ||
promise.succeed(try factory(eventloop)) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
} | ||
return promise.futureResult |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm :)
return lifecycle.start().flatMap { | ||
return lifecycle.shutdownFuture.always { _ in | ||
|
||
var r: Result<Int, Error>? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r -> result
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also maybe Result<Int, Error>!
instead of Result<Int, Error>?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have two results here, one within the function scope and one within the .always
closure.
signalSource.cancel() | ||
eventLoop.shutdownGracefully { _ in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this do in the MultiThreadedEventLoopGroup.withCurrentThreadAsEventLoop
case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomerd it makes withCurrentThreadAsEventLoop
return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabianfett actually, you gotta handle the error there :). The right way is
if let error = error {
preconditionFailure("Failed to shutdown eventloop: \(error)")
}
The reason this is a precondition is because this has to work, unless you have a programmer error (tried to shut it down twice or so).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, one naming issue and one question
@tomerd fixed. |
_ = lifecycle.start().flatMap { | ||
lifecycle.shutdownFuture | ||
} | ||
.always { lifecycleResult in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually maybe we can use whenComplete
instead of always
here so we dont need to do _ =
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh sure. fixed!
9f8f864
to
f967460
Compare
Use @weissi's awesome pr...
apple/swift-nio#1499
Remains in draft until
2.17
is released.Don't review this yet. This is just made to compile & run.