mirror of
https://github.com/outbackdingo/estserver.git
synced 2026-01-28 18:18:54 +00:00
Copied logic from:
78b9c98c5c/registry/registry.go (L299)
Signed-off-by: Andy Doan <andy@foundries.io>
37 lines
769 B
Go
37 lines
769 B
Go
package est
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func RunGracefully(ctx context.Context, server *http.Server, e *echo.Echo) error {
|
|
// setup channel to get notified on SIGTERM signal
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGTERM)
|
|
serveErr := make(chan error)
|
|
|
|
// Start serving in goroutine and listen for stop signal in main thread
|
|
go func() {
|
|
if err := e.StartServer(server); err != http.ErrServerClosed {
|
|
serveErr <- err
|
|
}
|
|
}()
|
|
|
|
select {
|
|
case err := <-serveErr:
|
|
return err
|
|
case <-quit:
|
|
// shutdown the server with a grace period of configured timeout
|
|
c, cancel := context.WithTimeout(ctx, 1*time.Minute)
|
|
defer cancel()
|
|
return server.Shutdown(c)
|
|
}
|
|
}
|