News for hackers. Stuff that matters.
Re:Go is less reliable than Flask/Tornado? by taw9 on Thursday September 20, 2012 @11:35AM (#4549048)
How do you force the socket to close after writing the response?
Re:Go is less reliable than Flask/Tornado? by georgemcbay on Thursday September 20, 2012 @01:49PM (#4549723)
There are a few ways. The most control is via hijacking the connection, the most direct is to close the body of the reader (eg. r.Body.Close()) but the best way is to set the "Connection" header to "close" on your writer.

eg. w.Header().Set("Connection", "close") in the handler func.

If you set this, the http package should close the connection for you after it is done processing your current request. You can and usually should call this up front in your handler, it won't close the connection immediately. I prefer this to trying to manually close the connection because it should work despite the function exit point (sort of like a defer without the defer).

It is probably also worth boosting your fd count, because if you're flooded with connections you still might hit the limit faster than you can close the connections. At the very least you may want to do the sorts of things web servers like apache do: http://httpd.apache.org/docs/2.2/vhosts/fd-limits.html (Setrlimit is available in Go's syscall package and may help you out here).