Graceful HTTP Server Shutdown with Keep-Alive Connections

2,664 views
Skip to first unread message

Steve Molitor

unread,
Jan 13, 2012, 10:17:34 AM1/13/12
to nod...@googlegroups.com
What's the best way to gracefully shutdown a node HTTP server, with keep alive connections?  I'm calling server.close(), which stops accepting new connections, and wait for the server to emit the 'close' event to exit.  This works fine on my laptop, but not on my EC2 instance.  It never exits.  The Amazon 'ELB Health Checker' is sending HTTP keep alive requests to my node instance, so server.close() thinks it still has open connections (it does in a sense) and never emits the 'close' event.

I'd like to shutdown if there are no connections actively serving requests.  If there is a keep alive connection that is not currently serving a requests I still want to shut down.  

What's the best way to do this?  I think if I listen all the appropriate connection related events I can keep track of things myself.  However it's not clear to me however precisely which events to listen to.   Will this work, or is there a better approach?

Thanks,

Steve

Felix Geisendoerfer

unread,
Jan 13, 2012, 12:07:02 PM1/13/12
to nod...@googlegroups.com
I see a few options:

* You could send "Connection: close" headers as a response to your health check requests. This will work unless you have other clients that have a keep-alive connection that sees activity every < 2 minutes.
* You could lower the keep-alive timeout below the check interval of ELB (default is 2 minutes). This will work unless ELB tries to re-establish the connection right away.
* You could call `server.destroy()` after a given timeout. This will work in all cases, but may drop some legit connections.

AFAIK node currently does not expose the amount of idle keep-alive connections, so if you want to make your decision based on that, you'd have to keep track of them yourself.

HTH,
--fg

Felix Geisendoerfer

unread,
Jan 13, 2012, 12:07:48 PM1/13/12
to nod...@googlegroups.com
Small correction: There is no specific keep-alive timeout, the timeout applies to the underlaying socket of any node http request.

Jann Horn

unread,
Jan 13, 2012, 12:25:56 PM1/13/12
to nod...@googlegroups.com
2012/1/13 Steve Molitor <stevem...@gmail.com>:

> What's the best way to gracefully shutdown a node HTTP server, with keep
> alive connections?  I'm calling server.close(), which stops accepting new
> connections, and wait for the server to emit the 'close' event to exit.

Maybe this should become a node issue... sending "Connection: close"
by default after server.close() was called would work for you, right?

Ben Noordhuis

unread,
Jan 13, 2012, 12:38:45 PM1/13/12
to nod...@googlegroups.com

It's not a bug, it's working as expected. `server.close()` stops Node
from accepting new connections. What you do with the existing
connections is up to you.

Steve Molitor

unread,
Jan 13, 2012, 12:53:47 PM1/13/12
to nod...@googlegroups.com
Yes, I tested sending 'Connection: close' after server.close().  That works perfectly.  Thanks so much.    I think I will go with this option.  It would be nice if node did this by default, IMHO.  

I also tested Felix's suggestions, and they also all work fine.  So I have options.  Thanks Felix and Jann for the help.

Steve


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Jann Horn

unread,
Jan 13, 2012, 1:00:46 PM1/13/12
to nod...@googlegroups.com
2012/1/13 Steve Molitor <stevem...@gmail.com>:

> Yes, I tested sending 'Connection: close' after server.close().  That works
> perfectly.  Thanks so much.    I think I will go with this option.  It would
> be nice if node did this by default, IMHO.

Opened https://github.com/joyent/node/issues/2530 .

Reply all
Reply to author
Forward
0 new messages