-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.go
More file actions
164 lines (134 loc) · 2.94 KB
/
Copy pathserver.go
File metadata and controls
164 lines (134 loc) · 2.94 KB
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
package httpserver
import (
"context"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
log "github.com/sirupsen/logrus"
)
var cfg *Config
var srv *http.Server
func fork() (*os.Process, error) {
// get the listener file
lnFile, err := getListenerFile(cfg.ln)
if err != nil {
return nil, err
}
defer lnFile.Close()
// pass the stdin, stdout, stderr, and the listener files to the child
files := []*os.File{
os.Stdin,
os.Stdout,
os.Stderr,
lnFile,
}
// get process name and dir
execName, err := os.Executable()
if err != nil {
return nil, err
}
execDir := filepath.Dir(execName)
// spawn a child
p, err := os.StartProcess(execName, []string{execName}, &os.ProcAttr{
Dir: execDir,
Files: files,
Sys: &syscall.SysProcAttr{},
})
if err != nil {
return nil, err
}
return p, nil
}
func handleHangup() error {
c := make(chan string)
defer close(c)
errChn := make(chan error)
defer close(errChn)
go socketListener(c, errChn)
for {
select {
case cmd := <-c:
switch cmd {
case "socket_opened":
p, err := fork()
if err != nil {
log.WithError(err).
Error("Unable to fork child")
continue
}
log.WithField("PID", p.Pid).Info("Spawned a new child. Waiting for spinup.")
case "listener_sent":
log.
Debug("Sent listener information to fork, shutting down parent")
return nil
}
case err := <-errChn:
return err
}
}
}
func waitForSignals() error {
sig := make(chan os.Signal, cfg.SignalBufferSize)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
for {
select {
case s := <-sig:
switch s {
case syscall.SIGHUP:
err := handleHangup()
if err == nil {
return shutdown()
}
case syscall.SIGTERM, syscall.SIGINT:
return shutdown()
}
}
}
}
func start(handler http.Handler) {
srv = &http.Server{
Addr: cfg.Addr,
Handler: handler,
}
go srv.Serve(cfg.ln)
}
func shutdown() error {
log.Debug("Server shutting down")
ctx, cancel := context.WithTimeout(context.Background(), cfg.Timeout*time.Second)
defer cancel()
err := srv.Shutdown(ctx)
log.WithError(err).Debug("Server shut down")
return err
}
// Serve starts a new listener and an HTTP server. It will also start listening
// for system signals.
//
// SIGHUP - gracefully restart the server
// SIGTERM - gracefully shutdown the server with timeout
func Serve(config Config, handler http.Handler) error {
cfg = &getConfig(config)
var err error
cfg.ln, err = getListener()
if err != nil {
log.WithError(err).Panic("Unable to create or import a listener")
}
start(handler)
err = waitForSignals()
if err != nil {
log.WithError(err).Info("Exiting with error")
return err
}
log.Info("Exiting")
return nil
}
// Stop gracefully stops the server. If 'true' is passed, the server will be
// killed without gracefull shutting down open connections.
func Stop(kill bool) error {
if kill {
return srv.Close()
}
return shutdown()
}