Koa - 无法增加默认请求超时
Koa - cannot increase default request timeout
我需要帮助增加 Koa 服务器中 2 分钟的默认请求超时。
我有一个需要几分钟的长任务操作。完成后,我会发回回复。问题是连接在节点 js 默认的 2 分钟超时后自动关闭。
我尝试了 google 中的所有方法。
尝试使用各种第三方 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() }
})
您可以像这样在应用的早期添加中间件:
app.use(async (ctx, next) => {
ctx.req.setTimeout(0);
await next();
});
看起来 koa 保留了 120s timeout default from http_server.js
因此,您可以尝试入侵它,但这个中间件应该可以解决问题。
你也可以试试这个:
const server = app.listen(PORT); // This returns the https_server instance
server.setTimeout(600000)
可以看到相关信息here
我测试了这个...工作正常:
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() }
})
确保 - 如果 apache
或 nginx
涉及到您的生产系统也修改它们的配置(这里我将其增加到 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
# ...
我需要帮助增加 Koa 服务器中 2 分钟的默认请求超时。 我有一个需要几分钟的长任务操作。完成后,我会发回回复。问题是连接在节点 js 默认的 2 分钟超时后自动关闭。
我尝试了 google 中的所有方法。 尝试使用各种第三方 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() }
})
您可以像这样在应用的早期添加中间件:
app.use(async (ctx, next) => {
ctx.req.setTimeout(0);
await next();
});
看起来 koa 保留了 120s timeout default from http_server.js
因此,您可以尝试入侵它,但这个中间件应该可以解决问题。
你也可以试试这个:
const server = app.listen(PORT); // This returns the https_server instance
server.setTimeout(600000)
可以看到相关信息here
我测试了这个...工作正常:
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() }
})
确保 - 如果 apache
或 nginx
涉及到您的生产系统也修改它们的配置(这里我将其增加到 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
# ...