1

I have a node js server A that makes a request on another server B who then has to make a very complex operation that takes quite some time to finish (it triggers an algorithm that computes in about 3 minutes). I would like the server B to respond to the request with the result of the algorithm. But before the response can be sent the server A receives a response with status code 504.

How can I deal with that ? How can I avoid this 504 error because in my case it is normal that the response is slow to arrive.

Stéphane Bnn
  • 75
  • 2
  • 11
  • Are you using the `request` module or something? Can't you extend the timeout? – Jeremy Thille Jan 18 '18 at 16:07
  • I have found this package: https://github.com/expressjs/timeout But I can't vouch for it (never had this use case) – t3__rry Jan 18 '18 at 16:07
  • I have tried the timeout option of the request module but it did not change anything. – Stéphane Bnn Jan 18 '18 at 16:50
  • I have also tried this package to no success. – Stéphane Bnn Jan 18 '18 at 16:50
  • I use express I also tried server.timeout = 300000 and server.setTimeout(300000) but both did not work either – Stéphane Bnn Jan 18 '18 at 17:08
  • @StéphaneBnn Have you got the property solution? I am having the same issue which I zip and download a large folder, and zip part will take some time to finish . But before that it seems TCP has already closed which causes `Request aborted` sometime – MMzztx Dec 20 '18 at 17:17
  • In my case the problem was an AWS load balancer timeout and I was able to solve it by configuring proper settings and use the timeout option of node js request module – Stéphane Bnn Dec 21 '18 at 19:36

1 Answers1

0

perhaps you could try Server Sent Events. I had success with a similar situation and also allowed for the progress to be reported back to user if your computation algorithm emits some kind of progress. It basically streams the response back to the client.

res.writeHead(200, {
  'Content-Type': 'text/event-stream',
  'Cache-Control': 'no-cache',
  'Connection': 'keep-alive'
});

followed by a progess or other message

res.write(JSON.stringify(progress)+ '\n\n');
njorlsaga
  • 801
  • 2
  • 9
  • 21