|
|
@@ -5,18 +5,27 @@ import (
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
"path/filepath"
|
|
|
"time"
|
|
|
+
|
|
|
+ "sync"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- debounceWriteLog map[string]time.Time
|
|
|
+ debounceWriteLog map[string]*FsNotifyLogEntry
|
|
|
+
|
|
|
+ debounceWriteLogMutex = sync.Mutex{}
|
|
|
)
|
|
|
|
|
|
func init() {
|
|
|
- debounceWriteLog = make(map[string]time.Time)
|
|
|
+ debounceWriteLog = make(map[string]*FsNotifyLogEntry)
|
|
|
+}
|
|
|
+
|
|
|
+type FsNotifyLogEntry struct {
|
|
|
+ callbackWrapper *time.Timer
|
|
|
+ callbackComplete bool
|
|
|
}
|
|
|
|
|
|
const (
|
|
|
- debounceDelay = 300
|
|
|
+ debounceDelay = 300 * time.Millisecond
|
|
|
)
|
|
|
|
|
|
type watchContext struct {
|
|
|
@@ -117,22 +126,44 @@ func consumeEvent(ok bool, ctx *watchContext) bool {
|
|
|
|
|
|
func consumeRelevantEvents(ctx *watchContext) {
|
|
|
if ctx.event.Has(ctx.interestedEvent) {
|
|
|
- log.Debugf("fsnotify write event: %v", ctx.event)
|
|
|
+ log.Debugf("fsnotify event relevant: %v", ctx.event)
|
|
|
|
|
|
processDebounce(ctx)
|
|
|
} else {
|
|
|
- log.Debugf("fsnotify irrelevant event on file %v", ctx.event)
|
|
|
+ log.Debugf("fsnotify event irrelevant: %v", ctx.event)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func processDebounce(ctx *watchContext) {
|
|
|
- entry, found := debounceWriteLog[ctx.filename]
|
|
|
+ debounceWriteLogMutex.Lock()
|
|
|
|
|
|
- if !found || time.Since(entry) < debounceDelay {
|
|
|
- debounceWriteLog[ctx.filename] = time.Now()
|
|
|
+ logEntry, found := debounceWriteLog[ctx.filename]
|
|
|
|
|
|
- ctx.callback(ctx.event.Name)
|
|
|
+ if !found {
|
|
|
+ logEntry = &FsNotifyLogEntry{
|
|
|
+ callbackComplete: false,
|
|
|
+ callbackWrapper: nil,
|
|
|
+ }
|
|
|
+
|
|
|
+ debounceWriteLog[ctx.filename] = logEntry
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Infof("fsnotify event %+v", logEntry)
|
|
|
+
|
|
|
+ if logEntry.callbackComplete || logEntry.callbackWrapper == nil {
|
|
|
+ log.Debugf("fsnotify event callback queued within debounce delay: %v", ctx.filename)
|
|
|
+
|
|
|
+ logEntry.callbackComplete = false
|
|
|
+ logEntry.callbackWrapper = time.AfterFunc(debounceDelay, func() {
|
|
|
+ log.Debugf("fsnotify event callback being fired: %v", ctx.filename)
|
|
|
+
|
|
|
+ ctx.callback(ctx.event.Name)
|
|
|
+
|
|
|
+ logEntry.callbackComplete = true
|
|
|
+ })
|
|
|
} else {
|
|
|
- log.Debugf("Supressing write event because it's within the debounce delay: %v", ctx.filename)
|
|
|
+ log.Debugf("fsnotify event suppressed because it's within the debounce delay: %v", ctx.filename)
|
|
|
}
|
|
|
+
|
|
|
+ debounceWriteLogMutex.Unlock()
|
|
|
}
|