Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AddWith() to pass options, allow controlling Windows buffer size #521

Merged
merged 1 commit into from Oct 30, 2022

Commits on Oct 30, 2022

  1. Add AddWith() to pass options, allow controlling Windows buffer size

    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
    arp242 committed Oct 30, 2022
    Configuration menu
    Copy the full SHA
    c6d7b47 View commit details
    Browse the repository at this point in the history