Skip to content

Don’t create an EventLoopFuture chain #4

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

Merged
merged 2 commits into from
Mar 6, 2020
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
46 changes: 30 additions & 16 deletions Sources/SwiftAwsLambda/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public enum Lambda {
let runner = LambdaRunner(eventLoop: self.eventLoop, configuration: self.configuration, lambdaHandler: self.handler)
return runner.initialize(logger: logger).flatMap { _ in
self.state = .active
return self.run(logger: logger, runner: runner, count: 0)
return self.run(runner: runner)
}
}

Expand All @@ -133,23 +133,37 @@ public enum Lambda {
self.state = .shutdown
}

private func run(logger: Logger, runner: LambdaRunner, count: Int) -> EventLoopFuture<Int> {
switch self.state {
case .active:
if self.configuration.lifecycle.maxTimes > 0, count >= self.configuration.lifecycle.maxTimes {
return self.eventLoop.makeSucceededFuture(count)
@inline(__always)
private func run(runner: LambdaRunner) -> EventLoopFuture<Int> {
let promise = self.eventLoop.makePromise(of: Int.self)

func _run(_ count: Int) {
switch self.state {
case .active:
if self.configuration.lifecycle.maxTimes > 0, count >= self.configuration.lifecycle.maxTimes {
return promise.succeed(count)
}
var logger = self.logger
logger[metadataKey: "lifecycleIteration"] = "\(count)"
runner.run(logger: logger).whenComplete { result in
switch result {
case .success:
// recursive! per aws lambda runtime spec the polling requests are to be done one at a time
_run(count + 1)
case .failure(let error):
promise.fail(error)
}
}
case .stopping, .shutdown:
promise.succeed(count)
default:
preconditionFailure("invalid run state: \(self.state)")
}
var logger = logger
logger[metadataKey: "lifecycleIteration"] = "\(count)"
return runner.run(logger: logger).flatMap { _ in
// recursive! per aws lambda runtime spec the polling requests are to be done one at a time
self.run(logger: logger, runner: runner, count: count + 1)
}
case .stopping, .shutdown:
return self.eventLoop.makeSucceededFuture(count)
default:
preconditionFailure("invalid run state: \(self.state)")
}

_run(0)

return promise.futureResult
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftAwsLambdaTests/LambdaTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class LambdaTest: XCTestCase {
}
}
let signal = Signal.ALRM
let maxTimes = 50
let maxTimes = 1000
let configuration = Lambda.Configuration(lifecycle: .init(maxTimes: maxTimes, stopSignal: signal))
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
let future = Lambda.runAsync(eventLoopGroup: eventLoopGroup, handler: MyHandler(), configuration: configuration)
Expand Down