Skip to content

Commit 40332d3

Browse files
committed
Wrap reload config logic in goroutine
1 parent 0fcefff commit 40332d3

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

main.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,25 @@ func reloadConfig(path string) {
170170
}
171171

172172
func watchConfig(path string) {
173-
watcher, err := fsnotify.NewWatcher()
174-
if err != nil {
175-
log.Error("unable to hot reload configuration: " + err.Error())
176-
return
177-
}
178-
defer func(watcher *fsnotify.Watcher) {
179-
err := watcher.Close()
173+
go func() {
174+
watcher, err := fsnotify.NewWatcher()
180175
if err != nil {
181-
panic(err)
176+
log.Error("unable to hot reload configuration: " + err.Error())
177+
return
182178
}
183-
}(watcher)
179+
defer func() {
180+
err := watcher.Close()
181+
if err != nil {
182+
log.Error("failed to close watcher: " + err.Error())
183+
}
184+
}()
184185

185-
err = watcher.Add(path)
186-
if err != nil {
187-
log.Error("unable to hot reload configuration: " + err.Error())
188-
return
189-
}
186+
err = watcher.Add(path)
187+
if err != nil {
188+
log.Error("unable to hot reload configuration: " + err.Error())
189+
return
190+
}
190191

191-
go func() {
192192
for {
193193
select {
194194
case event, ok := <-watcher.Events:
@@ -207,9 +207,6 @@ func watchConfig(path string) {
207207
}
208208
}
209209
}()
210-
211-
<-make(chan struct{})
212-
213210
}
214211

215212
func main() {
@@ -222,7 +219,7 @@ func main() {
222219
prometheus.MustRegister(promGauge)
223220
http.HandleFunc("/metrics", promMetrics)
224221

225-
var httpBindAddr string = getEnv("BIND", ":8080")
222+
var httpBindAddr = getEnv("BIND", ":8080")
226223
log.Infof("listening on %s", httpBindAddr)
227224
err := http.ListenAndServe(httpBindAddr, nil)
228225
if err != nil {

0 commit comments

Comments
 (0)