mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
35 lines
650 B
Go
35 lines
650 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, 4)
|
|
closeOnce := sync.Once{}
|
|
signal.Notify(mc, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGUSR1)
|
|
go func() {
|
|
for {
|
|
switch <-mc {
|
|
case os.Interrupt, syscall.SIGTERM:
|
|
closeOnce.Do(func() {
|
|
close(mainStopCh)
|
|
})
|
|
case syscall.SIGQUIT, syscall.SIGUSR1:
|
|
dumpStack()
|
|
}
|
|
}
|
|
}()
|
|
})
|
|
return mainStopCh
|
|
}
|