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

Add inlinable where potentially usefull #511

Merged
merged 3 commits into from
Mar 21, 2025
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
9 changes: 8 additions & 1 deletion Sources/AWSLambdaRuntime/ControlPlaneRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ enum ControlPlaneResponse: Hashable {
case error(ErrorResponse)
}

package struct InvocationMetadata: Hashable {
@usableFromInline
package struct InvocationMetadata: Hashable, Sendable {
@usableFromInline
package let requestID: String
@usableFromInline
package let deadlineInMillisSinceEpoch: Int64
@usableFromInline
package let invokedFunctionARN: String
@usableFromInline
package let traceID: String
@usableFromInline
package let clientContext: String?
@usableFromInline
package let cognitoIdentity: String?

package init(headers: HTTPHeaders) throws(LambdaRuntimeError) {
Expand Down
1 change: 1 addition & 0 deletions Sources/AWSLambdaRuntime/Lambda+LocalServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extension Lambda {
/// - body: Code to run within the context of the mock server. Typically this would be a Lambda.run function call.
///
/// - note: This API is designed strictly for local testing and is behind a DEBUG flag
@usableFromInline
static func withLocalServer(
invocationEndpoint: String? = nil,
_ body: sending @escaping () async throws -> Void
Expand Down
1 change: 1 addition & 0 deletions Sources/AWSLambdaRuntime/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import ucrt
#endif

public enum Lambda {
@inlinable
package static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
runtimeClient: RuntimeClient,
handler: Handler,
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntime/LambdaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
self.storage.logger
}

init(
public init(
requestID: String,
traceID: String,
invokedFunctionARN: String,
Expand Down
4 changes: 4 additions & 0 deletions Sources/AWSLambdaRuntime/LambdaRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ import Foundation
// sadly crashes the compiler today.
public final class LambdaRuntime<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
@usableFromInline
let handlerMutex: NIOLockedValueBox<Handler?>
@usableFromInline
let logger: Logger
@usableFromInline
let eventLoop: EventLoop

public init(
Expand All @@ -48,6 +51,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
self.logger.debug("LambdaRuntime initialized")
}

@inlinable
public func run() async throws {
let handler = self.handlerMutex.withLockedValue { handler in
let result = handler
Expand Down
26 changes: 22 additions & 4 deletions Sources/AWSLambdaRuntime/LambdaRuntimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,47 @@ import NIOCore
import NIOHTTP1
import NIOPosix

@usableFromInline
final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
@usableFromInline
nonisolated let unownedExecutor: UnownedSerialExecutor

struct Configuration {
@usableFromInline
struct Configuration: Sendable {
var ip: String
var port: Int

@usableFromInline
init(ip: String, port: Int) {
self.ip = ip
self.port = port
}
}

struct Writer: LambdaRuntimeClientResponseStreamWriter {
@usableFromInline
struct Writer: LambdaRuntimeClientResponseStreamWriter, Sendable {
private var runtimeClient: LambdaRuntimeClient

fileprivate init(runtimeClient: LambdaRuntimeClient) {
self.runtimeClient = runtimeClient
}

@usableFromInline
func write(_ buffer: NIOCore.ByteBuffer) async throws {
try await self.runtimeClient.write(buffer)
}

@usableFromInline
func finish() async throws {
try await self.runtimeClient.writeAndFinish(nil)
}

@usableFromInline
func writeAndFinish(_ buffer: NIOCore.ByteBuffer) async throws {
try await self.runtimeClient.writeAndFinish(buffer)
}

@usableFromInline
func reportError(_ error: any Error) async throws {
try await self.runtimeClient.reportError(error)
}
Expand Down Expand Up @@ -90,6 +104,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
// being fully closed before we can return from it.
private var closingConnections: [any Channel] = []

@inlinable
static func withRuntimeClient<Result>(
configuration: Configuration,
eventLoop: any EventLoop,
Expand All @@ -110,14 +125,16 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
return try result.get()
}

private init(configuration: Configuration, eventLoop: any EventLoop, logger: Logger) {
@usableFromInline
init(configuration: Configuration, eventLoop: any EventLoop, logger: Logger) {
self.unownedExecutor = eventLoop.executor.asUnownedSerialExecutor()
self.configuration = configuration
self.eventLoop = eventLoop
self.logger = logger
}

private func close() async {
@usableFromInline
func close() async {
self.logger.trace("Close lambda runtime client")

guard case .notClosing = self.closingState else {
Expand All @@ -144,6 +161,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
}
}

@usableFromInline
func nextInvocation() async throws -> (Invocation, Writer) {
try await withTaskCancellationHandler {
switch self.lambdaState {
Expand Down
7 changes: 6 additions & 1 deletion Sources/AWSLambdaRuntime/LambdaRuntimeClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@

import NIOCore

@usableFromInline
package protocol LambdaRuntimeClientResponseStreamWriter: LambdaResponseStreamWriter {
func write(_ buffer: ByteBuffer) async throws
func finish() async throws
func writeAndFinish(_ buffer: ByteBuffer) async throws
func reportError(_ error: any Error) async throws
}

@usableFromInline
package protocol LambdaRuntimeClientProtocol {
associatedtype Writer: LambdaRuntimeClientResponseStreamWriter

func nextInvocation() async throws -> (Invocation, Writer)
}

package struct Invocation {
@usableFromInline
package struct Invocation: Sendable {
@usableFromInline
package var metadata: InvocationMetadata
@usableFromInline
package var event: ByteBuffer

package init(metadata: InvocationMetadata, event: ByteBuffer) {
Expand Down
7 changes: 6 additions & 1 deletion Sources/AWSLambdaRuntime/LambdaRuntimeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
//
//===----------------------------------------------------------------------===//

@usableFromInline
package struct LambdaRuntimeError: Error {
package enum Code {
@usableFromInline
package enum Code: Sendable {
case closingRuntimeClient

case connectionToControlPlaneLost
Expand All @@ -34,12 +36,15 @@ package struct LambdaRuntimeError: Error {
case invalidPort
}

@usableFromInline
package init(code: Code, underlying: (any Error)? = nil) {
self.code = code
self.underlying = underlying
}

@usableFromInline
package var code: Code
@usableFromInline
package var underlying: (any Error)?

}
1 change: 1 addition & 0 deletions Sources/AWSLambdaRuntime/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ enum Signal: Int32 {
}

extension DispatchWallTime {
@usableFromInline
init(millisSinceEpoch: Int64) {
let nanoSinceEpoch = UInt64(millisSinceEpoch) * 1_000_000
let seconds = UInt64(nanoSinceEpoch / 1_000_000_000)
Expand Down
Loading