-
Notifications
You must be signed in to change notification settings - Fork 3
/
master.go
155 lines (134 loc) · 2.86 KB
/
master.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package graceful
import (
"fmt"
"log"
"net"
"os"
"os/exec"
"os/signal"
)
type master struct {
addr Address
opt *option
socketFiles []*os.File
workerPid int
workerExit chan error
}
func (m *master) run(services []*service) error {
err := m.createFDs(services)
if err != nil {
return err
}
pid, err := m.forkWorker()
if err != nil {
return err
}
m.workerPid = pid
m.waitSignal()
return nil
}
func (m *master) waitSignal() {
ch := make(chan os.Signal, 1)
sigs := make([]os.Signal, 0, len(m.opt.reloadSignals)+len(m.opt.stopSignals))
for _, s := range m.opt.reloadSignals {
sigs = append(sigs, s)
}
for _, s := range m.opt.stopSignals {
sigs = append(sigs, s)
}
signal.Notify(ch, sigs...)
for {
var sig os.Signal
select {
case err := <-m.workerExit:
if _, ok := err.(*exec.ExitError); ok {
log.Printf("[warning] worker exit with error: %+v, master is going to shutdown.", err)
m.stop()
return
}
case sig = <-ch:
log.Printf("[info] master got signal: %v\n", sig)
}
for _, s := range m.opt.stopSignals {
if s == sig {
m.stop()
return
}
}
for _, s := range m.opt.reloadSignals {
if s == sig {
m.reload()
break
}
}
}
}
func (m *master) reload() {
pid, err := m.forkWorker()
if err != nil {
log.Printf("[warning] fork error: %v\n", err)
}
m.workerPid = pid
}
func (m *master) stop() {
os.Exit(1)
}
func (m *master) forkWorker() (int, error) {
// 获取 args
var args []string
path := os.Args[0]
if len(os.Args) > 1 {
args = os.Args[1:]
}
workerFlag := fmt.Sprintf("%s=%s", EnvWorker, EnvWorkerVal)
oldWorkerPid := fmt.Sprintf("%s=%d", EnvOldWorkerPid, m.workerPid)
env := append(os.Environ(), workerFlag, oldWorkerPid)
cmd := exec.Command(path, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.ExtraFiles = m.socketFiles
cmd.Env = env
err := cmd.Start()
if err != nil {
return 0, err
}
go func() {
m.workerExit <- cmd.Wait()
}()
forkId := cmd.Process.Pid
log.Printf("[info] start new process success, pid %d\n", forkId)
return forkId, nil
}
func (m *master) createFDs(services []*service) error {
m.socketFiles = make([]*os.File, 0, len(services))
for _, service := range services {
f, err := m.listen(service.addr)
if err != nil {
return fmt.Errorf("failed to listen on addr: %s, err: %v", m.addr, err)
}
m.socketFiles = append(m.socketFiles, f)
}
return nil
}
func (m *master) listen(addr Address) (*os.File, error) {
if addr.network == "tcp" {
a, err := net.ResolveTCPAddr("tcp", addr.addr)
if err != nil {
return nil, err
}
l, err := net.ListenTCP("tcp", a)
if err != nil {
return nil, err
}
f, err := l.File()
if err != nil {
return nil, err
}
if err := l.Close(); err != nil {
return nil, err
}
return f, nil
}
return nil, fmt.Errorf("unknown network: %v", addr.network)
}