-
Notifications
You must be signed in to change notification settings - Fork 110
/
watcher.go
188 lines (170 loc) · 4.48 KB
/
watcher.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
package config
import (
"bytes"
"context"
"os"
"time"
"github.com/bep/debounce"
"github.com/edaniels/golog"
"github.com/fsnotify/fsnotify"
"go.viam.com/utils"
)
// A Watcher is responsible for watching for changes
// to a config from some source and delivering those changes
// to some destination.
type Watcher interface {
Config() <-chan *Config
}
// NewWatcher returns an optimally selected Watcher based on the
// given config.
func NewWatcher(ctx context.Context, config *Config, logger golog.Logger) (Watcher, error) {
if err := config.Ensure(false); err != nil {
return nil, err
}
if config.Cloud != nil {
return newCloudWatcher(ctx, config, logger), nil
}
if config.ConfigFilePath != "" {
return newFSWatcher(ctx, config.ConfigFilePath, logger)
}
return noopWatcher{}, nil
}
// A cloudWatcher periodically fetches new configs from the cloud.
type cloudWatcher struct {
configCh chan *Config
watcherDoneCh chan struct{}
cancel func()
}
const checkForNewCertInterval = time.Hour
// newCloudWatcher returns a cloudWatcher that will periodically fetch
// new configs from the cloud.
func newCloudWatcher(ctx context.Context, config *Config, logger golog.Logger) *cloudWatcher {
configCh := make(chan *Config)
watcherDoneCh := make(chan struct{})
cancelCtx, cancel := context.WithCancel(ctx)
nextCheckForNewCert := time.Now().Add(checkForNewCertInterval)
ticker := time.NewTicker(config.Cloud.RefreshInterval)
var prevCfg *Config
utils.ManagedGo(func() {
for {
if !utils.SelectContextOrWait(cancelCtx, config.Cloud.RefreshInterval) {
return
}
var checkForNewCert bool
if time.Now().After(nextCheckForNewCert) {
checkForNewCert = true
}
newConfig, err := readFromCloud(cancelCtx, config, prevCfg, false, checkForNewCert, logger)
if err != nil {
logger.Errorw("error reading cloud config", "error", err)
continue
}
if cp, err := newConfig.CopyOnlyPublicFields(); err == nil {
prevCfg = cp
}
if checkForNewCert {
nextCheckForNewCert = time.Now().Add(checkForNewCertInterval)
}
select {
case <-cancelCtx.Done():
return
case configCh <- newConfig:
}
}
}, func() {
ticker.Stop()
close(watcherDoneCh)
})
return &cloudWatcher{
configCh: configCh,
watcherDoneCh: watcherDoneCh,
cancel: cancel,
}
}
func (w *cloudWatcher) Config() <-chan *Config {
return w.configCh
}
func (w *cloudWatcher) Close() {
w.cancel()
<-w.watcherDoneCh
}
// A fsConfigWatcher fetches new configs from an underlying file when written to.
type fsConfigWatcher struct {
fsWatcher *fsnotify.Watcher
configCh chan *Config
watcherDoneCh chan struct{}
cancel func()
}
// newFSWatcher returns a new v that will fetch new configs
// as soon as the underlying file is written to.
func newFSWatcher(ctx context.Context, configPath string, logger golog.Logger) (*fsConfigWatcher, error) {
fsWatcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
if err := fsWatcher.Add(configPath); err != nil {
return nil, err
}
configCh := make(chan *Config)
watcherDoneCh := make(chan struct{})
cancelCtx, cancel := context.WithCancel(ctx)
var lastRd []byte
utils.ManagedGo(func() {
debounced := debounce.New(time.Millisecond * 500)
for {
if cancelCtx.Err() != nil {
return
}
select {
case <-cancelCtx.Done():
return
case event := <-fsWatcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
debounced(func() {
//nolint:gosec
rd, err := os.ReadFile(configPath)
if err != nil {
logger.Errorw("error reading config file after write", "error", err)
return
}
if bytes.Equal(rd, lastRd) {
return
}
lastRd = rd
newConfig, err := FromReader(cancelCtx, configPath, bytes.NewReader(rd), logger)
if err != nil {
logger.Errorw("error reading config after write", "error", err)
return
}
select {
case <-cancelCtx.Done():
return
case configCh <- newConfig:
}
})
}
}
}
}, func() {
close(watcherDoneCh)
})
return &fsConfigWatcher{
fsWatcher: fsWatcher,
configCh: configCh,
watcherDoneCh: watcherDoneCh,
cancel: cancel,
}, nil
}
func (w *fsConfigWatcher) Config() <-chan *Config {
return w.configCh
}
func (w *fsConfigWatcher) Close() error {
w.cancel()
<-w.watcherDoneCh
return w.fsWatcher.Close()
}
// A noopWatcher does nothing.
type noopWatcher struct{}
func (w noopWatcher) Config() <-chan *Config {
return nil
}