@@ -24,15 +24,17 @@ type eventInfo struct {
24
24
}
25
25
26
26
type fsnWatcher struct {
27
- watcher * fsnotify.Watcher
28
- eventMap map [string ]eventInfo
27
+ watcher * fsnotify.Watcher
29
28
30
29
directoryCount int
31
30
32
- Logger * slog.Logger
33
- OnlyWatchSuffixes []string
34
- IgnoreSuffixes []string
35
- ExcludeDirs map [string ]struct {}
31
+ Logger * slog.Logger
32
+ OnlySuffixes []string
33
+ IgnoreSuffixes []string
34
+ ExcludeDirs map [string ]struct {}
35
+ watchingDirs map [string ]struct {}
36
+
37
+ cooldownDuration time.Duration
36
38
}
37
39
38
40
type Event fsnotify.Event
@@ -71,13 +73,13 @@ func (f fsnWatcher) ignoreEvent(event fsnotify.Event) (ignore bool, reason strin
71
73
}
72
74
}
73
75
74
- if len (f .OnlyWatchSuffixes ) == 0 {
76
+ if len (f .OnlySuffixes ) == 0 {
75
77
return false , "event not in ignore list, and only-watch list is also empty"
76
78
}
77
79
78
80
matched := false
79
- for _ , suffix := range f .OnlyWatchSuffixes {
80
- f .Logger .Debug ("[only-watch- suffix] suffix: (%s), event.name: %s" , suffix , event .Name )
81
+ for _ , suffix := range f .OnlySuffixes {
82
+ f .Logger .Debug (fmt . Sprintf ( "[only-suffix] suffix: (%s), event.name: %s" , suffix , event .Name ) )
81
83
if strings .HasSuffix (event .Name , suffix ) {
82
84
matched = true
83
85
break
@@ -123,8 +125,8 @@ func (f *fsnWatcher) WatchEvents(watcherFunc func(event Event, fp string) error)
123
125
// eInfo.Counter += 1
124
126
// f.eventMap[event.Name] = eInfo
125
127
126
- if time .Since (lastProcessingTime ) < 100 * time . Millisecond {
127
- // f.Logger.Debug("too many events under 100ms , ignoring...", "counter ", eInfo.Counter )
128
+ if time .Since (lastProcessingTime ) < f . cooldownDuration {
129
+ f .Logger .Debug (fmt . Sprintf ( "too many events under %s , ignoring..." , f . cooldownDuration . String ()), "event.name " , event . Name )
128
130
continue
129
131
}
130
132
@@ -185,6 +187,11 @@ func (f *fsnWatcher) RecursiveAdd(dirs ...string) error {
185
187
}
186
188
187
189
func (f * fsnWatcher ) addToWatchList (dir string ) error {
190
+ if _ , ok := f .watchingDirs [dir ]; ok {
191
+ return nil
192
+ }
193
+
194
+ f .watchingDirs [dir ] = struct {}{}
188
195
if err := f .watcher .Add (dir ); err != nil {
189
196
f .Logger .Error ("failed to add directory" , "dir" , dir , "err" , err )
190
197
return err
@@ -199,11 +206,16 @@ func (f *fsnWatcher) Close() error {
199
206
}
200
207
201
208
type WatcherArgs struct {
202
- Logger * slog.Logger
203
- OnlyWatchSuffixes []string
204
- IgnoreSuffixes []string
205
- ExcludeDirs []string
209
+ Logger * slog.Logger
210
+
211
+ WatchDirs []string
212
+ OnlySuffixes []string
213
+ IgnoreSuffixes []string
214
+ ExcludeDirs []string
215
+
206
216
UseDefaultIgnoreList bool
217
+
218
+ CooldownDuration * time.Duration
207
219
}
208
220
209
221
func NewWatcher (args WatcherArgs ) (Watcher , error ) {
@@ -215,6 +227,12 @@ func NewWatcher(args WatcherArgs) (Watcher, error) {
215
227
args .ExcludeDirs = append (args .ExcludeDirs , globalExcludeDirs ... )
216
228
}
217
229
230
+ cooldown := 500 * time .Millisecond
231
+
232
+ if args .CooldownDuration != nil {
233
+ cooldown = * args .CooldownDuration
234
+ }
235
+
218
236
excludeDirs := map [string ]struct {}{}
219
237
for _ , dir := range args .ExcludeDirs {
220
238
excludeDirs [dir ] = struct {}{}
@@ -225,11 +243,25 @@ func NewWatcher(args WatcherArgs) (Watcher, error) {
225
243
args .Logger .Error ("failed to create watcher, got" , "err" , err )
226
244
return nil , err
227
245
}
228
- return & fsnWatcher {
229
- watcher : watcher ,
230
- Logger : args .Logger ,
231
- ExcludeDirs : excludeDirs ,
232
- IgnoreSuffixes : args .IgnoreSuffixes ,
233
- OnlyWatchSuffixes : args .OnlyWatchSuffixes ,
234
- }, nil
246
+
247
+ if args .WatchDirs == nil {
248
+ dir , _ := os .Getwd ()
249
+ args .WatchDirs = append (args .WatchDirs , dir )
250
+ }
251
+
252
+ fsw := & fsnWatcher {
253
+ watcher : watcher ,
254
+ Logger : args .Logger ,
255
+ ExcludeDirs : excludeDirs ,
256
+ IgnoreSuffixes : args .IgnoreSuffixes ,
257
+ OnlySuffixes : args .OnlySuffixes ,
258
+ cooldownDuration : cooldown ,
259
+ watchingDirs : make (map [string ]struct {}),
260
+ }
261
+
262
+ if err := fsw .RecursiveAdd (args .WatchDirs ... ); err != nil {
263
+ return nil , err
264
+ }
265
+
266
+ return fsw , nil
235
267
}
0 commit comments