-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
stat.go
93 lines (74 loc) · 1.33 KB
/
stat.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
package main
import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
)
type (
Stater interface {
AddConn(key string, conn *net.TCPConn)
DelConn(key string)
Start()
Stop()
}
compositeStater struct {
staters []Stater
}
)
func NewStater(staters ...Stater) Stater {
stat := compositeStater{
staters: append([]Stater(nil), staters...),
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
for sig := range c {
signal.Stop(c)
stat.Stop()
p, err := os.FindProcess(syscall.Getpid())
if err != nil {
fmt.Println(err)
os.Exit(0)
}
if err := p.Signal(sig); err != nil {
fmt.Println(err)
}
}
}()
return stat
}
func (c compositeStater) AddConn(key string, conn *net.TCPConn) {
for _, s := range c.staters {
s.AddConn(key, conn)
}
}
func (c compositeStater) DelConn(key string) {
for _, s := range c.staters {
s.DelConn(key)
}
}
func (c compositeStater) Start() {
for _, s := range c.staters {
s.Start()
}
}
func (c compositeStater) Stop() {
for _, s := range c.staters {
s.Stop()
}
}
type NilPrinter struct{}
func NewNilPrinter(_ time.Duration) Stater {
return NilPrinter{}
}
func (p NilPrinter) AddConn(_ string, _ *net.TCPConn) {
}
func (p NilPrinter) DelConn(_ string) {
}
func (p NilPrinter) Start() {
}
func (p NilPrinter) Stop() {
}