-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathreload.go
112 lines (93 loc) · 2.16 KB
/
reload.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
)
// support for reloading configuration without restarting Redwood
var (
configLock sync.RWMutex
configuration *config
)
// getConfig returns the current configuration.
func getConfig() *config {
configLock.RLock()
defer configLock.RUnlock()
return configuration
}
var (
// shutdownChan is closed to indicate that the server is shutting down, and
// no more connections should be accepted.
shutdownChan = make(chan struct{})
activeConnections sync.WaitGroup
)
var configReloadLock sync.Mutex
func reloadConfig() error {
configReloadLock.Lock()
defer configReloadLock.Unlock()
newConf, err := loadConfiguration()
if err != nil {
log.Println("Error reloading configuration:", err)
return err
}
configLock.Lock()
configuration = newConf
configLock.Unlock()
accessLog.Open(newConf.AccessLog)
tlsLog.Open(newConf.TLSLog)
contentLog.Open(filepath.Join(newConf.ContentLogDir, "index.csv"))
starlarkLog.Open(newConf.StarlarkLog)
authLog.Open(newConf.AuthLog)
customLogLock.Lock()
for p, l := range customLogs {
l.Open(p)
}
customLogLock.Unlock()
newConf.openPerUserPorts()
log.Println("Reloaded configuration")
return nil
}
func init() {
hupChan := make(chan os.Signal, 1)
signal.Notify(hupChan, syscall.SIGHUP)
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGTERM)
go func() {
for {
select {
case <-termChan:
log.Println("Received SIGTERM")
close(shutdownChan)
conf := getConfig()
if conf != nil && conf.PIDFile != "" {
os.Remove(conf.PIDFile)
}
go func() {
// Allow 20 seconds for active connections to finish.
time.Sleep(20 * time.Second)
os.Exit(0)
}()
// Or exit when all active connections have finished.
activeConnections.Wait()
os.Exit(0)
case <-hupChan:
log.Println("Received SIGHUP")
reloadConfig()
}
}
}()
}
func handleReload(w http.ResponseWriter, r *http.Request) {
err := reloadConfig()
if err != nil {
fmt.Fprintln(w, "Error reloading configuration:", err)
return
}
fmt.Fprintln(w, "Reloaded configuration")
}