Swift4Lambda v2 equivalent of init(context: LambdaInitializationContext)

So I am curious about something. I'm playing around with the new-ish v2 Swift for Lambda package and trying to adapt my v1 code for v2. In the v1 days, if I had a client (let's call it Vapor's MySQLKit client because that's what it is), I could get a reference to the Lambda event loop by implementing init(context: LambdaInitializationContext) for my LambdaHandler and then asking for context.eventLoop. The cool thing about that is that I could use that event loop to setup the MySQLKit client:

// Use the same (NIO) event loop that the AWS handler uses for MySQL
// queries ... this is okay since any MySQL protocol handling will not
// block the event loop
self.pool = EventLoopGroupConnectionPool(
	source: mySQLConnectionSource,
	on: context.eventLoop
)

I gather that in Swift4Lambda v2, there is a conscious move away from EventLoops…but it still looks like EventLoops are being used…at least for now…and certainly they're still needed to setup MySQLKit. Now I see that I could, in principle, do something like this:

let runtime = LambdaRuntime(body: handler.handle)
let eventLoop = runtime.eventLoop

…buuuuut, even though eventLoop is a property of LambdaRuntime, it's inaccessible.

Now, of course, I could just explicitly create an EventLoopGroup for MySQLKit…that should totally work…but I always thought it was kind of clever to piggyback on Lambda's event loop since it already exists. I am wondering whether this is still do-able in the v2 world.

I hope this makes sense.

Oh well, I guess this is really intentional:

From the v2 proposal doc:
LambdaContext will be largely unchanged, but the eventLoop property will be removed.

I should read more carefully. I guess this means explicitly configuring MySQLKit to use its very own EventLoopGroup if it's in a v2 Lambda function. No biggie…I think I have my answer though.

You can use the singleton ELG instead

I just used Lambda.defaultEventLoop when moving to v2, though I'm not sure what the maintainers think of using that. I noticed the underlying v2 code is using Lambda.defaultEventLoop as the default EventLoop and grabbed it from there.

1 Like

Oh that is a genius move and I can't believe I didn't think of it while looking Swift4AWSLambda source code! I definitely need more coffee. I deeply suspect, however, that yeah, that's not what is intended. I mean, I definitely "get" the push to structured concurrency…totally makes sense…and I think service lifecycle is also super cool…it's just that it's probably gonna take Vapor some time to catch up to it. I'm not sure there'll be an easy way to configure MySQLKit or PostgreSQLKit without reference to an EventLoop.