forked from yomorun/yomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipper_notwindows.go
39 lines (35 loc) · 1.07 KB
/
zipper_notwindows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//go:build !windows
// +build !windows
package yomo
import (
"os"
"os/signal"
"runtime"
"syscall"
"github.com/yomorun/yomo/core"
"github.com/yomorun/yomo/core/ylog"
)
// initialize when zipper running as server. support inspection:
// - `kill -SIGUSR1 <pid>` inspect state()
// - `kill -SIGTERM <pid>` graceful shutdown
// - `kill -SIGUSR2 <pid>` inspect golang GC
func waitSignalForShutdownServer(server *core.Server) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM, syscall.SIGUSR2, syscall.SIGUSR1, syscall.SIGINT)
ylog.Info("Listening SIGUSR1, SIGUSR2, SIGTERM/SIGINT...")
for p1 := range c {
ylog.Debug("Received signal", "signal", p1)
if p1 == syscall.SIGTERM || p1 == syscall.SIGINT {
ylog.Debug("graceful shutting down ...", "sign", p1)
// waiting for the server to finish processing the current request
server.Close()
os.Exit(0)
} else if p1 == syscall.SIGUSR2 {
var m runtime.MemStats
runtime.ReadMemStats(&m)
ylog.Debug("runtime stats", "gc_nums", m.NumGC)
} else if p1 == syscall.SIGUSR1 {
statsToLogger(server)
}
}
}