-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignal-alarm.go
97 lines (80 loc) · 1.29 KB
/
signal-alarm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"time"
"sync"
"os"
"os/signal"
"syscall"
)
func someThing() {
for i:=1; i<10000; i++ {
fmt.Println("http_request-----------", i)
time.Sleep(time.Second)
}
}
func doMessage(c int) {
fmt.Println("---start", c)
time.Sleep(time.Second * 5)
fmt.Println("---end", c )
}
func runWorker(chq chan int, done chan bool) {
for {
select {
case <-done:
fmt.Println("bye~~")
return
case c := <-chq:
priority:
for {
select {
case <-done:
fmt.Println("bye~~")
return
default:
break priority
}
}
doMessage(c)
default:
fmt.Println("dddd====")
time.Sleep(time.Second)
}
}
}
func listenSignal(cs chan os.Signal, curr int, done chan bool) {
<-cs
for i:=0; i<curr; i++ {
done<-true
}
for {
if len(done) == 0 {
break
}
time.Sleep(time.Second)
}
signal.Stop(cs)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
var wg sync.WaitGroup
func test() {
chq := make(chan int, 100)
for i:=1; i< 50; i++ {
chq <- i
}
//close(chq)
curr := 3
done := make(chan bool, curr)
cs := make(chan os.Signal)
signal.Notify(cs, syscall.SIGINT, syscall.SIGTERM)
go listenSignal(cs, curr, done)
for i:=0; i<curr; i++ {
wg.Add(1)
go runWorker(chq, done)
}
someThing()
wg.Wait()
}
func main() {
test()
}