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-07-28 19:29:48 +08:00

35 lines
616 B
Go

//go:build !windows
// +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() {
mainStopCh = make(chan struct{})
mc := make(chan os.Signal, 3)
closeOnce := sync.Once{}
signal.Notify(mc, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
go func() {
for {
switch <-mc {
case os.Interrupt, syscall.SIGTERM:
closeOnce.Do(func() {
close(mainStopCh)
})
case syscall.SIGUSR1:
dumpStack()
}
}
}()
})
return mainStopCh
}