github.com/lingyao2333/mo-zero@v1.4.1/core/proc/signals.go (about) 1 //go:build linux || darwin 2 // +build linux darwin 3 4 package proc 5 6 import ( 7 "os" 8 "os/signal" 9 "syscall" 10 11 "github.com/lingyao2333/mo-zero/core/logx" 12 ) 13 14 const timeFormat = "0102150405" 15 16 var done = make(chan struct{}) 17 18 func init() { 19 go func() { 20 var profiler Stopper 21 22 // https://golang.org/pkg/os/signal/#Notify 23 signals := make(chan os.Signal, 1) 24 signal.Notify(signals, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM) 25 26 for { 27 v := <-signals 28 switch v { 29 case syscall.SIGUSR1: 30 dumpGoroutines() 31 case syscall.SIGUSR2: 32 if profiler == nil { 33 profiler = StartProfile() 34 } else { 35 profiler.Stop() 36 profiler = nil 37 } 38 case syscall.SIGTERM: 39 select { 40 case <-done: 41 // already closed 42 default: 43 close(done) 44 } 45 46 gracefulStop(signals) 47 default: 48 logx.Error("Got unregistered signal:", v) 49 } 50 } 51 }() 52 } 53 54 // Done returns the channel that notifies the process quitting. 55 func Done() <-chan struct{} { 56 return done 57 }