Koa-无法增加默认请求超时



我需要帮助在Koa服务器中增加2分钟的默认请求超时。我有一个需要几分钟的长任务操作。当它完成时,我会发回一个回复。问题是,连接在超时2分钟后自动关闭,这是节点js的默认值。

我在谷歌上什么都试过了。尝试使用所有类型的第三方npm模块。

已尝试ctx.request.socket.setTimeout(0(

我没有主意,需要帮助。

我正在使用无限超时的邮递员执行对服务器的请求。

更新-这是我试图做的事情的代码剪辑:

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

我测试了这个。。。工作良好:

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

请确保-如果生产系统中涉及apachenginx,也可以修改它们的配置(此处我将其增加到500秒(。

对于NGINX(代理(

vim /etc/nginx/nginx.conf

http{..}部分添加以下内容

(参见http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout)

http {
#...
proxy_read_timeout 500s;
proxy_send_timeout 500s;
#...
}

对于Apache

vim /etc/apache2/apache2.conf:

搜索超时

# ...
TimeOut 500
# ...

您可以在应用程序的早期添加一个中间件,如下所示:

app.use(async (ctx, next) => {
ctx.req.setTimeout(0);
await next();
});

koa似乎保留了http_server.js 的120秒超时默认值

所以,你可以尝试破解它,但这个中间件应该能做到。

你也可以试试这个:

const server = app.listen(PORT); // This returns the https_server instance
server.setTimeout(600000)

你可以在这里看到相关信息

最新更新