optimize polling watcher using stat check
does not send 'changed' events if the last modified time hasn't changed.
This commit is contained in:
parent
adcb2e389d
commit
dfe0ea8ba5
24
watch.go
24
watch.go
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue