Fix missed file update if consumer of events is busy, e.g. waiting on a lock

This commit is contained in:
Daulet Zhanguzin 2021-04-10 19:56:05 +01:00
parent 5d9283be34
commit 936a0815a0

View File

@ -26,10 +26,12 @@ func (d *File) Events(ctx context.Context) <-chan struct{} {
res := make(chan struct{})
// no need to queue multiple events
trySubmit := func(ch chan struct{}) {
trySubmit := func(ch chan struct{}) bool {
select {
case ch <- struct{}{}:
return true
default:
return false
}
}
@ -50,8 +52,9 @@ func (d *File) Events(ctx context.Context) <-chan struct{} {
}
log.Printf("[DEBUG] file %s changed, %s -> %s", d.FileName,
lastModif.Format(time.RFC3339Nano), fi.ModTime().Format(time.RFC3339Nano))
lastModif = fi.ModTime()
trySubmit(res)
if trySubmit(res) {
lastModif = fi.ModTime()
}
}
case <-ctx.Done():
close(res)