1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00
go-cqhttp/global/signal_unix.go
2021-06-26 14:53:19 +08:00

36 lines
599 B
Go

//+build !windows
package global
import (
"os"
"os/signal"
"sync"
"syscall"
)
// SetupMainSignalHandler is for main to use at last
func SetupMainSignalHandler() <-chan struct{} {
mainOnce.Do(func() {
mc := make(chan os.Signal, 2)
closeOnce := sync.Once{}
signal.Notify(mc, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
go func() {
for {
s := <-mc
switch s {
case os.Interrupt, syscall.SIGTERM:
closeOnce.Do(func() {
close(mc)
})
case syscall.SIGUSR1:
dumpStack()
}
}
}()
mainStopCh = make(chan struct{})
})
return mainStopCh
}