]> go.fuhry.dev Git - fsnotify.git/commit
Add AddWith() to pass options, allow controlling Windows buffer size (#521)
authorMartin Tournoij <martin@arp242.net>
Sun, 30 Oct 2022 10:47:55 +0000 (11:47 +0100)
committerGitHub <noreply@github.com>
Sun, 30 Oct 2022 10:47:55 +0000 (11:47 +0100)
commit3d6987fbf52d71bea6ff0eb830f754d61e6c7e52
tree2538513caaf8899b77d072d5624be5830ea79ea3
parentf07448894a45e3879d8d46c9ade8a7b94c9957d3
Add AddWith() to pass options, allow controlling Windows buffer size (#521)

This is similar to Add(), except that you can pass options. Ideally this
should just be Add(), but that's not a compatible change, so we're stuck
with this until we do a v2.

There are quite a few enhancements that depend on *some* way to pass
options; as an example I added WithBufferSize() to control the buffer
size for (see #72) for the Windows backend, because that one is fairly
trivial to implement:

w, err := fsnotify.NewWatcher()
err = w.AddWith("/path", fsnotify.WithBufferSize(65536*4))

Some other options we might want to add:

err = w.AddWith("/path",
fsnotify.WithEvents(fsnotify.Open | fsnotify.Close),  // Filter events
fsnotify.WithPoll(time.Second),                       // Use poll watcher
fsnotify.WithFanotify(),                              // Prefer fanotify on Linux
fsnotify.WithFollowLinks(true),                       // Control symlink follow behaviour
fsnotify.WithDebounce(100*time.Milliseconds),         // Automatically debounce duplicate events
fsnotify.WithRetry(1*time.Second, 1*time.Minute),     // Retry every second if the path disappears for a minute
)

These are just some ideas, nothing fixed here yet. Some of these options
are likely to change once I get around to actually working on it.

This uses "functional options" so we can add more later. Options are
passed to Add() rather than the Watcher itself, so the behaviour can be
modified for every watch, rather than being global. This way you can do
things like watch /nfs-drive with a poll backend, and use the regular OS
backend for ~/dir, without having to create two watchers.

This upgrades fairly nicely to v2 where we rename AddWith() to Add():

err = w.Add("/path",
fsnotify.WithBufferSize(65536*4),
fsnotify.WithEvents(fsnotify.Open | fsnotify.Close))

Folks will just have to s/fsnotify.AddWith/fsnotify.Add/, without having
to change all the option names. Plus having a consistent prefix
autocompletes nicely in editors.

Fixes #72
backend_fen.go
backend_inotify.go
backend_kqueue.go
backend_other.go
backend_windows.go
fsnotify.go
mkdoc.zsh