hpds_node/mq_notwindows.go

41 lines
1.0 KiB
Go
Raw Normal View History

2022-10-12 11:55:36 +08:00
//go:build !windows
// +build !windows
package hpds_node
import (
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"git.hpds.cc/Component/network/log"
)
// initialize when mq running as server. support inspection:
// - `kill -SIGUSR1 <pid>` inspect state()
// - `kill -SIGTERM <pid>` graceful shutdown
// - `kill -SIGUSR2 <pid>` inspect golang GC
func (z *messageQueue) init() {
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM, syscall.SIGUSR2, syscall.SIGUSR1, syscall.SIGINT)
2023-04-05 20:59:07 +08:00
log.Infof("%sListening SIGUSR1, SIGUSR2, SIGTERM/SIGINT...", mqLogPrefix)
2022-10-12 11:55:36 +08:00
for p1 := range c {
2023-04-05 20:59:07 +08:00
log.Infof("Received signal: %s", p1)
2022-10-12 11:55:36 +08:00
if p1 == syscall.SIGTERM || p1 == syscall.SIGINT {
2023-04-05 20:59:07 +08:00
log.Infof("graceful shutting down ... %s", p1)
_ = z.Close()
2022-10-12 11:55:36 +08:00
os.Exit(0)
} else if p1 == syscall.SIGUSR2 {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("\tNumGC = %v\n", m.NumGC)
} else if p1 == syscall.SIGUSR1 {
2023-04-05 20:59:07 +08:00
log.Infof("print MessageQueue stats(): %d", z.Stats())
2022-10-12 11:55:36 +08:00
}
}
}()
}