This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.go
341 lines (282 loc) · 7.37 KB
/
manager.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package tablecloth
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
)
// How long to wait for a newly started process to start serving requests.
var StartupDelay = 5 * time.Second
// The maximum time to wait for outstanding connections to complete after
// closing the servers.
var CloseWaitTimeout = 30 * time.Second
// Optional: the working directory for the application. This directory (if specified)
// will be changed to before re-execing.
//
// This is typically used when the working directory is accessed via a symlink
// so that the symlink is re-evaluated when re-execing. This allows updating a symlink
// to point at a new version of the application, and for this to be picked up.
var WorkingDir string
var (
theManager = &manager{}
// variable indirection to facilitate testing
setupFunc = theManager.setup
)
/*
ListenAndServe wraps the equivelent function from net/http, and therefore behaves in
the same way. It adds the necessary tracking for the connections created so that
they can be passed to new processes etc.
If using more than one call to ListenAndServe in an application, each call must pass
a unique string as identifier. This is used to identify the file descriptors passed
to new processes. If identifier is not specified, it uses a value of "default".
In order for the seamless restarts to work it is important that the calling application
exits after all ListenAndServe calls have returned.
A simple example:
package main
import (
"fmt"
"net/http"
"github.com/alext/tablecloth"
)
func main() {
tablecloth.ListenAndServe(":8080", http.HandlerFunc(handler))
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello world")
}
A more involved example that uses multiple ports:
package main
import (
"fmt"
"net/http"
"sync"
"github.com/alext/tablecloth"
)
func main() {
wg := &sync.WaitGroup{}
wg.Add(2)
go serve(":8080", "main", wg)
go serve(":8081", "admin", wg)
wg.Wait()
}
func serve(listenAddr, ident string, wg *sync.WaitGroup) {
defer wg.Done()
tablecloth.ListenAndServe(listenAddr, http.HandlerFunc(handler), ident)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello world")
}
*/
func ListenAndServe(addr string, handler http.Handler, identifier ...string) error {
theManager.once.Do(setupFunc)
ident := "default"
if len(identifier) >= 1 {
ident = identifier[0]
}
return theManager.listenAndServe(addr, handler, ident)
}
type serverInfo struct {
listener *net.TCPListener
server *http.Server
wg sync.WaitGroup
}
type manager struct {
once sync.Once
servers map[string]*serverInfo
serversLock sync.Mutex
activeServers sync.WaitGroup
inParent bool
}
func (m *manager) setup() {
m.servers = make(map[string]*serverInfo)
m.inParent = os.Getenv("TEMPORARY_CHILD") != "1"
go m.handleSignals()
if m.inParent {
go m.stopTemporaryChild()
}
}
func (m *manager) listenAndServe(addr string, handler http.Handler, ident string) error {
m.activeServers.Add(1)
defer m.activeServers.Done()
si, err := m.setupServer(addr, ident, handler)
if err != nil {
return err
}
si.wg.Add(1)
err = si.server.Serve(si.listener)
if err == http.ErrServerClosed {
si.wg.Wait() // Done() called in stopServers()
if m.inParent {
// This function will now never return, so the above defer won't happen.
m.activeServers.Done()
// prevent this goroutine returning before the server has re-exec'd
// This is to cover the case where this is the main goroutine, and exiting
// would therefore prevent the re-exec happening
c := make(chan bool)
<-c
}
return nil
}
return err
}
func (m *manager) setupServer(addr, ident string, handler http.Handler) (*serverInfo, error) {
m.serversLock.Lock()
defer m.serversLock.Unlock()
if m.servers[ident] != nil {
return nil, errors.New("duplicate ident")
}
l, err := resumeOrListen(listenFdFromEnv(ident), addr)
if err != nil {
return nil, err
}
si := &serverInfo{
listener: l,
server: &http.Server{Handler: handler},
}
m.servers[ident] = si
return si, nil
}
func listenFdFromEnv(ident string) int {
listenFD, err := strconv.Atoi(os.Getenv("LISTEN_FD_" + ident))
if err != nil {
return 0
}
return listenFD
}
func (m *manager) handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
for _ = range c {
m.handleHUP()
}
}
func (m *manager) handleHUP() {
m.serversLock.Lock()
defer m.serversLock.Unlock()
if m.inParent {
err := m.upgradeServer()
if err != nil {
log.Println("[tablecloth] error starting new server, aborting reload:", err)
return
}
}
m.stopServers()
}
func (m *manager) upgradeServer() error {
fds := make(map[string]int, len(m.servers))
for ident, si := range m.servers {
fd, err := prepareListenerFd(si.listener)
if err != nil {
// Close any that were successfully prepared so we don't leak.
closeFds(fds)
return err
}
fds[ident] = fd
}
proc, err := m.startTemporaryChild(fds)
if err != nil {
// Close all the copied file descriptors so we don't leak.
closeFds(fds)
return err
}
time.Sleep(StartupDelay)
err = assertChildStillRunning(proc.Pid)
if err != nil {
closeFds(fds)
return err
}
go m.reExecSelf(fds, proc.Pid)
return nil
}
func closeFds(fds map[string]int) {
for ident, fd := range fds {
os.NewFile(uintptr(fd), ident).Close()
}
}
func assertChildStillRunning(pid int) error {
pid, err := syscall.Wait4(pid, nil, syscall.WNOHANG, nil)
if err != nil {
return fmt.Errorf("wait4 error: %s", err.Error())
}
if pid != 0 {
return fmt.Errorf("child no longer running after StartupDelay(%s)", StartupDelay)
}
return nil
}
func (m *manager) stopServers() {
ctx, _ := context.WithTimeout(context.Background(), CloseWaitTimeout)
for _, si := range m.servers {
go func(si *serverInfo) {
defer si.wg.Done()
err := si.server.Shutdown(ctx)
if err != nil {
log.Println("[tablecloth] error shutting down server:", err)
}
}(si)
}
}
func (m *manager) reExecSelf(fds map[string]int, childPid int) {
// wait until there are no active servers
m.activeServers.Wait()
em := newEnvMap(os.Environ())
for ident, fd := range fds {
em["LISTEN_FD_"+ident] = strconv.Itoa(fd)
}
em["TEMPORARY_CHILD_PID"] = strconv.Itoa(childPid)
if WorkingDir != "" {
os.Chdir(WorkingDir)
}
syscall.Exec(os.Args[0], os.Args, em.ToEnv())
}
func (m *manager) startTemporaryChild(fds map[string]int) (proc *os.Process, err error) {
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
em := newEnvMap(os.Environ())
for ident, fd := range fds {
em["LISTEN_FD_"+ident] = strconv.Itoa(fd)
}
em["TEMPORARY_CHILD"] = "1"
cmd.Env = em.ToEnv()
if WorkingDir != "" {
cmd.Dir = WorkingDir
}
err = cmd.Start()
if err != nil {
return nil, err
}
return cmd.Process, nil
}
func (m *manager) stopTemporaryChild() {
childPid, err := strconv.Atoi(os.Getenv("TEMPORARY_CHILD_PID"))
if err != nil {
// non-integer/blank TEMPORARY_CHILD_PID so ignore
return
}
time.Sleep(StartupDelay)
proc, err := os.FindProcess(childPid)
if err != nil {
//TODO: something better here?
// Failed to find process
return
}
err = proc.Signal(syscall.SIGHUP)
if err != nil {
//TODO: better error handling
return
}
_, err = proc.Wait()
if err != nil {
//TODO: better error handling
return
}
}