mirror of
https://github.com/golang/go
synced 2024-11-14 07:40:21 -07:00
net/http: don't spin on temporary accept failure
R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5658049
This commit is contained in:
parent
11f6f7afd6
commit
913abfee3b
@ -1007,15 +1007,26 @@ func (srv *Server) ListenAndServe() error {
|
|||||||
// then call srv.Handler to reply to them.
|
// then call srv.Handler to reply to them.
|
||||||
func (srv *Server) Serve(l net.Listener) error {
|
func (srv *Server) Serve(l net.Listener) error {
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
|
var tempDelay time.Duration // how long to sleep on accept failure
|
||||||
for {
|
for {
|
||||||
rw, e := l.Accept()
|
rw, e := l.Accept()
|
||||||
if e != nil {
|
if e != nil {
|
||||||
if ne, ok := e.(net.Error); ok && ne.Temporary() {
|
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
|
continue
|
||||||
}
|
}
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
tempDelay = 0
|
||||||
if srv.ReadTimeout != 0 {
|
if srv.ReadTimeout != 0 {
|
||||||
rw.SetReadDeadline(time.Now().Add(srv.ReadTimeout))
|
rw.SetReadDeadline(time.Now().Add(srv.ReadTimeout))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user