0

Using pkgcloud to upload file to Rackspace Cloud files causing Request Timeout and then 502 error happens makes server unusable (stopped) but not crash.

My Code :

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'
      })
    }
  }
}

So here my code was waiting for upload image on Rackspace ended then send the response the size of file image are around 0.5mb to 2mb, it may takes 2 minutes using rackspace and then request timeout then my server become stalled and cannot be accessed. is this because the stream never ended? Should I do timeout on stream and make it ends if request was timeout? how to do that then?

For framework I'm using fastify.

mandaputtra
  • 922
  • 3
  • 18
  • 36
  • Did you set a big request timeout? (on client side) – Manuel Spigolon Jan 20 '20 at 08:11
  • I remember switching to amazon for this problem. @ManuelSpigolon Nope, I just upload file on the back-end side, after some digging the upload to rack-space server are super slow, and when I force stop the server with `CTRL + C` turns out that the upload (Streams) are still going so that is why I got 502 Proxy error. The Rackspace support center cannot help me too ~ they din't know why too. Digging on the lib / creating just a lib for this / using curl. Resulted in slow upload so I just changed service. – mandaputtra Jan 20 '20 at 09:54

0 Answers0