From 9fd2f9eabe93713c0e8fb7dd094e77109db84265 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 11:46:12 +0530 Subject: feat(watcher): introduce filesystem watcher abstraction --- internal/watcher/config.go | 11 +++++++++++ internal/watcher/doc.go | 7 +++++++ internal/watcher/watcher.go | 21 +++++++++++++++++++++ internal/watcher/watcher_test.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 internal/watcher/config.go create mode 100644 internal/watcher/doc.go create mode 100644 internal/watcher/watcher.go create mode 100644 internal/watcher/watcher_test.go (limited to 'internal/watcher') diff --git a/internal/watcher/config.go b/internal/watcher/config.go new file mode 100644 index 0000000..ad12d0c --- /dev/null +++ b/internal/watcher/config.go @@ -0,0 +1,11 @@ +package watcher + +import "time" + +// Configures a filesystem watcher +type Config struct { + Roots []string + + Debounce time.Duration + OnChange func() +} diff --git a/internal/watcher/doc.go b/internal/watcher/doc.go new file mode 100644 index 0000000..95801aa --- /dev/null +++ b/internal/watcher/doc.go @@ -0,0 +1,7 @@ +// Package watcher watches configured directories for filesystem changes. +// +// The package is intentionally unaware of galleries, thumbnails and HTTP. +// +// Its sole responsibility is detecting filesystem changes and notifying +// the caller through a callback. +package watcher diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go new file mode 100644 index 0000000..5465554 --- /dev/null +++ b/internal/watcher/watcher.go @@ -0,0 +1,21 @@ +package watcher + +import "context" + +type Watcher struct { + config Config +} + +func New(config Config) *Watcher { + return &Watcher{ + config: config, + } +} + +// Run starts the watcher. +// +// Placeholder implementation. +func (w *Watcher) Run(ctx context.Context) error { + <-ctx.Done() + return nil +} diff --git a/internal/watcher/watcher_test.go b/internal/watcher/watcher_test.go new file mode 100644 index 0000000..53c0baa --- /dev/null +++ b/internal/watcher/watcher_test.go @@ -0,0 +1,31 @@ +package watcher + +import ( + "context" + "testing" + "time" +) + +func TestRunReturnsOnContextCancellation(t *testing.T) { + w := New(Config{}) + + ctx, cancel := context.WithCancel(context.Background()) + + done := make(chan error, 1) + + go func() { + done <- w.Run(ctx) + }() + + cancel() + + select { + case err := <-done: + if err != nil { + t.Fatalf("expected nil, got %v", err) + } + + case <-time.After(time.Second): + t.Fatal("watcher did not exit") + } +} -- cgit v1.2.3 From b336f4a1328e24ac77a1cfee8c52b18caa555620 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 12:03:34 +0530 Subject: feat(watcher): watch configured directories --- internal/watcher/config.go | 2 +- internal/watcher/fsnotify.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ internal/watcher/watcher.go | 10 ------- 3 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 internal/watcher/fsnotify.go (limited to 'internal/watcher') diff --git a/internal/watcher/config.go b/internal/watcher/config.go index ad12d0c..a59474f 100644 --- a/internal/watcher/config.go +++ b/internal/watcher/config.go @@ -2,7 +2,7 @@ package watcher import "time" -// Configures a filesystem watcher +// Config configures a filesystem watcher type Config struct { Roots []string diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go new file mode 100644 index 0000000..e051c52 --- /dev/null +++ b/internal/watcher/fsnotify.go @@ -0,0 +1,62 @@ +package watcher + +import ( + "context" + "fmt" + "io/fs" + "log" + "path/filepath" + + "github.com/fsnotify/fsnotify" +) + +// Run starts watching the configured roots. +// +// Filesystem events are currently logged for debugging. +// Future commits will debounce these events and invoke OnChange. +func (w *Watcher) Run(ctx context.Context) error { + fswatcher, err := fsnotify.NewWatcher() + if err != nil { + return fmt.Errorf("create watcher: %w", err) + } + defer func() { + _ = fswatcher.Close() + }() + + for _, root := range w.config.Roots { + if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if !d.IsDir() { + return nil + } + + return fswatcher.Add(path) + }); err != nil { + return err + } + } + + for { + select { + case <-ctx.Done(): + return nil + + case event, ok := <-fswatcher.Events: + if !ok { + return nil + } + + log.Printf("watcher: %v %s", event.Op, event.Name) + + case err, ok := <-fswatcher.Errors: + if !ok { + return nil + } + + log.Printf("watcher error: %v", err) + } + } +} diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 5465554..1ca8e37 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -1,7 +1,5 @@ package watcher -import "context" - type Watcher struct { config Config } @@ -11,11 +9,3 @@ func New(config Config) *Watcher { config: config, } } - -// Run starts the watcher. -// -// Placeholder implementation. -func (w *Watcher) Run(ctx context.Context) error { - <-ctx.Done() - return nil -} -- cgit v1.2.3 From d2c0c970f690d52ee3675f377a5eebc22f84afab Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 12:35:51 +0530 Subject: feat(watcher): watch newly created directories --- internal/watcher/fsnotify.go | 32 +++++++++++++++++++------------- internal/watcher/watch.go | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 internal/watcher/watch.go (limited to 'internal/watcher') diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go index e051c52..7251907 100644 --- a/internal/watcher/fsnotify.go +++ b/internal/watcher/fsnotify.go @@ -3,9 +3,8 @@ package watcher import ( "context" "fmt" - "io/fs" "log" - "path/filepath" + "os" "github.com/fsnotify/fsnotify" ) @@ -24,17 +23,7 @@ func (w *Watcher) Run(ctx context.Context) error { }() for _, root := range w.config.Roots { - if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - - if !d.IsDir() { - return nil - } - - return fswatcher.Add(path) - }); err != nil { + if err := watchTree(fswatcher, root); err != nil { return err } } @@ -51,6 +40,23 @@ func (w *Watcher) Run(ctx context.Context) error { log.Printf("watcher: %v %s", event.Op, event.Name) + if !event.Has(fsnotify.Create) { + continue + } + + info, err := os.Stat(event.Name) + if err != nil { + continue + } + + if !info.IsDir() { + continue + } + + if err := watchTree(fswatcher, event.Name); err != nil { + log.Printf("watch new directory: %v", err) + } + case err, ok := <-fswatcher.Errors: if !ok { return nil diff --git a/internal/watcher/watch.go b/internal/watcher/watch.go new file mode 100644 index 0000000..95146ce --- /dev/null +++ b/internal/watcher/watch.go @@ -0,0 +1,26 @@ +package watcher + +import ( + "io/fs" + "path/filepath" + + "github.com/fsnotify/fsnotify" +) + +// watchTree recursively watches root and all of its subdirectories. +func watchTree( + w *fsnotify.Watcher, + root string, +) error { + return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if !d.IsDir() { + return nil + } + + return w.Add(path) + }) +} -- cgit v1.2.3 From 9eb299c3ac08e2bea4dfe9e3355178566bcf0501 Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 00:36:31 +0530 Subject: refactor(watcher): isolate debounce loop --- internal/watcher/debounce.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ internal/watcher/fsnotify.go | 4 +++ internal/watcher/watcher.go | 11 +++++++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 internal/watcher/debounce.go (limited to 'internal/watcher') diff --git a/internal/watcher/debounce.go b/internal/watcher/debounce.go new file mode 100644 index 0000000..b2961bd --- /dev/null +++ b/internal/watcher/debounce.go @@ -0,0 +1,62 @@ +package watcher + +import ( + "context" + "time" +) + +// notify schedules a debounced notification. +// +// Multiple filesystem events collapse into a single callback. +func (w *Watcher) notify() { + if w.config.OnChange == nil { + return + } + + select { + case w.debounce <- struct{}{}: + default: + } +} + +func (w *Watcher) debounceLoop(ctx context.Context) { + if w.config.OnChange == nil { + return + } + + timer := time.NewTimer(time.Hour) + + if !timer.Stop() { + select { + case <-timer.C: + default: + } + } + + var pending bool + + for { + select { + case <-ctx.Done(): + return + + case <-w.debounce: + pending = true + + if !timer.Stop() { + select { + case <-timer.C: + default: + } + } + + timer.Reset(w.config.Debounce) + + case <-timer.C: + if pending { + pending = false + w.config.OnChange() + } + } + } +} diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go index 7251907..b759c50 100644 --- a/internal/watcher/fsnotify.go +++ b/internal/watcher/fsnotify.go @@ -22,6 +22,8 @@ func (w *Watcher) Run(ctx context.Context) error { _ = fswatcher.Close() }() + go w.debounceLoop(ctx) + for _, root := range w.config.Roots { if err := watchTree(fswatcher, root); err != nil { return err @@ -40,6 +42,8 @@ func (w *Watcher) Run(ctx context.Context) error { log.Printf("watcher: %v %s", event.Op, event.Name) + w.notify() + if !event.Has(fsnotify.Create) { continue } diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 1ca8e37..ee16b7b 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -1,11 +1,20 @@ package watcher +import "time" + type Watcher struct { config Config + + debounce chan struct{} } func New(config Config) *Watcher { + if config.Debounce <= 0 { + config.Debounce = 500 * time.Millisecond + } + return &Watcher{ - config: config, + config: config, + debounce: make(chan struct{}), } } -- cgit v1.2.3 From c8a8431158f07b75adc1fd86b344790075b9cf7a Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 10:33:58 +0530 Subject: refactor(watcher): buffer debounce notifications --- internal/watcher/watcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'internal/watcher') diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index ee16b7b..303eced 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -15,6 +15,6 @@ func New(config Config) *Watcher { return &Watcher{ config: config, - debounce: make(chan struct{}), + debounce: make(chan struct{}, 1), } } -- cgit v1.2.3 From cc42b82782eca935beb6b552e293e4458bb715bc Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 11:00:10 +0530 Subject: test(watcher): verify debounce behavior --- internal/watcher/debounce_test.go | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 internal/watcher/debounce_test.go (limited to 'internal/watcher') diff --git a/internal/watcher/debounce_test.go b/internal/watcher/debounce_test.go new file mode 100644 index 0000000..a93ba05 --- /dev/null +++ b/internal/watcher/debounce_test.go @@ -0,0 +1,55 @@ +package watcher + +import ( + "sync/atomic" + "testing" + "time" +) + +func TestDebounceCollapsesNotifications(t *testing.T) { + var calls atomic.Int32 + + w := New(Config{ + Debounce: 25 * time.Millisecond, + OnChange: func() { + calls.Add(1) + }, + }) + + go w.debounceLoop(t.Context()) + + for range 10 { + w.notify() + } + + time.Sleep(75 * time.Millisecond) + + if got := calls.Load(); got != 1 { + t.Fatalf("expected 1 callback, got %d", got) + } +} + +func TestDebounceSeparateBursts(t *testing.T) { + var calls atomic.Int32 + + w := New(Config{ + Debounce: 20 * time.Millisecond, + OnChange: func() { + calls.Add(1) + }, + }) + + go w.debounceLoop(t.Context()) + + w.notify() + + time.Sleep(40 * time.Millisecond) + + w.notify() + + time.Sleep(40 * time.Millisecond) + + if got := calls.Load(); got != 2 { + t.Fatalf("expected 2 callbacks, got %d", got) + } +} -- cgit v1.2.3 From 868c02846962558a97a84c085c92baa5ba6e535f Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 11:02:36 +0530 Subject: refactor(watcher): refactor tests for faster testing time --- internal/watcher/debounce_test.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'internal/watcher') diff --git a/internal/watcher/debounce_test.go b/internal/watcher/debounce_test.go index a93ba05..007dbd4 100644 --- a/internal/watcher/debounce_test.go +++ b/internal/watcher/debounce_test.go @@ -7,12 +7,18 @@ import ( ) func TestDebounceCollapsesNotifications(t *testing.T) { + done := make(chan struct{}, 1) var calls atomic.Int32 w := New(Config{ Debounce: 25 * time.Millisecond, OnChange: func() { calls.Add(1) + + select { + case done <- struct{}{}: + default: + } }, }) @@ -22,7 +28,11 @@ func TestDebounceCollapsesNotifications(t *testing.T) { w.notify() } - time.Sleep(75 * time.Millisecond) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for callback") + } if got := calls.Load(); got != 1 { t.Fatalf("expected 1 callback, got %d", got) @@ -30,12 +40,18 @@ func TestDebounceCollapsesNotifications(t *testing.T) { } func TestDebounceSeparateBursts(t *testing.T) { + done := make(chan struct{}, 2) var calls atomic.Int32 w := New(Config{ Debounce: 20 * time.Millisecond, OnChange: func() { calls.Add(1) + + select { + case done <- struct{}{}: + default: + } }, }) @@ -43,11 +59,19 @@ func TestDebounceSeparateBursts(t *testing.T) { w.notify() - time.Sleep(40 * time.Millisecond) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for first callback") + } w.notify() - time.Sleep(40 * time.Millisecond) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for second callback") + } if got := calls.Load(); got != 2 { t.Fatalf("expected 2 callbacks, got %d", got) -- cgit v1.2.3