4

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

akaphenom
  • 6,728
  • 10
  • 59
  • 109
  • 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. http://stackoverflow.com/questions/10814481/how-to-debug-a-socket-hang-up-error-in-nodejs/11542134#11542134 – Larry Turtis Oct 19 '16 at 19:45
  • Thanks Larry - I wondering if there is a more koa, specific answer. It wraps the HTTP stuff and gives you limited control... – akaphenom Oct 19 '16 at 19:56
  • Have you tried running the app with `DEBUG=*` and using app.onerror? You're trying to find the bad request, right? – Larry Turtis Oct 19 '16 at 20:09
  • 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. – zeronone Oct 30 '16 at 13:35

2 Answers2

4

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); 
})
neo
  • 51
  • 5
3

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

Community
  • 1
  • 1
m1uan
  • 528
  • 6
  • 16