You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/proc/signals.go

64 lines
1.1 KiB
Go

//go:build linux || darwin
4 years ago
package proc
import (
"os"
"os/signal"
"syscall"
"time"
4 years ago
"github.com/zeromicro/go-zero/core/logx"
4 years ago
)
const (
profileDuration = time.Minute
timeFormat = "0102150405"
)
4 years ago
var done = make(chan struct{})
4 years ago
func init() {
go func() {
// https://golang.org/pkg/os/signal/#Notify
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM, syscall.SIGINT)
4 years ago
for {
v := <-signals
switch v {
case syscall.SIGUSR1:
dumpGoroutines(fileCreator{})
4 years ago
case syscall.SIGUSR2:
profiler := StartProfile()
go func() {
<-time.After(profileDuration)
4 years ago
profiler.Stop()
}()
case syscall.SIGTERM:
stopOnSignal()
gracefulStop(signals, syscall.SIGTERM)
case syscall.SIGINT:
stopOnSignal()
gracefulStop(signals, syscall.SIGINT)
4 years ago
default:
logx.Error("Got unregistered signal:", v)
}
}
}()
}
// Done returns the channel that notifies the process quitting.
func Done() <-chan struct{} {
return done
}
func stopOnSignal() {
select {
case <-done:
// already closed
default:
close(done)
}
}