/
reloader_linux.go
72 lines (59 loc) · 1.34 KB
/
reloader_linux.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package module
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
)
// Reloader aims to handle policies reloading triggers
type Reloader struct {
wg sync.WaitGroup
ctx context.Context
cancelFnc context.CancelFunc
sighupChan chan os.Signal
reloadChan chan struct{}
}
// NewReloader returns a new Reloader
func NewReloader() *Reloader {
ctx, cancelFnc := context.WithCancel(context.Background())
return &Reloader{
sighupChan: make(chan os.Signal, 1),
reloadChan: make(chan struct{}, 1),
ctx: ctx,
cancelFnc: cancelFnc,
}
}
// Start the reloader
func (r *Reloader) Start() error {
signal.Notify(r.sighupChan, syscall.SIGHUP)
r.wg.Add(1)
go func() {
defer r.wg.Done()
for {
select {
case <-r.sighupChan:
r.reloadChan <- struct{}{}
case <-r.ctx.Done():
return
}
}
}()
return nil
}
// Chan returns the chan of reload events
func (r *Reloader) Chan() <-chan struct{} {
return r.reloadChan
}
// Stop the Reloader
func (r *Reloader) Stop() {
signal.Stop(r.sighupChan)
r.cancelFnc()
r.wg.Wait()
close(r.sighupChan)
close(r.reloadChan)
}