signals.go 718 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // +build linux darwin
  2. package proc
  3. import (
  4. "os"
  5. "os/signal"
  6. "syscall"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. )
  9. const timeFormat = "0102150405"
  10. func init() {
  11. go func() {
  12. var profiler Stopper
  13. // https://golang.org/pkg/os/signal/#Notify
  14. signals := make(chan os.Signal, 1)
  15. signal.Notify(signals, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM)
  16. for {
  17. v := <-signals
  18. switch v {
  19. case syscall.SIGUSR1:
  20. dumpGoroutines()
  21. case syscall.SIGUSR2:
  22. if profiler == nil {
  23. profiler = StartProfile()
  24. } else {
  25. profiler.Stop()
  26. profiler = nil
  27. }
  28. case syscall.SIGTERM:
  29. gracefulStop(signals)
  30. default:
  31. logx.Error("Got unregistered signal:", v)
  32. }
  33. }
  34. }()
  35. }