optimize polling watcher using stat check

does not send 'changed' events if the last modified time hasn't changed.
This commit is contained in:
Sridhar Ratnakumar 2012-10-12 18:12:49 -07:00
parent adcb2e389d
commit dfe0ea8ba5
1 changed files with 21 additions and 3 deletions

View File

@ -107,15 +107,33 @@ func (fw *PollingFileWatcher) ChangeEvents() chan bool {
stop := make(chan bool) stop := make(chan bool)
every2Seconds := time.Tick(2 * time.Second) every2Seconds := time.Tick(2 * time.Second)
var prevModTime time.Time
go func() { go func() {
for { for {
time.Sleep(250 * time.Millisecond)
select { select {
case ch <- true:
case <-stop: case <-stop:
return return
default: default:
} }
time.Sleep(250 * time.Millisecond)
fi, err := os.Stat(fw.Filename)
if err != nil {
if os.IsNotExist(err) {
// below goroutine (every2Seconds) will catch up eventually and stop us.
continue
}
panic(err)
}
modTime := fi.ModTime()
if modTime != prevModTime {
prevModTime = modTime
select {
case ch <- true:
default:
}
}
} }
}() }()
@ -123,7 +141,7 @@ func (fw *PollingFileWatcher) ChangeEvents() chan bool {
for { for {
select { select {
case <-every2Seconds: case <-every2Seconds:
// NOTE: not using file descriptor. // XXX: not using file descriptor as per contract.
if _, err := os.Stat(fw.Filename); os.IsNotExist(err) { if _, err := os.Stat(fw.Filename); os.IsNotExist(err) {
stop <- true stop <- true
close(ch) close(ch)