Skip to content

Commit 792d1c1

Browse files
committed
introduce CoreByteBufferLambdaHandler
1 parent 6aafe79 commit 792d1c1

File tree

4 files changed

+67
-62
lines changed

4 files changed

+67
-62
lines changed

Sources/AWSLambdaRuntimeCore/Lambda.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public enum Lambda {
3838
configuration: LambdaConfiguration = .init(),
3939
handlerType: Handler.Type
4040
) -> Result<Int, Error> {
41-
Self.run(configuration: configuration, handlerType: CodableSimpleLambdaHandler<Handler>.self)
41+
Self.run(configuration: configuration, handlerProvider: CodableSimpleLambdaHandler<Handler>.makeHandler(context:))
4242
}
4343

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

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

7676
/// Run a Lambda defined by implementing the ``ByteBufferLambdaHandler`` protocol.
@@ -100,7 +100,7 @@ public enum Lambda {
100100
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
101101
internal static func run(
102102
configuration: LambdaConfiguration = .init(),
103-
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some ByteBufferLambdaHandler>
103+
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some CoreByteBufferLambdaHandler>
104104
) -> Result<Int, Error> {
105105
let _run = { (configuration: LambdaConfiguration) -> Result<Int, Error> in
106106
#if swift(<5.9)

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ extension EventLoopLambdaHandler {
398398
/// - note: This is a low level protocol designed to power the higher level ``EventLoopLambdaHandler`` and
399399
/// ``LambdaHandler`` based APIs.
400400
/// Most users are not expected to use this protocol.
401-
public protocol ByteBufferLambdaHandler {
401+
public protocol ByteBufferLambdaHandler: CoreByteBufferLambdaHandler {
402402
/// Create a Lambda handler for the runtime.
403403
///
404404
/// Use this to initialize all your resources that you want to cache between invocations. This could be database
@@ -433,6 +433,21 @@ extension ByteBufferLambdaHandler {
433433
}
434434
}
435435

436+
// MARK: - FIXME
437+
438+
public protocol CoreByteBufferLambdaHandler {
439+
/// The Lambda handling method.
440+
/// Concrete Lambda handlers implement this method to provide the Lambda functionality.
441+
///
442+
/// - parameters:
443+
/// - context: Runtime ``LambdaContext``.
444+
/// - event: The event or input payload encoded as `ByteBuffer`.
445+
///
446+
/// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine.
447+
/// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error`.
448+
func handle(_ buffer: ByteBuffer, context: LambdaContext) -> EventLoopFuture<ByteBuffer?>
449+
}
450+
436451
// MARK: - Other
437452

438453
@usableFromInline

Sources/AWSLambdaRuntimeCore/LambdaRunner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal final class LambdaRunner {
3333
/// Run the user provided initializer. This *must* only be called once.
3434
///
3535
/// - Returns: An `EventLoopFuture<LambdaHandler>` fulfilled with the outcome of the initialization.
36-
func initialize<Handler: ByteBufferLambdaHandler>(
36+
func initialize<Handler: CoreByteBufferLambdaHandler>(
3737
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
3838
logger: Logger,
3939
terminator: LambdaTerminator
@@ -63,7 +63,7 @@ internal final class LambdaRunner {
6363
}
6464
}
6565

66-
func run(handler: some ByteBufferLambdaHandler, logger: Logger) -> EventLoopFuture<Void> {
66+
func run(handler: some CoreByteBufferLambdaHandler, logger: Logger) -> EventLoopFuture<Void> {
6767
logger.debug("lambda invocation sequence starting")
6868
// 1. request invocation from lambda runtime engine
6969
self.isGettingNextInvocation = true

Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import NIOCore
1919
/// `LambdaRuntime` manages the Lambda process lifecycle.
2020
///
2121
/// Use this API, if you build a higher level web framework which shall be able to run inside the Lambda environment.
22-
public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
22+
public final class LambdaRuntime<Handler: CoreByteBufferLambdaHandler> {
2323
private let eventLoop: EventLoop
2424
private let shutdownPromise: EventLoopPromise<Int>
2525
private let logger: Logger
@@ -34,23 +34,14 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
3434
}
3535
}
3636

37-
/// Create a new `LambdaRuntime`.
38-
///
39-
/// - parameters:
40-
/// - handlerType: The ``ByteBufferLambdaHandler`` type the `LambdaRuntime` shall create and manage.
41-
/// - eventLoop: An `EventLoop` to run the Lambda on.
42-
/// - logger: A `Logger` to log the Lambda events.
43-
public convenience init(_ handlerType: Handler.Type, eventLoop: EventLoop, logger: Logger) {
44-
self.init(handlerProvider: handlerType.makeHandler(context:), eventLoop: eventLoop, logger: logger, configuration: .init())
45-
}
46-
4737
/// Create a new `LambdaRuntime`.
4838
///
4939
/// - parameters:
5040
/// - handlerProvider: A provider of the ``ByteBufferLambdaHandler`` the `LambdaRuntime` will manage.
5141
/// - eventLoop: An `EventLoop` to run the Lambda on.
5242
/// - logger: A `Logger` to log the Lambda events.
53-
public convenience init(
43+
@usableFromInline
44+
convenience init(
5445
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
5546
eventLoop: EventLoop,
5647
logger: Logger
@@ -63,6 +54,12 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
6354
)
6455
}
6556

57+
/// Create a new `LambdaRuntime`.
58+
///
59+
/// - parameters:
60+
/// - handlerProvider: A provider of the ``ByteBufferLambdaHandler`` the `LambdaRuntime` will manage.
61+
/// - eventLoop: An `EventLoop` to run the Lambda on.
62+
/// - logger: A `Logger` to log the Lambda events.
6663
init(
6764
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
6865
eventLoop: EventLoop,
@@ -203,7 +200,7 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
203200
private enum State {
204201
case idle
205202
case initializing
206-
case active(LambdaRunner, any ByteBufferLambdaHandler)
203+
case active(LambdaRunner, any CoreByteBufferLambdaHandler)
207204
case shuttingdown
208205
case shutdown
209206

@@ -232,8 +229,16 @@ public enum LambdaRuntimeFactory {
232229
/// - eventLoop: An `EventLoop` to run the Lambda on.
233230
/// - logger: A `Logger` to log the Lambda events.
234231
@inlinable
235-
public static func makeRuntime<H: SimpleLambdaHandler>(_ handlerType: H.Type, eventLoop: any EventLoop, logger: Logger) -> LambdaRuntime<some ByteBufferLambdaHandler> {
236-
LambdaRuntime<CodableSimpleLambdaHandler<H>>(CodableSimpleLambdaHandler<H>.self, eventLoop: eventLoop, logger: logger)
232+
public static func makeRuntime<Handler: SimpleLambdaHandler>(
233+
_ handlerType: Handler.Type,
234+
eventLoop: any EventLoop,
235+
logger: Logger
236+
) -> LambdaRuntime<some ByteBufferLambdaHandler> {
237+
LambdaRuntime<CodableSimpleLambdaHandler<Handler>>(
238+
handlerProvider: CodableSimpleLambdaHandler<Handler>.makeHandler(context:),
239+
eventLoop: eventLoop,
240+
logger: logger
241+
)
237242
}
238243

239244
/// Create a new `LambdaRuntime`.
@@ -243,8 +248,16 @@ public enum LambdaRuntimeFactory {
243248
/// - eventLoop: An `EventLoop` to run the Lambda on.
244249
/// - logger: A `Logger` to log the Lambda events.
245250
@inlinable
246-
public static func makeRuntime<H: LambdaHandler>(_ handlerType: H.Type, eventLoop: any EventLoop, logger: Logger) -> LambdaRuntime<some ByteBufferLambdaHandler> {
247-
LambdaRuntime<CodableLambdaHandler<H>>(CodableLambdaHandler<H>.self, eventLoop: eventLoop, logger: logger)
251+
public static func makeRuntime<Handler: LambdaHandler>(
252+
_ handlerType: Handler.Type,
253+
eventLoop: any EventLoop,
254+
logger: Logger
255+
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
256+
LambdaRuntime<CodableLambdaHandler<Handler>>(
257+
handlerProvider: CodableLambdaHandler<Handler>.makeHandler(context:),
258+
eventLoop: eventLoop,
259+
logger: logger
260+
)
248261
}
249262

250263
/// Create a new `LambdaRuntime`.
@@ -254,28 +267,13 @@ public enum LambdaRuntimeFactory {
254267
/// - eventLoop: An `EventLoop` to run the Lambda on.
255268
/// - logger: A `Logger` to log the Lambda events.
256269
@inlinable
257-
public static func makeRuntime<H: EventLoopLambdaHandler>(_ handlerType: H.Type, eventLoop: any EventLoop, logger: Logger) -> LambdaRuntime<some ByteBufferLambdaHandler> {
258-
LambdaRuntime<CodableEventLoopLambdaHandler<H>>(CodableEventLoopLambdaHandler<H>.self, eventLoop: eventLoop, logger: logger)
259-
}
260-
261-
/// Create a new `LambdaRuntime`.
262-
///
263-
/// - parameters:
264-
/// - handlerProvider: A provider of the ``SimpleLambdaHandler`` the `LambdaRuntime` will manage.
265-
/// - eventLoop: An `EventLoop` to run the Lambda on.
266-
/// - logger: A `Logger` to log the Lambda events.
267-
@inlinable
268-
public static func makeRuntime<H: SimpleLambdaHandler>(
269-
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<H>,
270+
public static func makeRuntime<Handler: EventLoopLambdaHandler>(
271+
_ handlerType: Handler.Type,
270272
eventLoop: any EventLoop,
271273
logger: Logger
272-
) -> LambdaRuntime<some ByteBufferLambdaHandler> {
273-
LambdaRuntime(
274-
handlerProvider: { context in
275-
handlerProvider(context).map {
276-
CodableSimpleLambdaHandler(handler: $0, allocator: context.allocator)
277-
}
278-
},
274+
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
275+
LambdaRuntime<CodableEventLoopLambdaHandler<Handler>>(
276+
handlerProvider: CodableEventLoopLambdaHandler<Handler>.makeHandler(context:),
279277
eventLoop: eventLoop,
280278
logger: logger
281279
)
@@ -284,21 +282,17 @@ public enum LambdaRuntimeFactory {
284282
/// Create a new `LambdaRuntime`.
285283
///
286284
/// - parameters:
287-
/// - handlerProvider: A provider of the ``LambdaHandler`` the `LambdaRuntime` will manage.
285+
/// - handlerType: The ``EventLoopLambdaHandler`` type the `LambdaRuntime` shall create and manage.
288286
/// - eventLoop: An `EventLoop` to run the Lambda on.
289287
/// - logger: A `Logger` to log the Lambda events.
290288
@inlinable
291-
public static func makeRuntime<H: LambdaHandler>(
292-
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<H>,
289+
public static func makeRuntime<Handler: ByteBufferLambdaHandler>(
290+
_ handlerType: Handler.Type,
293291
eventLoop: any EventLoop,
294292
logger: Logger
295-
) -> LambdaRuntime<some ByteBufferLambdaHandler> {
296-
LambdaRuntime(
297-
handlerProvider: { context in
298-
handlerProvider(context).map {
299-
CodableLambdaHandler(handler: $0, allocator: context.allocator)
300-
}
301-
},
293+
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
294+
LambdaRuntime<Handler>(
295+
handlerProvider: Handler.makeHandler(context:),
302296
eventLoop: eventLoop,
303297
logger: logger
304298
)
@@ -311,17 +305,13 @@ public enum LambdaRuntimeFactory {
311305
/// - eventLoop: An `EventLoop` to run the Lambda on.
312306
/// - logger: A `Logger` to log the Lambda events.
313307
@inlinable
314-
public static func makeRuntime<H: EventLoopLambdaHandler>(
315-
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<H>,
308+
public static func makeRuntime<Handler: CoreByteBufferLambdaHandler>(
309+
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
316310
eventLoop: any EventLoop,
317311
logger: Logger
318-
) -> LambdaRuntime<some ByteBufferLambdaHandler> {
312+
) -> LambdaRuntime<Handler> {
319313
LambdaRuntime(
320-
handlerProvider: { context in
321-
handlerProvider(context).map {
322-
CodableEventLoopLambdaHandler(handler: $0, allocator: context.allocator)
323-
}
324-
},
314+
handlerProvider: handlerProvider,
325315
eventLoop: eventLoop,
326316
logger: logger
327317
)

0 commit comments

Comments
 (0)