I have a problem with the following error when calling http.ListenAndServe
:
http: Accept error: *ip* accept tcp too many open files; retrying in 10ms
How I can disable this? ulimit -n
is 1024 and I can't change it.
I have a problem with the following error when calling http.ListenAndServe
:
http: Accept error: *ip* accept tcp too many open files; retrying in 10ms
How I can disable this? ulimit -n
is 1024 and I can't change it.
The problem is that you cannot open anymore file descriptors and therefore you cannot accept more connections. If you can't change the fact you have three possible options to deal with this problem:
http
package does this for every temporary network error that occurs.net.Listener
returned by net.Listen
and modify it's Accept()
method to drop the connection on temporary errors.log.Output()
to ioutil.Discard
. (While this is an option, I don't see the use of that. You will end up ignoring important error messages and wonder why your service does not work.)I would prefer the first but there may be a case where you want the second, so here is an example of a connection dropping listener (on play):
type DroppingListener struct {
net.Listener
}
func (d DroppingListener) Accept() (net.Conn, error) {
for {
conn, err := d.Listener.Accept()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
log.Println("Dropping connection because:", ne)
continue
}
}
return conn, err
}
}
func ListenAndServe(addr string, handler http.Handler) error {
srv := &http.Server{Addr: addr, Handler: handler}
l, e := net.Listen("tcp", addr)
if e != nil {
return e
}
l = &DroppingListener{l}
return srv.Serve(l)
}
With this DroppingListener
, the Accept
method will return a connection as soon as there is
no more temporary error.