-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
156 lines (132 loc) · 2.66 KB
/
worker.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
package grace
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
)
const (
WorkerStopSignal = syscall.SIGTERM
)
// wait all goroutine from http connection
var wg sync.WaitGroup
type worker struct {
handlers []http.Handler
servers []server
opt *option
shutdownCh chan struct{}
sync.Mutex
}
type server struct {
listener net.Listener
*http.Server
}
// worker run.
func (w *worker) run() error {
// init servers with fds from master
err := w.initServers()
if err != nil {
return err
}
// start http servers
err = w.startServers()
if err != nil {
return err
}
// water master
go w.watchMaster()
// waitSignal
w.waitSignal()
return nil
}
// init servers listen fds.
func (w *worker) initServers() error {
fdnum, err := strconv.Atoi(os.Getenv(EnvFdNum))
if err != nil {
return fmt.Errorf("invalid %s integer", EnvFdNum)
}
if len(w.handlers) != fdnum {
return fmt.Errorf("handler number does not match numFDs, %v!=%v", len(w.handlers), fdnum)
}
for i := 0; i < fdnum; i++ {
f := os.NewFile(uintptr(3+i), "")
l, err := net.FileListener(f)
if err != nil {
return fmt.Errorf("failed to inherit file descriptor: %d", i)
}
server := server{
Server: &http.Server{
Handler: w.handlers[i],
},
listener: l,
}
w.servers = append(w.servers, server)
}
return nil
}
// start servers.
func (w *worker) startServers() error {
if len(w.servers) == 0 {
return errors.New("no server")
}
for i := 0; i < len(w.servers); i++ {
s := w.servers[i]
go func() {
if err := s.Serve(s.listener); err != nil {
log.Printf("http Serve error: %v\n", err)
}
}()
}
return nil
}
// watchMaster to monitor if master dead
func (w *worker) watchMaster() error {
for {
select {
case <-time.After(w.opt.pulseInterval):
// if parent id change to 1, it means parent is dead
if os.Getppid() == 1 {
log.Printf("master dead, stop worker\n")
w.shutdown()
return nil
}
}
}
w.shutdownCh <- struct{}{}
return nil
}
// wait worker signal.
func (w *worker) waitSignal() {
ch := make(chan os.Signal)
signal.Notify(ch, WorkerStopSignal)
select {
case sig := <-ch:
log.Printf("worker got signal: %v\n", sig)
case <-w.shutdownCh:
log.Printf("stop worker")
}
w.shutdown()
}
// worker graceful shutdown.
func (w *worker) shutdown() {
w.Lock()
defer w.Unlock()
for _, server := range w.servers {
ctx, cancel := context.WithTimeout(context.Background(), w.opt.shutdownTimeout)
defer cancel()
err := server.Shutdown(ctx)
if err != nil {
log.Printf("shutdown server error: %v\n", err)
}
}
// wait process all goroutine.
wg.Wait()
}