-
Notifications
You must be signed in to change notification settings - Fork 2
/
relaoder.go
70 lines (58 loc) · 1.46 KB
/
relaoder.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
package gofigure
import (
"log"
"os"
"os/signal"
"syscall"
)
// Reloader is an interface to be implemented by the calling program, that gets called when we need
// to reload our configs
type Reloader interface {
Reload()
}
// ReloadFunc can be used to make a simple func conform to the Reloader interface
type ReloadFunc func()
func (f ReloadFunc) Reload() {
f()
}
// ReloadMonitor is an interface for waiting for external notifications that we need to reload our configs.
// The only implementation right now is a SIGHUP listener
type ReloadMonitor interface {
Watch(Reloader)
Stop()
}
// SignalMonitor is a monitor that waits for SIGHUP and calls its Reloader
type SignalMonitor struct {
stopch chan bool
}
// NewSignalMonitor creates a new signal monitor
func NewSignalMonitor() *SignalMonitor {
return &SignalMonitor{
make(chan bool),
}
}
// Monitor waits for SIGHUP and calls the reloader's Reload method
func (m *SignalMonitor) Monitor(r Reloader) {
go func() {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, syscall.SIGHUP)
for {
// Block until a signal is received.
select {
case sig := <-sigch:
log.Printf("Received signal %v", sig)
// call the reloader to reload its configs
r.Reload()
case <-m.stopch:
log.Printf("Stopping reload listener")
break
}
}
signal.Stop(sigch)
}()
}
// Stop stops the signal monitor
func (m *SignalMonitor) Stop() {
m.stopch <- true
log.Printf("Stopped signal monitor")
}