recwatch: Close the ConfigWatch properly

This improves the shutdown process so that there is no change of
blocking if the sender runs close without having emptied the channel.
This commit is contained in:
James Shubin
2017-02-22 17:37:07 -05:00
parent 49594b8435
commit e5bb8d7992

View File

@@ -59,15 +59,23 @@ func (obj *ConfigWatcher) Add(file ...string) {
ch := obj.ConfigWatch(file[0]) ch := obj.ConfigWatch(file[0])
for { for {
select { select {
case e := <-ch: case e, ok := <-ch:
if !ok { // channel closed
return
}
if e != nil { if e != nil {
obj.errorchan <- e obj.errorchan <- e
return return
} }
obj.ch <- file[0] select {
case obj.ch <- file[0]: // send on channel
case <-obj.closechan:
return // never mind, close early!
}
continue continue
case <-obj.closechan: // not needed, closes via ConfigWatch() chan close
return //case <-obj.closechan:
// return
} }
} }
}() }()
@@ -126,7 +134,12 @@ func (obj *ConfigWatcher) ConfigWatch(file string) chan error {
close(ch) close(ch)
return return
} }
ch <- nil // send event! select {
case ch <- nil: // send event!
case <-obj.closechan:
close(ch)
return
}
} }
} }
//close(ch) //close(ch)