MCPcopy Create free account
hub / github.com/baidu/EasyFaaS / SetupSignalHandler

Function SetupSignalHandler

cmd/funclet/funclet.go:51–108  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

49}
50
51func SetupSignalHandler() (<-chan struct{}, chan struct{}) {
52 stop := make(chan struct{})
53 finishCh := make(chan struct{})
54 sigc := make(chan os.Signal, 2048)
55 signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGCHLD)
56 var exit bool
57 go func() {
58 for {
59 select {
60 case <-finishCh:
61 exit = true
62 }
63 }
64 }()
65 go func() {
66 for {
67 s := <-sigc
68 logs.Debugf("got signal %s\n", s.String())
69 switch s {
70 case syscall.SIGCHLD:
71 for {
72 var ws syscall.WaitStatus
73 var rus syscall.Rusage
74 pid, err := syscall.Wait4(-1, &ws, syscall.WNOHANG, &rus)
75 if err != nil {
76 if err == syscall.EINTR {
77 continue
78 }
79 if err != syscall.ECHILD {
80 logs.Errorf("wait process err %s ", err)
81 }
82 break
83 }
84 if pid <= 0 {
85 break
86 }
87 logs.V(6).Infof("child pid %d exits\n", pid)
88 }
89 case syscall.SIGINT, syscall.SIGTERM:
90 if !exit {
91 close(stop)
92 // grace shutdown timeout with 10 min
93 timer := time.NewTimer(10 * time.Minute)
94 go func() {
95 select {
96 case <-timer.C:
97 exit = true
98 }
99 }()
100 } else {
101 os.Exit(1)
102 }
103 }
104 }
105 }()
106
107 return stop, finishCh
108}

Callers 2

mainFunction · 0.70
TestSetupSignalHandlerFunction · 0.70

Calls 6

DebugfFunction · 0.92
ErrorfFunction · 0.92
VFunction · 0.92
NotifyMethod · 0.80
InfofMethod · 0.80
StringMethod · 0.65

Tested by 1

TestSetupSignalHandlerFunction · 0.56