http: fix HTTP shutdown panicking

This commit is contained in:
Leo Balduf 2017-04-04 16:05:09 +02:00
parent ea1324602e
commit 2e625af44d
2 changed files with 6 additions and 7 deletions
cmd/chihaya
frontend/http

View file

@ -124,7 +124,7 @@ func rootCmdRun(cmd *cobra.Command, args []string) error {
close(shutdown) close(shutdown)
closed = true closed = true
} else { } else {
log.Infoln(bufErr) log.Errorln(bufErr)
} }
bufErr = err bufErr = err
} }
@ -175,7 +175,7 @@ func startFrontends(httpConfig httpfrontend.Config, udpConfig udpfrontend.Config
go func() { go func() {
log.Infoln("started serving HTTP on", httpConfig.Addr) log.Infoln("started serving HTTP on", httpConfig.Addr)
if err := httpFrontend.ListenAndServe(); err != nil { if err := httpFrontend.ListenAndServe(); err != nil {
errChan <- errors.New("failed to cleanly shutdown HTTP frontend: " + err.Error()) errChan <- err
} }
}() }()
} }
@ -186,7 +186,7 @@ func startFrontends(httpConfig httpfrontend.Config, udpConfig udpfrontend.Config
go func() { go func() {
log.Infoln("started serving UDP on", udpConfig.Addr) log.Infoln("started serving UDP on", udpConfig.Addr)
if err := udpFrontend.ListenAndServe(); err != nil { if err := udpFrontend.ListenAndServe(); err != nil {
errChan <- errors.New("failed to cleanly shutdown UDP frontend: " + err.Error()) errChan <- err
} }
}() }()
} }

View file

@ -5,6 +5,7 @@ package http
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"net" "net"
"net/http" "net/http"
"time" "time"
@ -144,10 +145,8 @@ func (t *Frontend) ListenAndServe() error {
} }
// Start the HTTP server and gracefully handle any network errors. // Start the HTTP server and gracefully handle any network errors.
if err := t.s.ListenAndServe(); err != nil { if err := t.s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
if opErr, ok := err.(*net.OpError); !ok || (ok && opErr.Op != "accept") { return errors.New("http: failed to run HTTP server: " + err.Error())
panic("http: failed to gracefully run HTTP server: " + err.Error())
}
} }
return nil return nil