使用节点中的大型媒体文件一次发出多个 post 请求时出现内存问题
memory issue when making multiple post requests at once with large media file in node
我正在尝试使用 request
和 bluebird
上传大型媒体文件。这是我的代码
const options = {
url: 'someuploadurl',
method: 'POST',
json: true,
headers: {
Authorization: 'Bearer access_token',
'Content-Type': 'multipart/form-data'
},
formData: {
file: {
value: mediaData,
options: { filename: 'myMedia.mp4', contentType: 'video/mp4'}
}
}
}
const uploadFunction = (options) => {
new Promise((resolve, reject) => {
request(options, (error, response, body) => {
if(error){
reject(error)
}
resolve(body)
})
}).then(console.log).catch(console.log)
}
const sendMultipleRequests = () => {
const totalRequests = [1, 2, 3, 4, 5, 6, 7, 8]
// make 8 requests at once
blueBird.map(totalRequests, () => {
uploadFunction(options)
})
}
此代码在 totalRequests.length
等于或小于 3 时有效。
当所有媒体文件都很小(< 2-3mb)时代码也有效,但当媒体文件很大(> 30mb)时,它会破坏代码并给出错误 Socket hang up
。
我看到了我系统的内存使用情况,随着请求的增加,内存使用量增加,进程占用了整个 ram 内存,然后代码因错误而崩溃。
请指导我使此代码正常工作,或者您可以分享任何其他上传媒体的方式,这些方式将适用于该场景。
您需要根据 运行 系统可以处理的数量来限制并发请求的数量。
blueBird.map(totalRequests, () => uploadFunction(options), { concurrency: 3 })
我正在尝试使用 request
和 bluebird
上传大型媒体文件。这是我的代码
const options = {
url: 'someuploadurl',
method: 'POST',
json: true,
headers: {
Authorization: 'Bearer access_token',
'Content-Type': 'multipart/form-data'
},
formData: {
file: {
value: mediaData,
options: { filename: 'myMedia.mp4', contentType: 'video/mp4'}
}
}
}
const uploadFunction = (options) => {
new Promise((resolve, reject) => {
request(options, (error, response, body) => {
if(error){
reject(error)
}
resolve(body)
})
}).then(console.log).catch(console.log)
}
const sendMultipleRequests = () => {
const totalRequests = [1, 2, 3, 4, 5, 6, 7, 8]
// make 8 requests at once
blueBird.map(totalRequests, () => {
uploadFunction(options)
})
}
此代码在 totalRequests.length
等于或小于 3 时有效。
当所有媒体文件都很小(< 2-3mb)时代码也有效,但当媒体文件很大(> 30mb)时,它会破坏代码并给出错误 Socket hang up
。
我看到了我系统的内存使用情况,随着请求的增加,内存使用量增加,进程占用了整个 ram 内存,然后代码因错误而崩溃。
请指导我使此代码正常工作,或者您可以分享任何其他上传媒体的方式,这些方式将适用于该场景。
您需要根据 运行 系统可以处理的数量来限制并发请求的数量。
blueBird.map(totalRequests, () => uploadFunction(options), { concurrency: 3 })