Commit b641de29 authored by Matthew Slipper's avatar Matthew Slipper

op-service: Exit HTTP server more cleanly

parent de172b11
...@@ -2,13 +2,14 @@ package httputil ...@@ -2,13 +2,14 @@ package httputil
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
) )
func ListenAndServeContext(ctx context.Context, server *http.Server) error { func ListenAndServeContext(ctx context.Context, server *http.Server) error {
errCh := make(chan error) errCh := make(chan error, 1)
go func() { go func() {
errCh <- server.ListenAndServe() errCh <- server.ListenAndServe()
}() }()
...@@ -22,6 +23,19 @@ func ListenAndServeContext(ctx context.Context, server *http.Server) error { ...@@ -22,6 +23,19 @@ func ListenAndServeContext(ctx context.Context, server *http.Server) error {
break break
} }
<-ctx.Done() select {
return ctx.Err() 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
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment