Skip to content

Commit b5ccea7

Browse files
Dan Millerndeloof
authored andcommitted
watch: record num watches in expvars (docker#1795)
1 parent 4755189 commit b5ccea7

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pkg/watch/watcher_naive.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package watch
44

55
import (
6+
"expvar"
67
"fmt"
78
"os"
89
"path/filepath"
@@ -34,6 +35,10 @@ type naiveNotify struct {
3435
notifyList map[string]bool
3536
}
3637

38+
var (
39+
numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
40+
)
41+
3742
func (d *naiveNotify) Add(name string) error {
3843
fi, err := os.Stat(name)
3944
if err != nil && !os.IsNotExist(err) {
@@ -52,7 +57,7 @@ func (d *naiveNotify) Add(name string) error {
5257
return errors.Wrapf(err, "notify.Add(%q)", name)
5358
}
5459
} else {
55-
err = d.watcher.Add(filepath.Dir(name))
60+
err = d.add(filepath.Dir(name))
5661
if err != nil {
5762
return errors.Wrapf(err, "notify.Add(%q)", filepath.Dir(name))
5863
}
@@ -74,7 +79,7 @@ func (d *naiveNotify) watchRecursively(dir string) error {
7479
if !mode.IsDir() {
7580
return nil
7681
}
77-
err = d.watcher.Add(path)
82+
err = d.add(path)
7883
if err != nil {
7984
if os.IsNotExist(err) {
8085
return nil
@@ -100,7 +105,7 @@ func (d *naiveNotify) watchAncestorOfMissingPath(path string) error {
100105
return d.watchAncestorOfMissingPath(parent)
101106
}
102107

103-
return d.watcher.Add(path)
108+
return d.add(path)
104109
}
105110

106111
func (d *naiveNotify) Close() error {
@@ -151,7 +156,7 @@ func (d *naiveNotify) loop() {
151156
}
152157
}
153158
if shouldWatch {
154-
err := d.watcher.Add(path)
159+
err := d.add(path)
155160
if err != nil && !os.IsNotExist(err) {
156161
d.log.Infof("Error watching path %s: %s", e.Name, err)
157162
}
@@ -179,6 +184,15 @@ func (d *naiveNotify) shouldNotify(path string) bool {
179184
return false
180185
}
181186

187+
func (d *naiveNotify) add(path string) error {
188+
err := d.watcher.Add(path)
189+
if err != nil {
190+
return err
191+
}
192+
numberOfWatches.Add(1)
193+
return nil
194+
}
195+
182196
func NewWatcher(l logger.Logger) (*naiveNotify, error) {
183197
fsw, err := fsnotify.NewWatcher()
184198
if err != nil {

0 commit comments

Comments
 (0)