Skip to content

Commit

Permalink
Deduplicate frequent events
Browse files Browse the repository at this point in the history
  • Loading branch information
kloyan committed Jan 1, 2023
1 parent 3b05339 commit 0ff9352
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/kloyan/canaryfs
module github.com/kloyan/canary-fswatcher

go 1.19

Expand Down
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ import (
"github.com/fsnotify/fsnotify"
)

var backoffPolicy = []time.Duration{0, 250, 500, 1_000}
var backoffPolicy = []time.Duration{0, 250, 500, 1_000, 2_000, 4_000}

type config struct {
watcher *fsnotify.Watcher
tokenUrl string
waitFor time.Duration
}

func main() {
var path, tokenUrl string
var waitFor time.Duration

flag.StringVar(&path, "path", "/tmp", "File or directory to monitor for changes")
flag.StringVar(&tokenUrl, "token-url", "", "URL canary token to be triggered on events")
flag.DurationVar(&waitFor, "wait-for", 1*time.Second, "Time to wait for new events to arrive before ping")
flag.Parse()

if _, err := os.Stat(path); os.IsNotExist(err) {
Expand All @@ -44,13 +48,15 @@ func main() {
log.Panicf("could not monitor path: %v", err)
}

c := config{watcher: watcher, tokenUrl: tokenUrl}
c := config{watcher: watcher, tokenUrl: tokenUrl, waitFor: waitFor}
if err := c.startWatchLoop(watcher); err != nil {
log.Panicf("watch loop failed: %v", err)
}
}

func (c *config) startWatchLoop(watcher *fsnotify.Watcher) error {
timers := map[string]*time.Timer{}

for {
select {
case err, ok := <-watcher.Errors:
Expand All @@ -66,12 +72,16 @@ func (c *config) startWatchLoop(watcher *fsnotify.Watcher) error {
return nil
}

// todo: implement dedup
// Ignore CHMOD events as they are too frequent
if e.Has(fsnotify.Chmod) {
continue
}

c.pingWithRetry(e)
if t, ok := timers[e.Name]; ok {
t.Stop()
}

timers[e.Name] = time.AfterFunc(c.waitFor, func() { c.pingWithRetry(e) })
}
}
}
Expand Down

0 comments on commit 0ff9352

Please sign in to comment.