-
-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support listening events, add unsupported events
This adds the ability to only listen for some event types. If you're only interested in Created events and you're getting a lot of Write events then you're just wasting CPU cycles This also adds the ability listen on extra unportable event types; since this is so related I figured I might as well do both. Ideally we want to 1) make it very very obvious you're doing something unportable, and 2) make it reasonably easy "fallback" for platforms where this isn't supported. Unportable events start with "Unportable", which should document their unportabilitiness. Also add a new Supports(Op) method, which should make adding fallback logic relatively painless. For example, to use CloseWrite where supported, but falling back to Write when it's not: var op fsnotify.Op if w.Supports(fsnotify.UnportableCloseWrite) { op |= fsnotify.UnportableCloseWrite } else { op |= fsnotify.Create | fsnotify.Write } w.AddWith("/tmp", fsnotify.WithEvents(op)) And then you can deal with this in the write loop. There's a full example in cmd/fsnotify/closewrite.go TODO: need to write tests for this, update all platforms. Fixes 7 Updates 519
- Loading branch information
Showing
14 changed files
with
243 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"math" | ||
"sync" | ||
"time" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
) | ||
|
||
func closeWrite(paths ...string) { | ||
if len(paths) < 1 { | ||
exit("must specify at least one path to watch") | ||
} | ||
|
||
w, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
exit("creating a new watcher: %s", err) | ||
} | ||
defer w.Close() | ||
|
||
var ( | ||
op fsnotify.Op | ||
cw = w.Supports(fsnotify.UnportableCloseWrite) | ||
) | ||
if cw { | ||
op |= fsnotify.UnportableCloseWrite | ||
} else { | ||
op |= fsnotify.Create | fsnotify.Write | ||
} | ||
|
||
go closeWriteLoop(w, cw) | ||
|
||
for _, p := range paths { | ||
err := w.AddWith(p, fsnotify.WithEvents(op)) | ||
if err != nil { | ||
exit("%q: %s", p, err) | ||
} | ||
} | ||
|
||
printTime("ready; press ^C to exit") | ||
<-make(chan struct{}) | ||
} | ||
|
||
func closeWriteLoop(w *fsnotify.Watcher, cw bool) { | ||
var ( | ||
waitFor = 100 * time.Millisecond | ||
mu sync.Mutex | ||
timers = make(map[string]*time.Timer) | ||
) | ||
for { | ||
select { | ||
case err, ok := <-w.Errors: | ||
if !ok { | ||
return | ||
} | ||
panic(err) | ||
case e, ok := <-w.Events: | ||
if !ok { | ||
return | ||
} | ||
|
||
// CloseWrite is supported: easy case. | ||
if cw { | ||
if e.Has(fsnotify.UnportableCloseWrite) { | ||
printTime(e.String()) | ||
} | ||
continue | ||
} | ||
|
||
// Get timer. | ||
mu.Lock() | ||
t, ok := timers[e.Name] | ||
mu.Unlock() | ||
|
||
// No timer yet, so create one. | ||
if !ok { | ||
t = time.AfterFunc(math.MaxInt64, func() { | ||
printTime(e.String()) | ||
mu.Lock() | ||
delete(timers, e.Name) | ||
mu.Unlock() | ||
}) | ||
t.Stop() | ||
|
||
mu.Lock() | ||
timers[e.Name] = t | ||
mu.Unlock() | ||
} | ||
|
||
// Reset the timer for this path, so it will start from 100ms again. | ||
t.Reset(waitFor) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.