-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
227 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import Logging | ||
import NIOCore | ||
import NIOConcurrencyHelpers | ||
|
||
// We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today. | ||
// We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this | ||
// sadly crashes the compiler today. | ||
package final class NewLambdaRuntime<Handler>: @unchecked Sendable where Handler: StreamingLambdaHandler { | ||
// TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore | ||
let handlerMutex: NIOLockedValueBox<Optional<Handler>> | ||
let logger: Logger | ||
let eventLoop: EventLoop | ||
|
||
package init( | ||
handler: sending Handler, | ||
eventLoop: EventLoop = Lambda.defaultEventLoop, | ||
logger: Logger = Logger(label: "LambdaRuntime") | ||
) { | ||
self.handlerMutex = NIOLockedValueBox(handler) | ||
self.eventLoop = eventLoop | ||
self.logger = logger | ||
} | ||
|
||
package func run() async throws { | ||
guard let runtimeEndpoint = Lambda.env("AWS_LAMBDA_RUNTIME_API") else { | ||
throw NewLambdaRuntimeError(code: .missingLambdaRuntimeAPIEnvironmentVariable) | ||
} | ||
|
||
let ipAndPort = runtimeEndpoint.split(separator: ":", maxSplits: 1) | ||
let ip = String(ipAndPort[0]) | ||
guard let port = Int(ipAndPort[1]) else { throw NewLambdaRuntimeError(code: .invalidPort) } | ||
|
||
let handler = self.handlerMutex.withLockedValue { handler in | ||
let result = handler | ||
handler = nil | ||
return result | ||
} | ||
|
||
guard let handler else { | ||
throw NewLambdaRuntimeError(code: .runtimeCanOnlyBeStartedOnce) | ||
} | ||
|
||
try await NewLambdaRuntimeClient.withRuntimeClient( | ||
configuration: .init(ip: ip, port: port), | ||
eventLoop: self.eventLoop, | ||
logger: self.logger | ||
) { runtimeClient in | ||
try await Lambda.runLoop( | ||
runtimeClient: runtimeClient, | ||
handler: handler, | ||
logger: self.logger | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters