-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpoller.go
79 lines (64 loc) · 1.56 KB
/
poller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package filenotify
import (
"time"
"github.com/fsnotify/fsnotify"
"github.com/radovskyb/watcher"
)
// watchWaitTime is the time to wait between file poll loops
const watchWaitTime = 200 * time.Millisecond
// filePoller is used to poll files for changes, especially in cases where fsnotify
// can't be run (e.g. when inotify handles are exhausted)
// filePoller satisfies the FileWatcher interface
type filePoller struct {
wr *watcher.Watcher
// events is the channel to listen to for watch events.
events chan fsnotify.Event
// errors is the channel to listen to for watch errors.
errors chan error
}
// loop captures and transform radovskyb's watcher into fsnotify events.
func (w *filePoller) loop() {
for {
select {
case event := <-w.wr.Event:
var op fsnotify.Op
switch event.Op {
case watcher.Create:
op = fsnotify.Create
case watcher.Rename:
fallthrough
case watcher.Move:
op = fsnotify.Rename
default:
continue
}
w.events <- fsnotify.Event{
Op: op,
Name: event.Path,
}
case err := <-w.wr.Error:
w.errors <- err
case <-w.wr.Closed:
return
}
}
}
func (w *filePoller) Add(name string) error {
return w.wr.Add(name)
}
func (w *filePoller) Remove(name string) error {
return w.wr.Remove(name)
}
// Events returns the event channel.
func (w *filePoller) Events() <-chan fsnotify.Event {
return w.events
}
// Errors returns the errors channel.
func (w *filePoller) Errors() <-chan error {
return w.errors
}
// Close closes the poller.
func (w *filePoller) Close() error {
w.wr.Close()
return nil
}