2
0

signal.go 839 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) 2024 Winlin
  2. //
  3. // SPDX-License-Identifier: MIT
  4. package main
  5. import (
  6. "context"
  7. "os"
  8. "os/signal"
  9. "syscall"
  10. "time"
  11. "srs-proxy/errors"
  12. "srs-proxy/logger"
  13. )
  14. func installSignals(ctx context.Context, cancel context.CancelFunc) {
  15. sc := make(chan os.Signal, 1)
  16. signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
  17. go func() {
  18. for s := range sc {
  19. logger.Df(ctx, "Got signal %v", s)
  20. cancel()
  21. }
  22. }()
  23. }
  24. func installForceQuit(ctx context.Context) error {
  25. var forceTimeout time.Duration
  26. if t, err := time.ParseDuration(envForceQuitTimeout()); err != nil {
  27. return errors.Wrapf(err, "parse force timeout %v", envForceQuitTimeout())
  28. } else {
  29. forceTimeout = t
  30. }
  31. go func() {
  32. <-ctx.Done()
  33. time.Sleep(forceTimeout)
  34. logger.Wf(ctx, "Force to exit by timeout")
  35. os.Exit(1)
  36. }()
  37. return nil
  38. }