Hacker News new | past | comments | ask | show | jobs | submit login
Go is less reliable than Flask/Tornado? (webapps.stackexchange.com)
1 point by taw9 on Sept 20, 2012 | hide | past | favorite | 3 comments



The code is probably running out of file descriptors either because they are exhaused prior to Go's gc reaping the open ones or because they are being held open by default Keep-Alive.

Boost the fd count in /etc/security/limits.conf past 1024 or whatever the default is on the OS. Or if that isn't an option, then modify the Go http server code so that it closes each web connection after the request is processed.


How do you force the socket to close after writing the response?


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).




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: