From ac52960abda0c8c06183fe1e61cc712fbc1df2c1 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Wed, 20 Apr 2022 22:14:03 +0200 Subject: [PATCH] Initialize LambdaRuntime with concrete HandlerType + Docu fixes (#260) --- Sources/AWSLambdaRuntimeCore/LambdaContext.swift | 2 +- Sources/AWSLambdaRuntimeCore/LambdaHandler.swift | 9 +++++---- Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift | 9 ++++----- Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift index 60727f65..ae4e16e7 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift @@ -43,7 +43,7 @@ public struct LambdaInitializationContext: _AWSLambdaSendable { /// `ByteBufferAllocator` to allocate `ByteBuffer` public let allocator: ByteBufferAllocator - /// `Terminator` to register shutdown operations + /// ``LambdaTerminator`` to register shutdown operations public let terminator: LambdaTerminator init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, terminator: LambdaTerminator) { diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index 98a8a496..48bdb9bf 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -119,7 +119,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { /// The `EventLoopFuture` should be completed with either a response of type `Output` or an `Error` func handle(_ event: Event, context: LambdaContext) -> EventLoopFuture - /// Encode a response of type `Output` to `ByteBuffer` + /// Encode a response of type ``Output`` to `ByteBuffer` /// Concrete Lambda handlers implement this method to provide coding functionality. /// - parameters: /// - allocator: A `ByteBufferAllocator` to help allocate the `ByteBuffer`. @@ -128,7 +128,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { /// - Returns: A `ByteBuffer` with the encoded version of the `value`. func encode(allocator: ByteBufferAllocator, value: Output) throws -> ByteBuffer? - /// Decode a`ByteBuffer` to a request or event of type `Event` + /// Decode a `ByteBuffer` to a request or event of type ``Event`` /// Concrete Lambda handlers implement this method to provide coding functionality. /// /// - parameters: @@ -139,7 +139,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { } extension EventLoopLambdaHandler { - /// Driver for `ByteBuffer` -> `Event` decoding and `Output` -> `ByteBuffer` encoding + /// Driver for `ByteBuffer` -> ``Event`` decoding and ``Output`` -> `ByteBuffer` encoding @inlinable public func handle(_ event: ByteBuffer, context: LambdaContext) -> EventLoopFuture { let input: Event @@ -169,7 +169,8 @@ extension EventLoopLambdaHandler where Output == Void { // MARK: - ByteBufferLambdaHandler -/// An `EventLoopFuture` based processing protocol for a Lambda that takes a `ByteBuffer` and returns a `ByteBuffer?` asynchronously. +/// An `EventLoopFuture` based processing protocol for a Lambda that takes a `ByteBuffer` and returns +/// an optional `ByteBuffer` asynchronously. /// /// - note: This is a low level protocol designed to power the higher level ``EventLoopLambdaHandler`` and /// ``LambdaHandler`` based APIs. diff --git a/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift b/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift index 3827039f..ce019532 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift @@ -18,7 +18,7 @@ import NIOCore /// `LambdaRuntime` manages the Lambda process lifecycle. /// -/// - note: It is intended to be used within a single `EventLoop`. For this reason this class is not thread safe. +/// 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 { private let eventLoop: EventLoop private let shutdownPromise: EventLoopPromise @@ -35,9 +35,10 @@ public final class LambdaRuntime { /// 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(eventLoop: EventLoop, logger: Logger) { + public convenience init(_ handlerType: Handler.Type, eventLoop: EventLoop, logger: Logger) { self.init(eventLoop: eventLoop, logger: logger, configuration: .init()) } @@ -114,8 +115,7 @@ public final class LambdaRuntime { // MARK: - Private - #if DEBUG - /// Begin the `LambdaRuntime` shutdown. Only needed for debugging purposes, hence behind a `DEBUG` flag. + /// Begin the `LambdaRuntime` shutdown. public func shutdown() { // make this method thread safe by dispatching onto the eventloop self.eventLoop.execute { @@ -126,7 +126,6 @@ public final class LambdaRuntime { } } } - #endif private func markShutdown() { self.state = .shutdown diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift index 11aac73e..64bc4384 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift @@ -29,7 +29,7 @@ class LambdaRuntimeTest: XCTestCase { let eventLoop = eventLoopGroup.next() let logger = Logger(label: "TestLogger") - let runtime = LambdaRuntime(eventLoop: eventLoop, logger: logger) + let runtime = LambdaRuntime(StartupErrorHandler.self, eventLoop: eventLoop, logger: logger) // eventLoop.submit in this case returns an EventLoopFuture> // which is why we need `wait().wait()` @@ -51,7 +51,7 @@ class LambdaRuntimeTest: XCTestCase { let eventLoop = eventLoopGroup.next() let logger = Logger(label: "TestLogger") - let runtime = LambdaRuntime(eventLoop: eventLoop, logger: logger) + let runtime = LambdaRuntime(EchoHandler.self, eventLoop: eventLoop, logger: logger) XCTAssertNoThrow(_ = try eventLoop.flatSubmit { runtime.start() }.wait()) XCTAssertThrowsError(_ = try runtime.shutdownFuture.wait()) { @@ -101,7 +101,7 @@ class LambdaRuntimeTest: XCTestCase { let eventLoop = eventLoopGroup.next() let logger = Logger(label: "TestLogger") - let runtime = LambdaRuntime(eventLoop: eventLoop, logger: logger) + let runtime = LambdaRuntime(ShutdownErrorHandler.self, eventLoop: eventLoop, logger: logger) XCTAssertNoThrow(try eventLoop.flatSubmit { runtime.start() }.wait()) XCTAssertThrowsError(try runtime.shutdownFuture.wait()) { error in