使用pkgcloud
将文件上传到Rackspace Cloud文件会导致请求超时,然后发生502错误,这会使服务器无法使用(停止)但不会崩溃。
我的代码:
function processImage(req, reply) {
const options = {}
let imageName = null
let imageURL = null
req.multipart(handler, done, options)
// upload to rackspace here
function done() {
const readStream = fs.createReadStream(`${imgsPath}/${imageName}`)
const writeStream = rackspace.upload({
container: 'image-bearbrand-container',
remote: imageName
})
writeStream.on('error', function(err) {
reply.code(400).send({ err })
})
writeStream.on('success', function() {
rimraf.sync(`${imgsPath}/${imageName}`) // delete current image because it now uploaded to rackspace
reply.code(200).send({ imageURI: `${imageCdnUrl}${imageName}` })
})
readStream.pipe(writeStream)
}
// saving image to disk first
function handler(field, file, filename, encoding, mimetype) {
if (mime.includes(mimetype)) {
imageName = newFilename('jpg')
const imageStream = fs.createWriteStream(`${imgsPath}/${imageName}`)
pump(file, imageStream, err => {
if (err) throw Error(err.message)
})
} else {
reply.code(200).send({
message:
'Ssst! your file is malicious! We keep your secret do not worry'
})
}
}
}
因此,这里我的代码正在等待Rackspace上的上传图像结束,然后发送响应,文件图像的大小约为0.5mb至2mb,使用机架空间可能需要 2分钟,然后请求超时,那么我的服务器已停滞并且无法访问。这是因为流永不结束吗??我是否应该对流进行超时,并在请求超时的情况下使其终止?那怎么办?
对于框架,我正在使用fastify。