Skip to content

Commit 959595b

Browse files
committed
Switch to use syncthing/notify instead of fsnotify/fsnotify
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 1159e0f commit 959595b

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ require (
2727
github.com/emirpasic/gods v1.18.1
2828
github.com/ethantkoenig/rupture v1.0.1
2929
github.com/felixge/fgprof v0.9.2
30-
github.com/fsnotify/fsnotify v1.5.4
3130
github.com/gliderlabs/ssh v0.3.4
3231
github.com/go-ap/activitypub v0.0.0-20220615144428-48208c70483b
3332
github.com/go-ap/jsonld v0.0.0-20220615144122-1d862b15410d
@@ -82,6 +81,7 @@ require (
8281
github.com/sergi/go-diff v1.2.0
8382
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
8483
github.com/stretchr/testify v1.7.1
84+
github.com/syncthing/notify v0.0.0-20210616190510-c6b7342338d2
8585
github.com/syndtr/goleveldb v1.0.0
8686
github.com/tstranex/u2f v1.0.0
8787
github.com/unrolled/render v1.4.1
@@ -161,6 +161,7 @@ require (
161161
github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect
162162
github.com/felixge/httpsnoop v1.0.2 // indirect
163163
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
164+
github.com/fsnotify/fsnotify v1.5.4 // indirect
164165
github.com/fullstorydev/grpcurl v1.8.1 // indirect
165166
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
166167
github.com/go-ap/errors v0.0.0-20220615144307-e8bc4a40ae9f // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,8 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT
14741474
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14751475
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
14761476
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
1477+
github.com/syncthing/notify v0.0.0-20210616190510-c6b7342338d2 h1:F4snRP//nIuTTW9LYEzVH4HVwDG9T3M4t8y/2nqMbiY=
1478+
github.com/syncthing/notify v0.0.0-20210616190510-c6b7342338d2/go.mod h1:J0q59IWjLtpRIJulohwqEZvjzwOfTEPp8SVhDJl+y0Y=
14771479
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
14781480
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
14791481
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
@@ -1840,6 +1842,7 @@ golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5h
18401842
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18411843
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18421844
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
1845+
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18431846
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18441847
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18451848
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

modules/watcher/watcher.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/log"
1313
"code.gitea.io/gitea/modules/process"
1414

15-
"github.com/fsnotify/fsnotify"
15+
"github.com/syncthing/notify"
1616
)
1717

1818
type CreateWatcherOpts struct {
@@ -39,61 +39,57 @@ func run(ctx context.Context, desc string, opts *CreateWatcherOpts) {
3939
log.Trace("Watcher loop starting for %s", desc)
4040
defer log.Trace("Watcher loop ended for %s", desc)
4141

42-
watcher, err := fsnotify.NewWatcher()
43-
if err != nil {
44-
log.Error("Unable to create watcher for %s: %v", desc, err)
45-
return
46-
}
42+
// Make the channel buffered to ensure no event is dropped. Notify will drop
43+
// an event if the receiver is not able to keep up the sending pace.
44+
events := make(chan notify.EventInfo, 1)
45+
4746
if err := opts.PathsCallback(func(path, _ string, _ fs.DirEntry, err error) error {
4847
if err != nil && !os.IsNotExist(err) {
4948
return err
5049
}
5150
log.Trace("Watcher: %s watching %q", desc, path)
52-
_ = watcher.Add(path)
51+
if err := notify.Watch(path, events, notify.All); err != nil {
52+
log.Trace("Watcher: %s unable to watch %q: error %v", desc, path, err)
53+
}
5354
return nil
5455
}); err != nil {
5556
log.Error("Unable to create watcher for %s: %v", desc, err)
56-
_ = watcher.Close()
57+
notify.Stop(events)
5758
return
5859
}
5960

6061
// Note we don't call the BetweenCallback here
6162

6263
for {
6364
select {
64-
case event, ok := <-watcher.Events:
65+
case event, ok := <-events:
6566
if !ok {
66-
_ = watcher.Close()
67+
notify.Stop(events)
6768
return
6869
}
70+
6971
log.Debug("Watched file for %s had event: %v", desc, event)
70-
case err, ok := <-watcher.Errors:
71-
if !ok {
72-
_ = watcher.Close()
73-
return
74-
}
75-
log.Error("Error whilst watching files for %s: %v", desc, err)
7672
case <-ctx.Done():
77-
_ = watcher.Close()
73+
notify.Stop(events)
7874
return
7975
}
8076

8177
// Recreate the watcher - only call the BetweenCallback after the new watcher is set-up
82-
_ = watcher.Close()
83-
watcher, err = fsnotify.NewWatcher()
84-
if err != nil {
85-
log.Error("Unable to create watcher for %s: %v", desc, err)
86-
return
87-
}
78+
notify.Stop(events)
79+
events = make(chan notify.EventInfo, 1)
80+
8881
if err := opts.PathsCallback(func(path, _ string, _ fs.DirEntry, err error) error {
8982
if err != nil {
9083
return err
9184
}
92-
_ = watcher.Add(path)
85+
log.Trace("Watcher: %s watching %q", desc, path)
86+
if err := notify.Watch(path, events, notify.All); err != nil {
87+
log.Trace("Watcher: %s unable to watch %q: error %v", desc, path, err)
88+
}
9389
return nil
9490
}); err != nil {
9591
log.Error("Unable to create watcher for %s: %v", desc, err)
96-
_ = watcher.Close()
92+
notify.Stop(events)
9793
return
9894
}
9995

0 commit comments

Comments
 (0)