This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
graceful_unix.go
249 lines (223 loc) · 4.74 KB
/
graceful_unix.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//go:build !windows
// +build !windows
package well
import (
"context"
"errors"
"io"
"net"
"os"
"os/exec"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/cybozu-go/log"
)
const (
listenEnv = "CYBOZU_LISTEN_FDS"
restartWait = 10 * time.Millisecond
)
func isMaster() bool {
return len(os.Getenv(listenEnv)) == 0
}
type fileFunc interface {
File() (f *os.File, err error)
}
func listenerFiles(listeners []net.Listener) ([]*os.File, error) {
files := make([]*os.File, 0, len(listeners))
for _, l := range listeners {
fd, ok := l.(fileFunc)
if !ok {
return nil, errors.New("no File() method for " + l.Addr().String())
}
f, err := fd.File()
if err != nil {
return nil, err
}
files = append(files, f)
}
return files, nil
}
func restoreListeners(envvar string) ([]net.Listener, error) {
nfds, err := strconv.Atoi(os.Getenv(envvar))
defer os.Unsetenv(envvar)
if err != nil {
return nil, err
}
if nfds == 0 {
return nil, nil
}
log.Debug("well: restored listeners", map[string]interface{}{
"nfds": nfds,
})
ls := make([]net.Listener, 0, nfds)
for i := 0; i < nfds; i++ {
fd := 3 + i
f := os.NewFile(uintptr(fd), "FD"+strconv.Itoa(fd))
l, err := net.FileListener(f)
f.Close()
if err != nil {
return nil, err
}
ls = append(ls, l)
}
return ls, nil
}
// SystemdListeners returns listeners from systemd socket activation.
func SystemdListeners() ([]net.Listener, error) {
pid, err := strconv.Atoi(os.Getenv("LISTEN_PID"))
if err != nil {
return nil, err
}
if pid != os.Getpid() {
return nil, nil
}
return restoreListeners("LISTEN_FDS")
}
// Run runs the graceful restarting server.
//
// If this is the master process, Run starts a child process,
// and installs SIGHUP handler to restarts the child process.
//
// If this is a child process, Run simply calls g.Serve.
//
// Run returns immediately in the master process, and never
// returns in the child process.
func (g *Graceful) Run() {
if isMaster() {
env := g.Env
if env == nil {
env = defaultEnv
}
env.Go(g.runMaster)
return
}
lns, err := restoreListeners(listenEnv)
if err != nil {
log.ErrorExit(err)
}
log.DefaultLogger().SetDefaults(map[string]interface{}{
"pid": os.Getpid(),
})
log.Info("well: new child", nil)
g.Serve(lns)
// child process should not return.
os.Exit(0)
}
// runMaster is the main function of the master process.
func (g *Graceful) runMaster(ctx context.Context) error {
logger := log.DefaultLogger()
// prepare listener files
listeners, err := g.Listen()
if err != nil {
return err
}
files, err := listenerFiles(listeners)
if err != nil {
return err
}
if len(files) == 0 {
return errors.New("no listener")
}
defer func() {
for _, f := range files {
f.Close()
}
// we cannot close listeners no sooner than this point
// because net.UnixListener removes the socket file on Close.
for _, l := range listeners {
l.Close()
}
}()
sighup := make(chan os.Signal, 2)
signal.Notify(sighup, syscall.SIGHUP)
RESTART:
child := g.makeChild(files)
clog, err := child.StderrPipe()
if err != nil {
return err
}
copyDone := make(chan struct{})
// clog will be closed on child.Wait().
go copyLog(logger, clog, copyDone)
done := make(chan error, 1)
err = child.Start()
if err != nil {
return err
}
go func() {
<-copyDone
done <- child.Wait()
}()
select {
case err := <-done:
return err
case <-sighup:
child.Process.Signal(syscall.SIGTERM)
log.Warn("well: got sighup", nil)
time.Sleep(restartWait)
goto RESTART
case <-ctx.Done():
child.Process.Signal(syscall.SIGTERM)
if g.ExitTimeout == 0 {
<-done
return nil
}
select {
case <-done:
return nil
case <-time.After(g.ExitTimeout):
logger.Warn("well: timeout child exit", nil)
return nil
}
}
}
func (g *Graceful) makeChild(files []*os.File) *exec.Cmd {
child := exec.Command(os.Args[0], os.Args[1:]...)
child.Env = os.Environ()
child.Env = append(child.Env, listenEnv+"="+strconv.Itoa(len(files)))
child.ExtraFiles = files
return child
}
func copyLog(logger *log.Logger, r io.Reader, done chan<- struct{}) {
defer func() {
close(done)
}()
var unwritten []byte
buf := make([]byte, 1<<20)
for {
n, err := r.Read(buf)
if err != nil {
if len(unwritten) == 0 {
if n > 0 {
logger.WriteThrough(buf[0:n])
}
return
}
unwritten = append(unwritten, buf[0:n]...)
logger.WriteThrough(unwritten)
return
}
if n == 0 {
continue
}
if buf[n-1] != '\n' {
unwritten = append(unwritten, buf[0:n]...)
continue
}
if len(unwritten) == 0 {
err = logger.WriteThrough(buf[0:n])
if err != nil {
return
}
continue
}
unwritten = append(unwritten, buf[0:n]...)
err = logger.WriteThrough(unwritten)
if err != nil {
return
}
unwritten = unwritten[:0]
}
}