Graceful shut down server and vips

This commit is contained in:
DarthSim
2017-10-17 18:23:48 +06:00
parent 5b24ff905e
commit b8d655e1ba
4 changed files with 47 additions and 2 deletions

15
main.go
View File

@@ -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)
}

View File

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

19
shutdown.go Normal file
View File

@@ -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
shutdown_old.go Normal file
View File

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