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