From 8a0b506ea8d7e2f2abfee44e01dfc1efd6ee7a72 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Fri, 28 Aug 2015 12:15:46 +0200 Subject: [PATCH] Fix draining of file watcher events --- retrieval/discovery/file.go | 45 ++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/retrieval/discovery/file.go b/retrieval/discovery/file.go index b29b14e7e..756ccca44 100644 --- a/retrieval/discovery/file.go +++ b/retrieval/discovery/file.go @@ -160,6 +160,30 @@ func (fd *FileDiscovery) Run(ch chan<- *config.TargetGroup, done <-chan struct{} } } +// stop shuts down the file watcher. +func (fd *FileDiscovery) stop() { + log.Debugf("Stopping file discovery for %s...", fd.paths) + + done := make(chan struct{}) + defer close(done) + + // Closing the watcher will deadlock unless all events and errors are drained. + go func() { + for { + select { + case <-fd.watcher.Errors: + case <-fd.watcher.Events: + // Drain all events and errors. + case <-done: + return + } + } + }() + fd.watcher.Close() + + log.Debugf("File discovery for %s stopped.", fd.paths) +} + // refresh reads all files matching the discovery's patterns and sends the respective // updated target groups through the channel. func (fd *FileDiscovery) refresh(ch chan<- *config.TargetGroup) { @@ -196,27 +220,6 @@ func fileSource(filename string, i int) string { return fmt.Sprintf("%s:%d", filename, i) } -// stop shuts down the file watcher. -func (fd *FileDiscovery) stop() { - log.Debugf("Stopping file discovery for %s...", fd.paths) - - // Closing the watcher will deadlock unless all events and errors are drained. - go func() { - for { - select { - case <-fd.watcher.Errors: - case <-fd.watcher.Events: - // Drain all events and errors. - default: - return - } - } - }() - fd.watcher.Close() - - log.Debugf("File discovery for %s stopped.", fd.paths) -} - // readFile reads a JSON or YAML list of targets groups from the file, depending on its // file extension. It returns full configuration target groups. func readFile(filename string) ([]*config.TargetGroup, error) {