Skip to content

Commit 2abd9b5

Browse files
authored
Remove "New" prefix from v2 additions (#357)
1 parent 85b938e commit 2abd9b5

14 files changed

+123
-141
lines changed

Diff for: Sources/AWSLambdaRuntime/Context+Foundation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import AWSLambdaRuntimeCore
1616

1717
import struct Foundation.Date
1818

19-
extension NewLambdaContext {
19+
extension LambdaContext {
2020
var deadlineDate: Date {
2121
let secondsSinceEpoch = Double(Int64(bitPattern: self.deadline.rawValue)) / -1_000_000_000
2222
return Date(timeIntervalSince1970: secondsSinceEpoch)

Diff for: Sources/AWSLambdaRuntime/Lambda+Codable.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ extension LambdaCodableAdapter {
6666
}
6767
}
6868

69-
extension NewLambdaRuntime {
70-
/// Initialize an instance with a ``NewLambdaHandler`` defined in the form of a closure **with a non-`Void` return type**.
69+
extension LambdaRuntime {
70+
/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a non-`Void` return type**.
7171
/// - Parameter body: The handler in the form of a closure.
7272
/// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``. ``JSONEncoder()`` used as default.
7373
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type. ``JSONDecoder()`` used as default.
7474
package convenience init<Event: Decodable, Output>(
75-
body: @escaping (Event, NewLambdaContext) async throws -> Output,
75+
body: @escaping (Event, LambdaContext) async throws -> Output,
7676
encoder: JSONEncoder = JSONEncoder(),
7777
decoder: JSONDecoder = JSONDecoder()
7878
)
@@ -94,11 +94,11 @@ extension NewLambdaRuntime {
9494
self.init(handler: handler)
9595
}
9696

97-
/// Initialize an instance with a ``NewLambdaHandler`` defined in the form of a closure **with a `Void` return type**.
97+
/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a `Void` return type**.
9898
/// - Parameter body: The handler in the form of a closure.
9999
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type. ``JSONDecoder()`` used as default.
100100
package convenience init<Event: Decodable>(
101-
body: @escaping (Event, NewLambdaContext) async throws -> Void,
101+
body: @escaping (Event, LambdaContext) async throws -> Void,
102102
decoder: JSONDecoder = JSONDecoder()
103103
)
104104
where

Diff for: Sources/AWSLambdaRuntimeCore/ControlPlaneRequest.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ package struct InvocationMetadata: Hashable {
3636
package let clientContext: String?
3737
package let cognitoIdentity: String?
3838

39-
package init(headers: HTTPHeaders) throws(NewLambdaRuntimeError) {
39+
package init(headers: HTTPHeaders) throws(LambdaRuntimeError) {
4040
guard let requestID = headers.first(name: AmazonHeaders.requestID), !requestID.isEmpty else {
41-
throw NewLambdaRuntimeError(code: .nextInvocationMissingHeaderRequestID)
41+
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderRequestID)
4242
}
4343

4444
guard let deadline = headers.first(name: AmazonHeaders.deadline),
4545
let unixTimeInMilliseconds = Int64(deadline)
4646
else {
47-
throw NewLambdaRuntimeError(code: .nextInvocationMissingHeaderDeadline)
47+
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderDeadline)
4848
}
4949

5050
guard let invokedFunctionARN = headers.first(name: AmazonHeaders.invokedFunctionARN) else {
51-
throw NewLambdaRuntimeError(code: .nextInvocationMissingHeaderInvokeFuctionARN)
51+
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderInvokeFuctionARN)
5252
}
5353

5454
self.requestID = requestID

Diff for: Sources/AWSLambdaRuntimeCore/Lambda.swift

+34-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import Dispatch
1516
import Logging
1617
import NIOCore
1718
import NIOPosix
@@ -28,7 +29,39 @@ import ucrt
2829
#error("Unsupported platform")
2930
#endif
3031

31-
enum Lambda {}
32+
enum Lambda {
33+
package static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
34+
runtimeClient: RuntimeClient,
35+
handler: Handler,
36+
logger: Logger
37+
) async throws where Handler: StreamingLambdaHandler {
38+
var handler = handler
39+
40+
while !Task.isCancelled {
41+
let (invocation, writer) = try await runtimeClient.nextInvocation()
42+
43+
do {
44+
try await handler.handle(
45+
invocation.event,
46+
responseWriter: writer,
47+
context: LambdaContext(
48+
requestID: invocation.metadata.requestID,
49+
traceID: invocation.metadata.traceID,
50+
invokedFunctionARN: invocation.metadata.invokedFunctionARN,
51+
deadline: DispatchWallTime(millisSinceEpoch: invocation.metadata.deadlineInMillisSinceEpoch),
52+
logger: logger
53+
)
54+
)
55+
} catch {
56+
try await writer.reportError(error)
57+
continue
58+
}
59+
}
60+
}
61+
62+
/// The default EventLoop the Lambda is scheduled on.
63+
package static var defaultEventLoop: any EventLoop = NIOSingletons.posixEventLoopGroup.next()
64+
}
3265

3366
// MARK: - Public API
3467

Diff for: Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift renamed to Sources/AWSLambdaRuntimeCore/LambdaContext.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import NIOCore
2020

2121
/// Lambda runtime context.
2222
/// The Lambda runtime generates and passes the `LambdaContext` to the Lambda handler as an argument.
23-
package struct NewLambdaContext: CustomDebugStringConvertible, Sendable {
23+
package struct LambdaContext: CustomDebugStringConvertible, Sendable {
2424
final class _Storage: Sendable {
2525
let requestID: String
2626
let traceID: String
@@ -127,8 +127,8 @@ package struct NewLambdaContext: CustomDebugStringConvertible, Sendable {
127127
invokedFunctionARN: String,
128128
timeout: DispatchTimeInterval,
129129
logger: Logger
130-
) -> NewLambdaContext {
131-
NewLambdaContext(
130+
) -> LambdaContext {
131+
LambdaContext(
132132
requestID: requestID,
133133
traceID: traceID,
134134
invokedFunctionARN: invokedFunctionARN,

Diff for: Sources/AWSLambdaRuntimeCore/NewLambdaHandlers.swift renamed to Sources/AWSLambdaRuntimeCore/LambdaHandlers.swift

+27-27
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ package protocol StreamingLambdaHandler {
2626
/// - event: The invocation's input data.
2727
/// - responseWriter: A ``LambdaResponseStreamWriter`` to write the invocation's response to.
2828
/// If no response or error is written to `responseWriter` an error will be reported to the invoker.
29-
/// - context: The ``NewLambdaContext`` containing the invocation's metadata.
29+
/// - context: The ``LambdaContext`` containing the invocation's metadata.
3030
/// - Throws:
3131
/// How the thrown error will be handled by the runtime:
3232
/// - An invocation error will be reported if the error is thrown before the first call to
@@ -39,7 +39,7 @@ package protocol StreamingLambdaHandler {
3939
mutating func handle(
4040
_ event: ByteBuffer,
4141
responseWriter: some LambdaResponseStreamWriter,
42-
context: NewLambdaContext
42+
context: LambdaContext
4343
) async throws
4444
}
4545

@@ -64,7 +64,7 @@ package protocol LambdaResponseStreamWriter {
6464
///
6565
/// - note: This handler protocol does not support response streaming because the output has to be encoded prior to it being sent, e.g. it is not possible to encode a partial/incomplete JSON string.
6666
/// This protocol also does not support the execution of background work after the response has been returned -- the ``LambdaWithBackgroundProcessingHandler`` protocol caters for such use-cases.
67-
package protocol NewLambdaHandler {
67+
package protocol LambdaHandler {
6868
/// Generic input type.
6969
/// The body of the request sent to Lambda will be decoded into this type for the handler to consume.
7070
associatedtype Event: Decodable
@@ -75,12 +75,12 @@ package protocol NewLambdaHandler {
7575
/// Implement the business logic of the Lambda function here.
7676
/// - Parameters:
7777
/// - event: The generic ``Event`` object representing the invocation's input data.
78-
/// - context: The ``NewLambdaContext`` containing the invocation's metadata.
78+
/// - context: The ``LambdaContext`` containing the invocation's metadata.
7979
/// - Returns: A generic ``Output`` object representing the computed result.
80-
func handle(_ event: Event, context: NewLambdaContext) async throws -> Output
80+
func handle(_ event: Event, context: LambdaContext) async throws -> Output
8181
}
8282

83-
/// This protocol is exactly like ``NewLambdaHandler``, with the only difference being the added support for executing background
83+
/// This protocol is exactly like ``LambdaHandler``, with the only difference being the added support for executing background
8484
/// work after the result has been sent to the AWS Lambda control plane.
8585
/// This is achieved by not having a return type in the `handle` function. The output is instead written into a
8686
/// ``LambdaResponseWriter``that is passed in as an argument, meaning that the ``handle(_:)`` function is then free to implement
@@ -98,11 +98,11 @@ package protocol LambdaWithBackgroundProcessingHandler {
9898
/// - event: The generic ``Event`` object representing the invocation's input data.
9999
/// - outputWriter: The writer to send the computed response to. A call to `outputWriter.write(_:)` will return the response to the AWS Lambda response endpoint.
100100
/// Any background work can then be executed before returning.
101-
/// - context: The ``NewLambdaContext`` containing the invocation's metadata.
101+
/// - context: The ``LambdaContext`` containing the invocation's metadata.
102102
func handle(
103103
_ event: Event,
104104
outputWriter: some LambdaResponseWriter<Output>,
105-
context: NewLambdaContext
105+
context: LambdaContext
106106
) async throws
107107
}
108108

@@ -121,67 +121,67 @@ package protocol LambdaResponseWriter<Output> {
121121
/// A ``StreamingLambdaHandler`` conforming handler object that can be constructed with a closure.
122122
/// Allows for a handler to be defined in a clean manner, leveraging Swift's trailing closure syntax.
123123
package struct StreamingClosureHandler: StreamingLambdaHandler {
124-
let body: @Sendable (ByteBuffer, LambdaResponseStreamWriter, NewLambdaContext) async throws -> Void
124+
let body: @Sendable (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void
125125

126126
/// Initialize an instance from a handler function in the form of a closure.
127127
/// - Parameter body: The handler function written as a closure.
128128
package init(
129-
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, NewLambdaContext) async throws -> Void
129+
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void
130130
) {
131131
self.body = body
132132
}
133133

134-
/// Calls the provided `self.body` closure with the ``ByteBuffer`` invocation event, the ``LambdaResponseStreamWriter``, and the ``NewLambdaContext``
134+
/// Calls the provided `self.body` closure with the ``ByteBuffer`` invocation event, the ``LambdaResponseStreamWriter``, and the ``LambdaContext``
135135
/// - Parameters:
136136
/// - event: The invocation's input data.
137137
/// - responseWriter: A ``LambdaResponseStreamWriter`` to write the invocation's response to.
138138
/// If no response or error is written to `responseWriter` an error will be reported to the invoker.
139-
/// - context: The ``NewLambdaContext`` containing the invocation's metadata.
139+
/// - context: The ``LambdaContext`` containing the invocation's metadata.
140140
package func handle(
141141
_ request: ByteBuffer,
142142
responseWriter: some LambdaResponseStreamWriter,
143-
context: NewLambdaContext
143+
context: LambdaContext
144144
) async throws {
145145
try await self.body(request, responseWriter, context)
146146
}
147147
}
148148

149-
/// A ``NewLambdaHandler`` conforming handler object that can be constructed with a closure.
149+
/// A ``LambdaHandler`` conforming handler object that can be constructed with a closure.
150150
/// Allows for a handler to be defined in a clean manner, leveraging Swift's trailing closure syntax.
151-
package struct ClosureHandler<Event: Decodable, Output>: NewLambdaHandler {
152-
let body: (Event, NewLambdaContext) async throws -> Output
151+
package struct ClosureHandler<Event: Decodable, Output>: LambdaHandler {
152+
let body: (Event, LambdaContext) async throws -> Output
153153

154154
/// Initialize with a closure handler over generic `Input` and `Output` types.
155155
/// - Parameter body: The handler function written as a closure.
156-
package init(body: @escaping (Event, NewLambdaContext) async throws -> Output) where Output: Encodable {
156+
package init(body: @escaping (Event, LambdaContext) async throws -> Output) where Output: Encodable {
157157
self.body = body
158158
}
159159

160160
/// Initialize with a closure handler over a generic `Input` type, and a `Void` `Output`.
161161
/// - Parameter body: The handler function written as a closure.
162-
package init(body: @escaping (Event, NewLambdaContext) async throws -> Void) where Output == Void {
162+
package init(body: @escaping (Event, LambdaContext) async throws -> Void) where Output == Void {
163163
self.body = body
164164
}
165165

166-
/// Calls the provided `self.body` closure with the generic ``Event`` object representing the incoming event, and the ``NewLambdaContext``
166+
/// Calls the provided `self.body` closure with the generic ``Event`` object representing the incoming event, and the ``LambdaContext``
167167
/// - Parameters:
168168
/// - event: The generic ``Event`` object representing the invocation's input data.
169-
/// - context: The ``NewLambdaContext`` containing the invocation's metadata.
170-
package func handle(_ event: Event, context: NewLambdaContext) async throws -> Output {
169+
/// - context: The ``LambdaContext`` containing the invocation's metadata.
170+
package func handle(_ event: Event, context: LambdaContext) async throws -> Output {
171171
try await self.body(event, context)
172172
}
173173
}
174174

175-
extension NewLambdaRuntime {
175+
extension LambdaRuntime {
176176
/// Initialize an instance with a ``StreamingLambdaHandler`` in the form of a closure.
177177
/// - Parameter body: The handler in the form of a closure.
178178
package convenience init(
179-
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, NewLambdaContext) async throws -> Void
179+
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void
180180
) where Handler == StreamingClosureHandler {
181181
self.init(handler: StreamingClosureHandler(body: body))
182182
}
183183

184-
/// Initialize an instance with a ``NewLambdaHandler`` defined in the form of a closure **with a non-`Void` return type**, an encoder, and a decoder.
184+
/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a non-`Void` return type**, an encoder, and a decoder.
185185
/// - Parameter body: The handler in the form of a closure.
186186
/// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``.
187187
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type.
@@ -193,7 +193,7 @@ extension NewLambdaRuntime {
193193
>(
194194
encoder: Encoder,
195195
decoder: Decoder,
196-
body: @escaping (Event, NewLambdaContext) async throws -> Output
196+
body: @escaping (Event, LambdaContext) async throws -> Output
197197
)
198198
where
199199
Handler == LambdaCodableAdapter<
@@ -213,13 +213,13 @@ extension NewLambdaRuntime {
213213
self.init(handler: handler)
214214
}
215215

216-
/// Initialize an instance with a ``NewLambdaHandler`` defined in the form of a closure **with a `Void` return type**, an encoder, and a decoder.
216+
/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a `Void` return type**, an encoder, and a decoder.
217217
/// - Parameter body: The handler in the form of a closure.
218218
/// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``.
219219
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type.
220220
package convenience init<Event: Decodable, Decoder: LambdaEventDecoder>(
221221
decoder: Decoder,
222-
body: @escaping (Event, NewLambdaContext) async throws -> Void
222+
body: @escaping (Event, LambdaContext) async throws -> Void
223223
)
224224
where
225225
Handler == LambdaCodableAdapter<

Diff for: Sources/AWSLambdaRuntimeCore/NewLambdaRuntime.swift renamed to Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import NIOConcurrencyHelpers
2020
// We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today.
2121
// We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this
2222
// sadly crashes the compiler today.
23-
package final class NewLambdaRuntime<Handler>: @unchecked Sendable where Handler: StreamingLambdaHandler {
23+
package final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: StreamingLambdaHandler {
2424
// TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore
2525
let handlerMutex: NIOLockedValueBox<Optional<Handler>>
2626
let logger: Logger
@@ -38,12 +38,12 @@ package final class NewLambdaRuntime<Handler>: @unchecked Sendable where Handler
3838

3939
package func run() async throws {
4040
guard let runtimeEndpoint = Lambda.env("AWS_LAMBDA_RUNTIME_API") else {
41-
throw NewLambdaRuntimeError(code: .missingLambdaRuntimeAPIEnvironmentVariable)
41+
throw LambdaRuntimeError(code: .missingLambdaRuntimeAPIEnvironmentVariable)
4242
}
4343

4444
let ipAndPort = runtimeEndpoint.split(separator: ":", maxSplits: 1)
4545
let ip = String(ipAndPort[0])
46-
guard let port = Int(ipAndPort[1]) else { throw NewLambdaRuntimeError(code: .invalidPort) }
46+
guard let port = Int(ipAndPort[1]) else { throw LambdaRuntimeError(code: .invalidPort) }
4747

4848
let handler = self.handlerMutex.withLockedValue { handler in
4949
let result = handler
@@ -52,10 +52,10 @@ package final class NewLambdaRuntime<Handler>: @unchecked Sendable where Handler
5252
}
5353

5454
guard let handler else {
55-
throw NewLambdaRuntimeError(code: .runtimeCanOnlyBeStartedOnce)
55+
throw LambdaRuntimeError(code: .runtimeCanOnlyBeStartedOnce)
5656
}
5757

58-
try await NewLambdaRuntimeClient.withRuntimeClient(
58+
try await LambdaRuntimeClient.withRuntimeClient(
5959
configuration: .init(ip: ip, port: port),
6060
eventLoop: self.eventLoop,
6161
logger: self.logger

0 commit comments

Comments
 (0)