Node.js 当多个用户使用同一功能时,Firebase云函数截止日期错误
每当用户必须在存储器中发布图像时,我不会直接使用firebase存储功能,而是使用onCall cloud函数,通过传递base64图像、修改它(5次)并将其发布到存储器中 功能如下:Node.js 当多个用户使用同一功能时,Firebase云函数截止日期错误,node.js,firebase,google-cloud-functions,firebase-storage,deadlines,Node.js,Firebase,Google Cloud Functions,Firebase Storage,Deadlines,每当用户必须在存储器中发布图像时,我不会直接使用firebase存储功能,而是使用onCall cloud函数,通过传递base64图像、修改它(5次)并将其发布到存储器中 功能如下: exports.uploadImage = functions.https.onCall(async (data, context) => { var bucket = admin.storage().bucket(); // Convert the base64 string
exports.uploadImage = functions.https.onCall(async (data, context) => {
var bucket = admin.storage().bucket();
// Convert the base64 string back to an image
var base64EncodedImageString = data.image,
mimeType = 'image/jpeg',
fileName = 'testName',
imageBuffer = Buffer.from(base64EncodedImageString, 'base64');
if (!fs.existsSync(os.tmpdir()+"/myfolder")){
fs.mkdirSync(os.tmpdir()+"/myfolder");
}
const tempFilePath = path.join(os.tmpdir(),"myfolder", fileName+".jpg");
fs.writeFileSync(tempFilePath,base64EncodedImageString,'base64',function(err){
functions.logger.log("file scritto in"+tempFilePath);
})
await bucket.upload(tempFilePath, {
destination: 'test/'+fileName,
metadata: { contentType: mimeType,
metadata: {
firebaseStorageDownloadTokens: uuid()
}
},
});
const tempFilePath_25 = path.join(os.tmpdir(),"myfolder", fileName+"_25.jpg");
spawnSync('convert', [tempFilePath, '-scale', '10%','-scale','1000%>', tempFilePath_25]);
await bucket.upload(tempFilePath_25, {
destination: 'test/'+fileName+"_25.jpg",
metadata: { contentType: mimeType,
metadata: {
firebaseStorageDownloadTokens: uuid()
}
},
});
fs.unlinkSync(tempFilePath_25);
const tempFilePath_50 = path.join(os.tmpdir(),"myfolder", fileName+"_50.jpg");
spawnSync('convert', [tempFilePath, '-scale', '5%','-scale','2000%>', tempFilePath_50]);
await bucket.upload(tempFilePath_50, {
destination: 'test/'+fileName+"_50.jpg",
metadata: { contentType: mimeType,
metadata: {
firebaseStorageDownloadTokens: uuid()
}
},
});
fs.unlinkSync(tempFilePath_50);
const tempFilePath_75 = path.join(os.tmpdir(),"myfolder", fileName+"_75.jpg");
spawnSync('convert', [tempFilePath, '-scale', '3%','-scale','3333%>', tempFilePath_75]);
await bucket.upload(tempFilePath_75, {
destination: 'test/'+fileName+"_75.jpg",
metadata: { contentType: mimeType,
metadata: {
firebaseStorageDownloadTokens: uuid()
}
},
});
fs.unlinkSync(tempFilePath_75);
const tempFilePath_100 = path.join(os.tmpdir(),"myfolder", fileName+"_100.jpg");
spawnSync('convert', [tempFilePath, '-scale', '1%','-scale','10000%>', tempFilePath_100]);
await bucket.upload(tempFilePath_100, {
destination: 'test/'+fileName+"_100.jpg",
metadata: { contentType: mimeType,
metadata: {
firebaseStorageDownloadTokens: uuid()
}
},
});
fs.unlinkSync(tempFilePath_100);
});
我试着每2秒用for循环进行一次模拟,我得到了60%请求的截止时间错误。当我发布应用程序时,会有很多用户(希望如此)可能同时调用相同的函数来发布照片。我怎样才能解决这个问题?提前感谢。什么是“截止日期错误”?此外,您的代码每次都会写入硬编码路径,因此您是否可能遇到同时写入同一个bucket路径的问题?您应该向文件名添加某种类型的唯一性—可能是时间戳,或者使用库,例如
uuid
。这样,并发调用就不会互相覆盖。错误是超过了最后期限。问题是,每个用户需要5秒钟才能完成该函数。如果我有很多用户,由于函数是相同的,我很担心,因为函数有内存限制和调用限制。你在哪里看到这个错误?您是否考虑过在代码周围放置try/catch,以确定代码的哪个部分会抛出该错误?是云函数中的错误,还是调用onCall()
的客户端应用程序中的错误?错误不是来自客户端。代码是“完美的”。不要担心uuid和多个客户端可能相互覆盖的可能性。我知道,我会改变密码。这只是一个测试。问题是,当我用for循环模拟25个用户,每2秒调用相同的函数时,服务器会向我发送此错误。实际上,我认为它无法管理这么多请求。但这是一个问题,因为我的应用程序是一个聊天应用程序,所以这个功能会被很多用户调用很多次。我不能对我的用户说“服务器不可用。请稍后再试”,如果我运行的功能没有问题。即使我用相同的for循环每5秒调用一次。没问题。问题是当我每2秒钟打一次电话。