From 913abfee3bd25af5d80b3b9079d22f8e296d94c8 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 14 Feb 2012 15:04:29 +1100 Subject: [PATCH] net/http: don't spin on temporary accept failure R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5658049 --- src/pkg/net/http/server.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pkg/net/http/server.go b/src/pkg/net/http/server.go index fb3bc81756..e715c73cb6 100644 --- a/src/pkg/net/http/server.go +++ b/src/pkg/net/http/server.go @@ -1007,15 +1007,26 @@ func (srv *Server) ListenAndServe() error { // then call srv.Handler to reply to them. func (srv *Server) Serve(l net.Listener) error { defer l.Close() + var tempDelay time.Duration // how long to sleep on accept failure for { rw, e := l.Accept() if e != nil { if ne, ok := e.(net.Error); ok && ne.Temporary() { - log.Printf("http: Accept error: %v", e) + if tempDelay == 0 { + tempDelay = 5 * time.Millisecond + } else { + tempDelay *= 2 + } + if max := 1 * time.Second; tempDelay > max { + tempDelay = max + } + log.Printf("http: Accept error: %v; retrying in %v", e, tempDelay) + time.Sleep(tempDelay) continue } return e } + tempDelay = 0 if srv.ReadTimeout != 0 { rw.SetReadDeadline(time.Now().Add(srv.ReadTimeout)) }