所以我想使用Express、Mongoose和Multer的GridFS存储引擎来存储upload large CSV files to a mongoDB cloud database using a Node.js server个,但是when the file upload starts, my database becomes unable to handle any other API requests个.例如,如果在上传文件时,另一个客户端请求从数据库获取用户,服务器将接收该请求并try 从MongoDB云获取用户,but the request will get stuck因为大文件上传会占用所有计算资源.结果,客户端执行的get请求将不会向用户until返回正在进行的文件上载完成.
我知道,如果一个线程执行回调(事件循环)或任务(工作线程)需要很长时间,那么它会被认为是"阻塞"的,并且该 node 也会被阻塞.js在事件循环中运行JavaScript代码,同时它提供了一个工作池来处理昂贵的任务,如文件I/O.我已经读了this blog post by NodeJs.org篇文章,以保持您的 node .js服务器速度很快,在任何给定时间与每个客户机相关的工作都必须是"小"的,我的目标应该是minimize the variation in Task times.这样做的原因是,如果一个工作人员的当前任务比其他任务要昂贵得多,那么它将无法处理其他挂起的任务,从而将工作人员池的大小减少一个,直到任务完成.
换句话说,执行大文件上传的客户端正在执行一项昂贵的任务,这会降低工作池的吞吐量,进而降低服务器的吞吐量.根据上述博客帖子,当每个子任务完成时,它应该提交下一个子任务,当最后一个子任务完成时,它应该通知提交者.This way, between each sub-Task of the long Task(大文件上传),the Worker can work on a sub-Task from a shorter Task,从而解决了阻塞问题.
However, I do not know how to implement this solution in actual code.有没有具体的分区函数可以解决这个问题?我是否必须使用特定的上传架构或 node 包而不是multer gridfs存储来上传文件?请帮忙
以下是我目前使用Multer的GridFS存储引擎实现的文件上传:
// Adjust how files get stored.
const storage = new GridFsStorage({
// The DB connection
db: globalConnection,
// The file's storage configurations.
file: (req, file) => {
...
// Return the file's data to the file property.
return fileData;
}
});
// Configure a strategy for uploading files.
const datasetUpload = multer({
// Set the storage strategy.
storage: storage,
// Set the size limits for uploading a file to 300MB.
limits: { fileSize: 1024 * 1024 * 300 },
// Set the file filter.
fileFilter: fileFilter,
});
// Upload a dataset file.
router.post('/add/dataset', async (req, res)=>{
// Begin the file upload.
datasetUpload.single('file')(req, res, function (err) {
// Get the parsed file from multer.
const file = req.file;
// Upload Success.
return res.status(200).send(file);
});
});