decreased complexity watch

This commit is contained in:
asoseil 2017-11-11 23:25:27 +01:00
parent 5002abf479
commit b98f93706e
1 changed files with 8 additions and 10 deletions

View File

@ -227,15 +227,16 @@ func (w *filePoller) watch(f *os.File, lastFi os.FileInfo, chClose chan struct{}
defer f.Close()
for {
time.Sleep(w.interval)
fi, err := os.Stat(f.Name())
select {
case <-chClose:
logrus.Debugf("watch for %s closed", f.Name())
return
case err != nil:
if lastFi == nil {
continue
default:
}
fi, err := os.Stat(f.Name())
switch {
case err != nil && lastFi != nil:
// If it doesn't exist at this point, it must have been removed
// no need to send the error here since this is a valid operation
if os.IsNotExist(err) {
@ -243,12 +244,10 @@ func (w *filePoller) watch(f *os.File, lastFi os.FileInfo, chClose chan struct{}
return
}
lastFi = nil
continue
}
// at this point, send the error
if err := w.sendErr(err, chClose); err != nil {
w.sendErr(err, chClose)
return
}
case lastFi == nil:
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Create, Name: f.Name()}, chClose); err != nil {
return
@ -264,7 +263,6 @@ func (w *filePoller) watch(f *os.File, lastFi os.FileInfo, chClose chan struct{}
return
}
lastFi = fi
continue
}
}
}