Skip to content

Commit 4582587

Browse files
committed
New runtime works on happy path
1 parent 7c66455 commit 4582587

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private enum LocalLambda {
131131
guard let work = request.body else {
132132
return self.writeResponse(context: context, response: .init(status: .badRequest))
133133
}
134-
let requestID = "\(DispatchTime.now().uptimeNanoseconds)" // FIXME:
134+
let requestID = LambdaRequestID().lowercased
135135
let promise = context.eventLoop.makePromise(of: Response.self)
136136
promise.futureResult.whenComplete { result in
137137
switch result {

Sources/AWSLambdaRuntimeCore/Lambda.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public enum Lambda {
8989
return _run(configuration)
9090
}
9191
#else
92-
return _run(configuration, factory)
92+
return _run(configuration)
9393
#endif
9494
}
9595
}

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,27 @@ extension ByteBufferLambdaHandler {
201201
/// The lambda runtime provides a default implementation of the method that manages the launch
202202
/// process.
203203
public static func main() {
204+
#if false
204205
_ = Lambda.run(configuration: .init(), handlerType: Self.self)
206+
#else
207+
208+
#if DEBUG
209+
if Lambda.env("LOCAL_LAMBDA_SERVER_ENABLED").flatMap(Bool.init) ?? false {
210+
do {
211+
return try Lambda.withLocalServer {
212+
NewLambdaRuntime.run(handlerType: Self.self)
213+
}
214+
} catch {
215+
print(error)
216+
exit(1)
217+
}
218+
} else {
219+
NewLambdaRuntime.run(handlerType: Self.self)
220+
}
221+
#else
222+
NewLambdaRuntime.run(handlerType: Self.self)
223+
#endif
224+
#endif
205225
}
206226
}
207227

Sources/AWSLambdaRuntimeCore/LambdaRequestID.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ struct LambdaRequestID {
6464

6565
private let _uuid: uuid_t
6666

67-
/// Returns a string representation for the `LambdaRequestID`, such as "E621E1F8-C36C-495A-93FC-0C247A3E6E5F"
68-
var uuidString: String {
69-
self.uppercased
70-
}
71-
7267
/// Returns a lowercase string representation for the `LambdaRequestID`, such as "e621e1f8-c36c-495a-93fc-0c247a3e6e5f"
7368
var lowercased: String {
7469
var bytes = self.toAsciiBytesOnStack(characters: Self.lowercaseLookup)
@@ -144,13 +139,13 @@ extension LambdaRequestID: Hashable {
144139

145140
extension LambdaRequestID: CustomStringConvertible {
146141
var description: String {
147-
self.uuidString
142+
self.lowercased
148143
}
149144
}
150145

151146
extension LambdaRequestID: CustomDebugStringConvertible {
152147
var debugDescription: String {
153-
self.uuidString
148+
self.lowercased
154149
}
155150
}
156151

@@ -170,7 +165,7 @@ extension LambdaRequestID: Decodable {
170165
extension LambdaRequestID: Encodable {
171166
func encode(to encoder: Encoder) throws {
172167
var container = encoder.singleValueContainer()
173-
try container.encode(self.uuidString)
168+
try container.encode(self.lowercased)
174169
}
175170
}
176171

@@ -340,7 +335,7 @@ extension ByteBuffer {
340335

341336
@discardableResult
342337
mutating func setRequestID(_ requestID: LambdaRequestID, at index: Int) -> Int {
343-
var localBytes = requestID.toAsciiBytesOnStack(characters: LambdaRequestID.uppercaseLookup)
338+
var localBytes = requestID.toAsciiBytesOnStack(characters: LambdaRequestID.lowercaseLookup)
344339
return withUnsafeBytes(of: &localBytes) {
345340
self.setBytes($0, at: index)
346341
}

Sources/AWSLambdaRuntimeCore/NewLambdaChannelHandler.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ final class NewLambdaChannelHandler<Delegate: LambdaChannelHandlerDelegate>: Cha
6161

6262
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
6363
do {
64-
try self.decoder.process(buffer: self.unwrapInboundIn(data)) { response in
64+
let buffer = self.unwrapInboundIn(data)
65+
try self.decoder.process(buffer: buffer) { response in
6566
guard self.requestsInFlight.popFirst() != nil else {
6667
throw LambdaRuntimeError.unsolicitedResponse
6768
}

Sources/AWSLambdaRuntimeCore/NewLambdaRuntime.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import NIOCore
1818
import NIOPosix
1919
import Backtrace
2020

21+
#if canImport(Glibc)
22+
import Glibc
23+
#endif
24+
2125
/// `LambdaRuntime` manages the Lambda process lifecycle.
2226
///
2327
/// - note: All state changes are dispatched onto the supplied EventLoop.
@@ -134,6 +138,7 @@ public final class NewLambdaRuntime<Handler: ByteBufferLambdaHandler> {
134138
}
135139

136140
case .invokeHandler(let handler, let invocation, let event):
141+
self.logger.trace("invoking handler")
137142
let context = LambdaContext(
138143
logger: self.logger,
139144
eventLoop: self.eventLoop,
@@ -149,15 +154,22 @@ public final class NewLambdaRuntime<Handler: ByteBufferLambdaHandler> {
149154
self.shutdownPromise.fail(error)
150155

151156
case .requestNextInvocation(let handler, let startPromise):
157+
self.logger.trace("requesting next invocation")
152158
handler.sendRequest(.next)
153159
startPromise?.succeed(())
154160

155161
case .reportInvocationResult(let requestID, let result, let pipelineNextInvocationRequest, let handler):
156162
switch result {
157163
case .success(let body):
164+
self.logger.trace("reporting invocation success", metadata: [
165+
"lambda-request-id": "\(requestID)"
166+
])
158167
handler.sendRequest(.invocationResponse(requestID, body))
159168

160169
case .failure(let error):
170+
self.logger.trace("reporting invocation failure", metadata: [
171+
"lambda-request-id": "\(requestID)"
172+
])
161173
let errorString = String(describing: error)
162174
let errorResponse = ErrorResponse(errorType: errorString, errorMessage: errorString)
163175
handler.sendRequest(.invocationError(requestID, errorResponse))
@@ -262,7 +274,14 @@ extension NewLambdaRuntime {
262274
logger.logLevel = configuration.general.logLevel
263275

264276
MultiThreadedEventLoopGroup.withCurrentThreadAsEventLoop { eventLoop in
265-
let runtime = NewLambdaRuntime<Handler>(eventLoop: eventLoop, logger: logger, configuration: configuration, handlerType: Handler.self)
277+
let runtime = NewLambdaRuntime(
278+
eventLoop: eventLoop,
279+
logger: logger,
280+
configuration: configuration,
281+
handlerType: Handler.self
282+
)
283+
284+
logger.info("lambda runtime starting with \(configuration)")
266285

267286
#if DEBUG
268287
let signalSource = trap(signal: configuration.lifecycle.stopSignal) { signal in

0 commit comments

Comments
 (0)