Browse Source

gracefully shutdown

ls 4 years ago
parent
commit
e31aecf7d1
1 changed files with 9 additions and 5 deletions
  1. 9 5
      serve.go

+ 9 - 5
serve.go

@@ -1,6 +1,7 @@
 package myth
 
 import (
+	"context"
 	"fmt"
 	"log"
 	"net/http"
@@ -8,10 +9,7 @@ import (
 	"os/signal"
 	"syscall"
 	"time"
-
 	//"golang.org/x/net/http2"
-
-	_ "net/http/pprof"
 )
 
 var (
@@ -19,6 +17,7 @@ var (
 	readHeaderTimeout = 30 * time.Second
 	writeTimeout      = 30 * time.Second
 	idleTimeout       = 30 * time.Second
+	shutTimeout       = 30 * time.Second
 )
 
 func newServe(addr string, router http.Handler) *http.Server {
@@ -44,7 +43,7 @@ func run(useV2 bool, addr, certFile, keyFile string, router http.Handler) {
 	errc := make(chan error)
 	go func() {
 		c := make(chan os.Signal)
-		signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
+		signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
 		errc <- fmt.Errorf("%s", <-c)
 	}()
 
@@ -58,7 +57,12 @@ func run(useV2 bool, addr, certFile, keyFile string, router http.Handler) {
 
 		log.Println("Exit HTTP server", "Quit")
 	}()
-	log.Println("Exit", <-errc)
+	e := <-errc
+	log.Println("Recvie", e)
+	ctx, cancel := context.WithTimeout(context.Background(), shutTimeout)
+	defer cancel()
+	s.Shutdown(ctx)
+	log.Println("Exit HTTP server")
 }
 
 // ListenAndServe new server and start