zipper_notwindows.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //go:build !windows
  2. // +build !windows
  3. package yomo
  4. import (
  5. "os"
  6. "os/signal"
  7. "runtime"
  8. "syscall"
  9. "github.com/yomorun/yomo/core"
  10. "github.com/yomorun/yomo/core/ylog"
  11. "github.com/yomorun/yomo/pkg/trace"
  12. )
  13. // initialize when zipper running as server. support inspection:
  14. // - `kill -SIGUSR1 <pid>` inspect state()
  15. // - `kill -SIGTERM <pid>` graceful shutdown
  16. // - `kill -SIGUSR2 <pid>` inspect golang GC
  17. func waitSignalForShutdownServer(server *core.Server) {
  18. c := make(chan os.Signal, 1)
  19. signal.Notify(c, syscall.SIGTERM, syscall.SIGUSR2, syscall.SIGUSR1, syscall.SIGINT)
  20. ylog.Info("listening SIGUSR1, SIGUSR2, SIGTERM/SIGINT...")
  21. for p1 := range c {
  22. ylog.Debug("Received signal", "signal", p1)
  23. if p1 == syscall.SIGTERM || p1 == syscall.SIGINT {
  24. ylog.Debug("graceful shutting down ...", "sign", p1)
  25. // waiting for the server to finish processing the current request
  26. server.Close()
  27. trace.ShutdownTracerProvider()
  28. os.Exit(0)
  29. } else if p1 == syscall.SIGUSR2 {
  30. var m runtime.MemStats
  31. runtime.ReadMemStats(&m)
  32. ylog.Debug("runtime stats", "gc_nums", m.NumGC)
  33. } else if p1 == syscall.SIGUSR1 {
  34. statsToLogger(server)
  35. }
  36. }
  37. }