-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cloud.Api handler (AWS Lambda) - "Warm start" #5440
Comments
You can use bring cloud;
class MyType {
cache: cloud.Bucket;
new(cache: cloud.Bucket) {
this.cache = cache;
}
inflight cachedValue: str;
inflight new() {
log("load from cache");
this.cachedValue = this.cache.get("value");
}
pub inflight doStuff() {
log("hi");
log(this.cachedValue);
}
}
let b = new cloud.Bucket();
b.addObject("value", "hello");
let t = new MyType(b);
new cloud.Function(inflight () => {
for i in 0..1000 {
t.doStuff();
}
}); This will print:
Let me know if this is what you were after. |
Not really... the generated code looks like this: // cloud.function_c8d2eca1.js
exports.handler = async function(event) {
return await (await (async () => {
const $Closure1Client = require_inflight_Closure1_1()({
$t: await (async () => {
const MyTypeClient = require_inflight_MyType_1()({});
const client2 = new MyTypeClient({
$this_cache: new (require_bucket_inflight()).BucketClient(process.env["BUCKET_NAME_d755b447"])
});
if (client2.$inflight_init) {
await client2.$inflight_init();
}
return client2;
})()
});
const client = new $Closure1Client({});
if (client.$inflight_init) {
await client.$inflight_init();
}
return client;
})()).handle(event);
}; If you run the two codes in AWS: class Counter {
constructor() {
this.counter = 0;
}
inc(n) {
this.counter += n ?? 1;
}
dec(n) {
this.counter -= n ?? 1;
}
set(n) {
this.counter = n;
}
peek() {
return this.counter;
}
}
const counter = new Counter();
export const handler = async (event) => {
counter.inc();
return {
statusCode: 200,
body: `${counter}`,
};
};
You will see that the counter continues to increase in the JS code with each request (until, of course, the virtual machine is shutdown). The more important case for me is of course the connection to a database which can take a relatively long time. |
You are right. The client initialization should happen only once and not in every call to handle. @yoav-steinberg @Chriscbr curious if you have any thoughts here |
Right currently the lifting code (which calls the inflight new) runs on each invocation. This is logically sound but doesn't take "warm start" optimizations into effect. To get this right we need to move the lifting code (and the inflight new execution) to the global level of the generated js function. |
Perhaps as an alternative to hoisting the lifting code to the global level we can implement a global cache object so that clients are only created once. |
So the global cache is always created in the global scope, and each lifted client is looked up there before being instantiated? |
exactly |
It sounds like this is |
It sounds like we can collapse this with #3244 |
I tried this:
Edit: A better example
This happened:
The generated code looks like this:
I expected this:
No response
Is there a workaround?
No response
Anything else?
Is it possible to insert code that will run outside the handler function?
I want to take advantage of the warm start of the lambda in situations such as: connecting to a database, saving cache, etc.
Wing Version
0.54.30
Node.js Version
v18.16.0
Platform(s)
MacOS
Community Notes
The text was updated successfully, but these errors were encountered: