`
sillycat
  • 浏览: 2539422 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

AWS Lambda and NodeJS

 
阅读更多
AWS Lambda and NodeJS

Concurrency Model
NodeJS is single threaded and the event loop is the concurrency model that allows non-blocking I/O.

NodeJS EventLoop
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

Call Stack
A simple function code is as follow:
‘Use strict’;
Function work() { console.log(‘do work’); }

Function main() { console.log(‘main start’); work(); console.log(‘main end’); }

Main();

The call stack will be  empty -> main -> console/main —> main —> work/main —> console/work/main —> work/main —> main —> console/main —> main —> empty

Task Queue
Any asynchronous work in the runtime is as a task in a queue. FIFO.
When the call stack is empty and the task queue contains one or more tasks. The event loop will remove a task every tick and push in onto the call stack.

AWS Lambda
exports.handler ( event, context, callback)

Callback arguments may be used to return information to the caller and to signal that the handler function has completed so Lambda may end it.
‘use strict';

function timeout(ms) {
    console.log(’timeout start’);   
    return new Promise(resolve => {
        setTimeout(() => {
            console.log(`timeout cb fire after ${ms} ms`);
            resolve();
        }, ms);
    });
}

async function main() {
    conole.log(`main start`); timeout(5e3); console.log(`main end`);
}

main();

>main start
>timeout start
>main end
>timeout cb fired after 5000ms

But if we add one line to execute in Lambda, the lambda handle that differently
exports.handler = main;
>main start
>timeout start
>main end

AWS Lambda Execution Model
Background processes or callbacks initiated by your Lambda function that did not complete when the function ended resume if AWS Lambda chooses to reuse
The Execution Context.
When the callback is called (explicitly or implicitly), AWS Lambda continues the Lambda function invocation until the event loop is empty.

So, after timeout, main event is finished, calls tack and task queue are empty. AWS lambda freeze the process.

Do it Right
You should make sure any background processes or callbacks ( in case of NodeJS ) in your code are complete before the code exits.
Change timeout(5e3); to await timeout(5e3);
function timeout(ms) {
    console.log(`timeout start`);   
    return new Promise(resolve => {
        try {
        setTimeout(() => {
            console.log(`timeout cb fire after ${ms} ms`);
            resolve();
        }, ms);
        } catch(error){
            reject(error);
        }
    });
}
async function main() {
    console.log(`main start`);
    await timeout(5e3);
    console.log(`main end`);
}
main();

After the change, the console will be
main start
timeout start
timeout cb fire after 5000 ms
main end

There are 2 task queues.
macrotasks( e.g. setTimeout) and microtasks(e.g. Promise)

References:
https://medium.com/radient-tech-blog/aws-lambda-and-the-node-js-event-loop-864e48fba49
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics