gpt4 book ai didi

node.js - 无服务器框架的最佳实践

转载 作者:搜寻专家 更新时间:2023-11-01 00:14:23 25 4
gpt4 key购买 nike

我是无服务器框架的新手。在学习无服务器最佳实践时。 here

我有一个关于“在您的 Lambda 代码之外初始化外部服务”的问题。如何实现?例如:handler.js 中的以下代码

const getOneUser = (event, callback) => {
let response = null;
// validate parameters
if (event.accountid && process.env.SERVERLESS_SURVEYTABLE) {
let docClient = new aws.DynamoDB.DocumentClient();
let params = {
TableName: process.env.SERVERLESS_USERTABLE,
Key: {
accountid: event.accountid,
}
};
docClient.get(params, function(err, data) {
if (err) {
// console.error("Unable to get an item with the request: ", JSON.stringify(params), " along with error: ", JSON.stringify(err));
return callback(getDynamoDBError(err), null);
} else {
if (data.Item) { // got response
// compose response
response = {
accountid: data.Item.accountid,
username: data.Item.username,
email: data.Item.email,
role: data.Item.role,
};
return callback(null, response);
} else {
// console.error("Unable to get an item with the request: ", JSON.stringify(params));
return callback(new Error("404 Not Found: Unable to get an item with the request: " + JSON.stringify(params)), null);
}
}
});
}
// incomplete parameters
else {
return callback(new Error("400 Bad Request: Missing parameters: " + JSON.stringify(event)), null);
}
};

问题是如何在我的 Lambda 代码之外初始化 DynamoDB。

更新 2:

Is below code optimized?

处理程序.js

let survey = require('./survey');
module.exports.handler = (event, context, callback) => {
return survey.getOneSurvey({
accountid: event.accountid,
surveyid: event.surveyid
}, callback);
};

调查.js

let docClient = new aws.DynamoDB.DocumentClient();
module.exports = (() => {
const getOneSurvey = (event, callback) {....
docClient.get(params, function(err, data)...
....
};

return{
getOneSurvey : getOneSurvey,
}
})();

最佳答案

这是有问题的引述:

Initialize external services outside of your Lambda code

When using services (like DynamoDB) make sure to initialize outside of your lambda code. Ex: module initializer (for Node), or to a static constructor (for Java). If you initiate a connection to DDB inside the Lambda function, that code will run on every invoke.

换句话说,在同一个文件中,但在实际处理程序代码之前。

let docClient = new aws.DynamoDB...
...
const getOneUser = (event, callback) => {
....
docClient.get(params, ...

当容器启动时,处理程序外部的代码就会运行。当后续函数调用重用同一个容器时,您可以通过不再实例化外部服务来节省资源和时间。容器经常被重用,但每个容器一次只能处理一个并发请求,它们被重用的频率和时间是你无法控制的……除非你更新功能,在这种情况下任何现有的容器将不再是重用,因为他们拥有旧版本的功能。

您的代码将按编写的方式运行,但未经过优化。

当前一代 Node.js Lambda 函数(Node 4.x/6.x)中出现的这种方法的警告是,一些对象——特别是那些创建与外部服务的文字持久连接的对象——将阻止事件循环不会变空(一个常见的例子是 mysql 数据库连接,它与服务器保持实时 TCP 连接;相比之下,DynamoDB“连接”实际上是无连接的,因为它的传输协议(protocol)是 HTTPS)。在这种情况下,您需要采取不同的方法或 allow lambda to not wait for an empty event loop在卡住容器之前,通过在调用回调之前将 context.callbackWaitsForEmptyEventLoop 设置为 false...但只有在需要时才这样做,并且只有在您完全理解它的含义时才这样做。将其设置为默认值,因为 Internet 上有人说这是个好主意,稍后可能会给您带来神秘的错误。

关于node.js - 无服务器框架的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38238496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com