2

I need to force my express.js app to "abort" after a given amount of time if there is no response, in order to avoid the heroku H12 timeout that make my dyno crash (which require a manual restart).

I tried to add req.setTimeout(15000) , but it works only locally. On heroku my requests continue to wait for a response beyond the 15 second limit and the dyno crash once the heroku 30 second limit is reached.

Any suggestion ?

John doe
  • 3,680
  • 7
  • 31
  • 65
  • What is the purpose of the app? Is it a website or a worker such as a script or bot? – Harshana Nov 16 '20 at 02:01
  • This is mostly an api – John doe Nov 16 '20 at 02:02
  • Consider queuing tasks that tend to take a long amount of time, and using a separate tier of worker dynos to process the queue jobs (with no time limit). – ceejayoz Nov 16 '20 at 02:36
  • Does this answer your question? [How to have more then 30sec response timeout in heroku](https://stackoverflow.com/questions/69497229/how-to-have-more-then-30sec-response-timeout-in-heroku) – ggorlen Jan 30 '23 at 23:31

2 Answers2

0

You can try the express timeout middleware to timeout the request:

npm install connect-timeout
const timeout = require('connect-timeout')
app.use(timeout('15s'))

For more info : http://expressjs.com/en/resources/middleware/timeout.html

Harshana
  • 5,151
  • 1
  • 17
  • 27
  • It doesn't seem to work. My dyno still crash after 30 seconds. – John doe Nov 16 '20 at 03:09
  • You should move your tasks to background which that tend to take a long amount of time.You can add worker dyno to execute long tasks as there is restrictions of 30 seconds on heroku web dyno. – svikramjeet Nov 16 '20 at 06:17
0

You cannot configure the timeout. If your app takes more than 30secs to do its job then timeout will be called.

To avoid this you should send some response within 30secs. You can achieve it by creating a background dyno

follow this document for more details

https://devcenter.heroku.com/articles/request-timeout

pranav shinde
  • 1,260
  • 13
  • 11