Skip to content

Commit b3615d6

Browse files
nicksndeloof
authored andcommitted
watch: increase the windows watch i/o buffer (docker#3620)
fixes tilt-dev/tilt#3556
1 parent 1a1d170 commit b3615d6

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

pkg/watch/notify.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package watch
33
import (
44
"expvar"
55
"fmt"
6+
"os"
67
"path/filepath"
8+
"runtime"
9+
"strconv"
10+
"strings"
711

812
"github.com/tilt-dev/tilt/pkg/logger"
913
)
@@ -67,3 +71,22 @@ var _ PathMatcher = EmptyMatcher{}
6771
func NewWatcher(paths []string, ignore PathMatcher, l logger.Logger) (Notify, error) {
6872
return newWatcher(paths, ignore, l)
6973
}
74+
75+
const WindowsBufferSizeEnvVar = "TILT_WATCH_WINDOWS_BUFFER_SIZE"
76+
77+
const defaultBufferSize int = 65536
78+
79+
func DesiredWindowsBufferSize() int {
80+
envVar := os.Getenv(WindowsBufferSizeEnvVar)
81+
if envVar != "" {
82+
size, err := strconv.Atoi(envVar)
83+
if err != nil {
84+
return size
85+
}
86+
}
87+
return defaultBufferSize
88+
}
89+
90+
func IsWindowsShortReadError(err error) bool {
91+
return runtime.GOOS == "windows" && err != nil && strings.Contains(err.Error(), "short read")
92+
}

pkg/watch/watcher_naive.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ func (d *naiveNotify) Errors() chan error {
136136
func (d *naiveNotify) loop() {
137137
defer close(d.wrappedEvents)
138138
for e := range d.events {
139+
// The Windows fsnotify event stream sometimes gets events with empty names
140+
// that are also sent to the error stream. Hmmmm...
141+
if e.Name == "" {
142+
continue
143+
}
144+
139145
if e.Op&fsnotify.Create != fsnotify.Create {
140146
if d.shouldNotify(e.Name) {
141147
d.wrappedEvents <- FileEvent{e.Name}
@@ -251,6 +257,7 @@ func newWatcher(paths []string, ignore PathMatcher, l logger.Logger) (*naiveNoti
251257
if err != nil {
252258
return nil, err
253259
}
260+
MaybeIncreaseBufferSize(fsw)
254261

255262
err = fsw.SetRecursive()
256263
isWatcherRecursive := err == nil

pkg/watch/watcher_nonwin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build !windows
2+
3+
package watch
4+
5+
import "github.com/tilt-dev/fsnotify"
6+
7+
func MaybeIncreaseBufferSize(w *fsnotify.Watcher) {
8+
// Not needed on non-windows
9+
}

pkg/watch/watcher_windows.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// +build windows
2+
3+
package watch
4+
5+
import (
6+
"github.com/tilt-dev/fsnotify"
7+
)
8+
9+
// TODO(nick): I think the ideal API would be to automatically increase the
10+
// size of the buffer when we exceed capacity. But this gets messy,
11+
// because each time we get a short read error, we need to invalidate
12+
// everything we know about the currently changed files. So for now,
13+
// we just provide a way for people to increase the buffer ourselves.
14+
//
15+
// It might also pay to be clever about sizing the buffer
16+
// relative the number of files in the directory we're watching.
17+
func MaybeIncreaseBufferSize(w *fsnotify.Watcher) {
18+
w.SetBufferSize(DesiredWindowsBufferSize())
19+
}

0 commit comments

Comments
 (0)