I need help increasing the 2 min default request timeout in a Koa server. I have a long task operation that is taking a few minutes. When it is finished I'm sending a response back. The problem is that the connection is automatically closed after a timeout of 2 min which is node js default.
I tried everything in google. tried using all kind of third party npm modules.
tried ctx.request.socket.setTimeout(0)
I am out of ideas and need help.
I am executing my requests to the server using postmen with infinite timeout.
Update - This is a code snipped of something im trying to do:
const Koa = require('koa')
const app = new Koa()
const PORT = 7555
const server = app.listen(PORT)
app.use(async (ctx, next) => {
ctx.req.setTimeout(0);
await next();
});
app.use(async (ctx, next) => {
const wait = async () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 1000 * 60 * 5)
})
}
await wait()
console.log("settimeout finished", new Date())
ctx.response.body = { success: "true", date: new Date() }
})
I tested this one ... worked fine:
const Koa = require('koa')
const app = new Koa()
const PORT = 7555
const server = app.listen(PORT);
server.setTimeout(0); // <-- this should do the trick ...
app.use(async (ctx, next) => {
console.log("request started", new Date())
const wait = async () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 1000 * 60 * 5)
})
}
await wait()
console.log("settimeout finished", new Date())
ctx.response.body = { success: "true", date: new Date() }
})
Be sure that - if apache
or nginx
is involved in your production system to also modify their configurations (here I increased it to 500 seconds).
For NGINX (proxy)
vim /etc/nginx/nginx.conf
Add following in http{..}
section
(see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout)
http {
#...
proxy_read_timeout 500s;
proxy_send_timeout 500s;
#...
}
For Apache
vim /etc/apache2/apache2.conf:
Search for TimeOut
# ...
TimeOut 500
# ...