Disable Request TImeout in Koa
Asked Answered
R

2

5

I am getting a connection reset error. I am fairly certain this is coming from a long running REST request, that is timing out.

 { [Error: socket hang up] code: 'ECONNRESET' }

Is there a way to disable request timeouts in Koa, so that I can test this hypothesis?

I am running node version 5.x, koa 0.10, centOs 6

Retrorocket answered 19/10, 2016 at 18:5 Comment(4)
What do you mean by disable timeouts? Prevent the request from being made, or prevent it from throwing an error? This post could help, if you haven't read it yet. #10814981Dragging
Thanks Larry - I wondering if there is a more koa, specific answer. It wraps the HTTP stuff and gives you limited control...Retrorocket
Have you tried running the app with DEBUG=* and using app.onerror? You're trying to find the bad request, right?Dragging
AFAIK Koa doesn't impose any timeouts, the socket hang up error is thrown from the underlying nodejs socket. Maybe req.socket.setTimeout() might help you increase the timeout.Blanketyblank
H
5

It seems your request take longer than default Koa timeout. Default Koa timeout is 2 minutes

I had similar problem one request take more time than 2 minutes. I was inspirate by zeronone commend in this post, and finally this line helped to me

ctx.request.socket.setTimeout(5 * 60 * 1000); 

so whole code in router could look like

router.post('/long-request', async (ctx) => {
    // set timeout to 5 minutes
    ctx.request.socket.setTimeout(5 * 60 * 1000); 

    // do some stuf what take long time
    // but less than 5 minutes
});

I really don't recommend do request what take longer than 1 minute, ideally run the heavy stuff on separate process and by other request just check if is the work done.

So that could be good just for testing purposes

Hobnailed answered 27/11, 2017 at 10:37 Comment(0)
E
5

if want to set timeout for application server:

let app = new Koa();
let server=app.listen(3000);
server.timeout=5*60*1000;

if for per request, as @m1uan saying:

router.get("/path",async (ctx)=>{
      ctx.request.socket.setTimeout(5 * 60 * 1000); 
})
Endoplasm answered 1/2, 2019 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.