Skip to content

Commit

Permalink
introduce CoreByteBufferLambdaHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerd committed Nov 26, 2023
1 parent 6aafe79 commit 792d1c1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 62 deletions.
8 changes: 4 additions & 4 deletions Sources/AWSLambdaRuntimeCore/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public enum Lambda {
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerType: CodableSimpleLambdaHandler<Handler>.self)
Self.run(configuration: configuration, handlerProvider: CodableSimpleLambdaHandler<Handler>.makeHandler(context:))
}

/// Run a Lambda defined by implementing the ``LambdaHandler`` protocol.
Expand All @@ -54,7 +54,7 @@ public enum Lambda {
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerType: CodableLambdaHandler<Handler>.self)
Self.run(configuration: configuration, handlerProvider: CodableLambdaHandler<Handler>.makeHandler(context:))
}

/// Run a Lambda defined by implementing the ``EventLoopLambdaHandler`` protocol.
Expand All @@ -70,7 +70,7 @@ public enum Lambda {
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerType: CodableEventLoopLambdaHandler<Handler>.self)
Self.run(configuration: configuration, handlerProvider: CodableEventLoopLambdaHandler<Handler>.makeHandler(context:))
}

/// Run a Lambda defined by implementing the ``ByteBufferLambdaHandler`` protocol.
Expand Down Expand Up @@ -100,7 +100,7 @@ public enum Lambda {
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
internal static func run(
configuration: LambdaConfiguration = .init(),
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some ByteBufferLambdaHandler>
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some CoreByteBufferLambdaHandler>
) -> Result<Int, Error> {
let _run = { (configuration: LambdaConfiguration) -> Result<Int, Error> in
#if swift(<5.9)
Expand Down
17 changes: 16 additions & 1 deletion Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ extension EventLoopLambdaHandler {
/// - note: This is a low level protocol designed to power the higher level ``EventLoopLambdaHandler`` and
/// ``LambdaHandler`` based APIs.
/// Most users are not expected to use this protocol.
public protocol ByteBufferLambdaHandler {
public protocol ByteBufferLambdaHandler: CoreByteBufferLambdaHandler {
/// Create a Lambda handler for the runtime.
///
/// Use this to initialize all your resources that you want to cache between invocations. This could be database
Expand Down Expand Up @@ -433,6 +433,21 @@ extension ByteBufferLambdaHandler {
}
}

// MARK: - FIXME

public protocol CoreByteBufferLambdaHandler {
/// The Lambda handling method.
/// Concrete Lambda handlers implement this method to provide the Lambda functionality.
///
/// - parameters:
/// - context: Runtime ``LambdaContext``.
/// - event: The event or input payload encoded as `ByteBuffer`.
///
/// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine.
/// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error`.
func handle(_ buffer: ByteBuffer, context: LambdaContext) -> EventLoopFuture<ByteBuffer?>
}

// MARK: - Other

@usableFromInline
Expand Down
4 changes: 2 additions & 2 deletions Sources/AWSLambdaRuntimeCore/LambdaRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal final class LambdaRunner {
/// Run the user provided initializer. This *must* only be called once.
///
/// - Returns: An `EventLoopFuture<LambdaHandler>` fulfilled with the outcome of the initialization.
func initialize<Handler: ByteBufferLambdaHandler>(
func initialize<Handler: CoreByteBufferLambdaHandler>(
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
logger: Logger,
terminator: LambdaTerminator
Expand Down Expand Up @@ -63,7 +63,7 @@ internal final class LambdaRunner {
}
}

func run(handler: some ByteBufferLambdaHandler, logger: Logger) -> EventLoopFuture<Void> {
func run(handler: some CoreByteBufferLambdaHandler, logger: Logger) -> EventLoopFuture<Void> {
logger.debug("lambda invocation sequence starting")
// 1. request invocation from lambda runtime engine
self.isGettingNextInvocation = true
Expand Down
100 changes: 45 additions & 55 deletions Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import NIOCore
/// `LambdaRuntime` manages the Lambda process lifecycle.
///
/// Use this API, if you build a higher level web framework which shall be able to run inside the Lambda environment.
public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
public final class LambdaRuntime<Handler: CoreByteBufferLambdaHandler> {
private let eventLoop: EventLoop
private let shutdownPromise: EventLoopPromise<Int>
private let logger: Logger
Expand All @@ -34,23 +34,14 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
}
}

/// Create a new `LambdaRuntime`.
///
/// - parameters:
/// - handlerType: The ``ByteBufferLambdaHandler`` type the `LambdaRuntime` shall create and manage.
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
public convenience init(_ handlerType: Handler.Type, eventLoop: EventLoop, logger: Logger) {
self.init(handlerProvider: handlerType.makeHandler(context:), eventLoop: eventLoop, logger: logger, configuration: .init())
}

/// Create a new `LambdaRuntime`.
///
/// - parameters:
/// - handlerProvider: A provider of the ``ByteBufferLambdaHandler`` the `LambdaRuntime` will manage.
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
public convenience init(
@usableFromInline
convenience init(
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
eventLoop: EventLoop,
logger: Logger
Expand All @@ -63,6 +54,12 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
)
}

/// Create a new `LambdaRuntime`.
///
/// - parameters:
/// - handlerProvider: A provider of the ``ByteBufferLambdaHandler`` the `LambdaRuntime` will manage.
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
init(
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
eventLoop: EventLoop,
Expand Down Expand Up @@ -203,7 +200,7 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
private enum State {
case idle
case initializing
case active(LambdaRunner, any ByteBufferLambdaHandler)
case active(LambdaRunner, any CoreByteBufferLambdaHandler)
case shuttingdown
case shutdown

Expand Down Expand Up @@ -232,8 +229,16 @@ public enum LambdaRuntimeFactory {
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
@inlinable
public static func makeRuntime<H: SimpleLambdaHandler>(_ handlerType: H.Type, eventLoop: any EventLoop, logger: Logger) -> LambdaRuntime<some ByteBufferLambdaHandler> {
LambdaRuntime<CodableSimpleLambdaHandler<H>>(CodableSimpleLambdaHandler<H>.self, eventLoop: eventLoop, logger: logger)
public static func makeRuntime<Handler: SimpleLambdaHandler>(
_ handlerType: Handler.Type,
eventLoop: any EventLoop,
logger: Logger
) -> LambdaRuntime<some ByteBufferLambdaHandler> {
LambdaRuntime<CodableSimpleLambdaHandler<Handler>>(
handlerProvider: CodableSimpleLambdaHandler<Handler>.makeHandler(context:),
eventLoop: eventLoop,
logger: logger
)
}

/// Create a new `LambdaRuntime`.
Expand All @@ -243,8 +248,16 @@ public enum LambdaRuntimeFactory {
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
@inlinable
public static func makeRuntime<H: LambdaHandler>(_ handlerType: H.Type, eventLoop: any EventLoop, logger: Logger) -> LambdaRuntime<some ByteBufferLambdaHandler> {
LambdaRuntime<CodableLambdaHandler<H>>(CodableLambdaHandler<H>.self, eventLoop: eventLoop, logger: logger)
public static func makeRuntime<Handler: LambdaHandler>(
_ handlerType: Handler.Type,
eventLoop: any EventLoop,
logger: Logger
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
LambdaRuntime<CodableLambdaHandler<Handler>>(
handlerProvider: CodableLambdaHandler<Handler>.makeHandler(context:),
eventLoop: eventLoop,
logger: logger
)
}

/// Create a new `LambdaRuntime`.
Expand All @@ -254,28 +267,13 @@ public enum LambdaRuntimeFactory {
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
@inlinable
public static func makeRuntime<H: EventLoopLambdaHandler>(_ handlerType: H.Type, eventLoop: any EventLoop, logger: Logger) -> LambdaRuntime<some ByteBufferLambdaHandler> {
LambdaRuntime<CodableEventLoopLambdaHandler<H>>(CodableEventLoopLambdaHandler<H>.self, eventLoop: eventLoop, logger: logger)
}

/// Create a new `LambdaRuntime`.
///
/// - parameters:
/// - handlerProvider: A provider of the ``SimpleLambdaHandler`` the `LambdaRuntime` will manage.
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
@inlinable
public static func makeRuntime<H: SimpleLambdaHandler>(
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<H>,
public static func makeRuntime<Handler: EventLoopLambdaHandler>(
_ handlerType: Handler.Type,
eventLoop: any EventLoop,
logger: Logger
) -> LambdaRuntime<some ByteBufferLambdaHandler> {
LambdaRuntime(
handlerProvider: { context in
handlerProvider(context).map {
CodableSimpleLambdaHandler(handler: $0, allocator: context.allocator)
}
},
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
LambdaRuntime<CodableEventLoopLambdaHandler<Handler>>(
handlerProvider: CodableEventLoopLambdaHandler<Handler>.makeHandler(context:),
eventLoop: eventLoop,
logger: logger
)
Expand All @@ -284,21 +282,17 @@ public enum LambdaRuntimeFactory {
/// Create a new `LambdaRuntime`.
///
/// - parameters:
/// - handlerProvider: A provider of the ``LambdaHandler`` the `LambdaRuntime` will manage.
/// - handlerType: The ``EventLoopLambdaHandler`` type the `LambdaRuntime` shall create and manage.
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
@inlinable
public static func makeRuntime<H: LambdaHandler>(
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<H>,
public static func makeRuntime<Handler: ByteBufferLambdaHandler>(
_ handlerType: Handler.Type,
eventLoop: any EventLoop,
logger: Logger
) -> LambdaRuntime<some ByteBufferLambdaHandler> {
LambdaRuntime(
handlerProvider: { context in
handlerProvider(context).map {
CodableLambdaHandler(handler: $0, allocator: context.allocator)
}
},
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
LambdaRuntime<Handler>(
handlerProvider: Handler.makeHandler(context:),
eventLoop: eventLoop,
logger: logger
)
Expand All @@ -311,17 +305,13 @@ public enum LambdaRuntimeFactory {
/// - eventLoop: An `EventLoop` to run the Lambda on.
/// - logger: A `Logger` to log the Lambda events.
@inlinable
public static func makeRuntime<H: EventLoopLambdaHandler>(
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<H>,
public static func makeRuntime<Handler: CoreByteBufferLambdaHandler>(
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
eventLoop: any EventLoop,
logger: Logger
) -> LambdaRuntime<some ByteBufferLambdaHandler> {
) -> LambdaRuntime<Handler> {
LambdaRuntime(
handlerProvider: { context in
handlerProvider(context).map {
CodableEventLoopLambdaHandler(handler: $0, allocator: context.allocator)
}
},
handlerProvider: handlerProvider,
eventLoop: eventLoop,
logger: logger
)
Expand Down

0 comments on commit 792d1c1

Please sign in to comment.