소스 검색

Graceful shut down server and vips

DarthSim 7 년 전
부모
커밋
b8d655e1ba
4개의 변경된 파일47개의 추가작업 그리고 2개의 파일을 삭제
  1. 13 2
      main.go
  2. 4 0
      process.go
  3. 19 0
      shutdown.go
  4. 11 0
      shutdown_old.go

+ 13 - 2
main.go

@@ -4,6 +4,8 @@ import (
 	"log"
 	"net"
 	"net/http"
+	"os"
+	"os/signal"
 	"runtime/debug"
 	"time"
 
@@ -29,7 +31,16 @@ func main() {
 		MaxHeaderBytes: 1 << 20,
 	}
 
-	log.Printf("Starting server at %s\n", conf.Bind)
+	stop := make(chan os.Signal, 1)
+	signal.Notify(stop, os.Interrupt, os.Kill)
 
-	log.Fatal(s.Serve(netutil.LimitListener(l, conf.MaxClients)))
+	go func() {
+		log.Printf("Starting server at %s\n", conf.Bind)
+		log.Fatal(s.Serve(netutil.LimitListener(l, conf.MaxClients)))
+	}()
+
+	<-stop
+
+	shutdownVips()
+	shutdownServer(s)
 }

+ 4 - 0
process.go

@@ -127,6 +127,10 @@ func initVips() {
 	}
 }
 
+func shutdownVips() {
+	C.vips_shutdown()
+}
+
 func randomAccessRequired(po processingOptions) int {
 	if po.gravity == SMART {
 		return 1

+ 19 - 0
shutdown.go

@@ -0,0 +1,19 @@
+// +build go1.8
+
+package main
+
+import (
+	"context"
+	"log"
+	"net/http"
+	"time"
+)
+
+func shutdownServer(s *http.Server) {
+	log.Println("Shutting down the server...")
+
+	ctx, close := context.WithTimeout(context.Background(), 5*time.Second)
+	defer close()
+
+	s.Shutdown(ctx)
+}

+ 11 - 0
shutdown_old.go

@@ -0,0 +1,11 @@
+// +build !go1.8
+
+package main
+
+import (
+	"net/http"
+)
+
+func shutdownServer(_ *http.Server) {
+	// Nothing we can do here
+}