diff --git a/op-service/httputil/server.go b/op-service/httputil/server.go index 860786a860851ab869ca950edce563c896faa432..bc307cfeae17daf07f983014ecfd31515e61878f 100644 --- a/op-service/httputil/server.go +++ b/op-service/httputil/server.go @@ -2,13 +2,14 @@ package httputil import ( "context" + "errors" "fmt" "net/http" "time" ) func ListenAndServeContext(ctx context.Context, server *http.Server) error { - errCh := make(chan error) + errCh := make(chan error, 1) go func() { errCh <- server.ListenAndServe() }() @@ -22,6 +23,19 @@ func ListenAndServeContext(ctx context.Context, server *http.Server) error { break } - <-ctx.Done() - return ctx.Err() + select { + case err := <-errCh: + if errors.Is(err, http.ErrServerClosed) { + return nil + } + return err + case <-ctx.Done(): + _ = server.Shutdown(context.Background()) + + err := ctx.Err() + if errors.Is(err, context.Canceled) { + return nil + } + return err + } }