mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-08-28 17:57:16 +00:00
graceful shutdown
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/stefanprodan/k8s-podinfo/pkg/server"
|
||||
"github.com/stefanprodan/k8s-podinfo/pkg/signals"
|
||||
)
|
||||
|
||||
var (
|
||||
port string
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&port, "port", "8989", "Port to listen on.")
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
flag.Parse()
|
||||
|
||||
glog.Infof("Starting HTTP server on port %v", port)
|
||||
|
||||
hts := &http.Server{
|
||||
Addr: ":" + port,
|
||||
Handler: server.New(),
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := hts.ListenAndServe(); err != http.ErrServerClosed {
|
||||
glog.Fatal(err)
|
||||
}
|
||||
}()
|
||||
shutdown(hts, 10*time.Second)
|
||||
}
|
||||
|
||||
func shutdown(hs *http.Server, timeout time.Duration) {
|
||||
stopCh := signals.SetupSignalHandler()
|
||||
<-stopCh
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
glog.Infof("Shutdown with timeout: %s", timeout)
|
||||
|
||||
if err := hs.Shutdown(ctx); err != nil {
|
||||
glog.Errorf("Error: %v\n", err)
|
||||
} else {
|
||||
glog.Info("HTTP server stopped")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user