polling interval

This commit is contained in:
asoseil 2017-10-16 22:21:42 +02:00
parent 6e914d213e
commit 0df1d44abf
2 changed files with 23 additions and 18 deletions

View File

@ -31,12 +31,6 @@ type (
Errors() <-chan error Errors() <-chan error
Events() <-chan fsnotify.Event Events() <-chan fsnotify.Event
} }
filewatcher struct {
// force polling instead event watcher
polling bool
// polling interval express in milliseconds
interval time.Duration
}
// fsNotifyWatcher wraps the fsnotify package to satisfy the FileNotifier interface // fsNotifyWatcher wraps the fsnotify package to satisfy the FileNotifier interface
fsNotifyWatcher struct { fsNotifyWatcher struct {
*fsnotify.Watcher *fsnotify.Watcher
@ -55,24 +49,31 @@ type (
mu sync.Mutex mu sync.Mutex
// closed is used to specify when the poller has already closed // closed is used to specify when the poller has already closed
closed bool closed bool
// polling interval
interval time.Duration
} }
) )
// NewPollingWatcher returns a poll-based file watcher // NewPollingWatcher returns a poll-based file watcher
func PollingWatcher() FileWatcher { func PollingWatcher(interval time.Duration) FileWatcher {
if interval == 0 {
interval = 100 * time.Millisecond
}
return &filePoller{ return &filePoller{
//interval: f.interval * time.Millisecond, interval: interval,
events: make(chan fsnotify.Event), events: make(chan fsnotify.Event),
errors: make(chan error), errors: make(chan error),
} }
} }
// New tries to use an fs-event watcher, and falls back to the poller if there is an error // New tries to use an fs-event watcher, and falls back to the poller if there is an error
func Watcher() (FileWatcher, error) { func Watcher(force bool, interval time.Duration) (FileWatcher, error) {
if w, err := EventWatcher(); err == nil { if !force {
return w, nil if w, err := EventWatcher(); err == nil {
return w, nil
}
} }
return PollingWatcher(), nil return PollingWatcher(interval), nil
} }
// NewEventWatcher returns an fs-event based file watcher // NewEventWatcher returns an fs-event based file watcher
@ -222,7 +223,7 @@ func (w *filePoller) sendEvent(e fsnotify.Event, chClose <-chan struct{}) error
func (w *filePoller) watch(f *os.File, lastFi os.FileInfo, chClose chan struct{}) { func (w *filePoller) watch(f *os.File, lastFi os.FileInfo, chClose chan struct{}) {
defer f.Close() defer f.Close()
for { for {
time.Sleep(200 * time.Millisecond) time.Sleep(w.interval)
select { select {
case <-chClose: case <-chClose:
logrus.Debugf("watch for %s closed", f.Name()) logrus.Debugf("watch for %s closed", f.Name())

View File

@ -10,8 +10,12 @@ import (
"time" "time"
) )
const (
interval = 100 * time.Millisecond
)
func TestPoller_AddRemove(t *testing.T) { func TestPoller_AddRemove(t *testing.T) {
w := PollingWatcher() w := PollingWatcher(interval)
if err := w.Add("no-such-file"); err == nil { if err := w.Add("no-such-file"); err == nil {
t.Fatal("should have gotten error when adding a non-existent file") t.Fatal("should have gotten error when adding a non-existent file")
@ -39,7 +43,7 @@ func TestPoller_Event(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skip("No chmod on Windows") t.Skip("No chmod on Windows")
} }
w := PollingWatcher() w := PollingWatcher(interval)
f, err := ioutil.TempFile("", "test-poller") f, err := ioutil.TempFile("", "test-poller")
if err != nil { if err != nil {
@ -83,7 +87,7 @@ func TestPoller_Event(t *testing.T) {
} }
func TestPoller_Close(t *testing.T) { func TestPoller_Close(t *testing.T) {
w := PollingWatcher() w := PollingWatcher(interval)
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
t.Fatal(err) t.Fatal(err)
} }