diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-07-17 11:10:53 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-17 11:10:53 +0530 |
| commit | 6afb80ab754da137939dd523ee419818026f9fb5 (patch) | |
| tree | f9f94ec69ac87abe8060cefca8708572bc13bdd6 /internal/watcher | |
| parent | bcfb199ac8e5399e1a92cfbbc28af3674f80959d (diff) | |
| parent | 5ae787be3d1d9de3355f8b5662fea857204e693c (diff) | |
| download | galleryd-6afb80ab754da137939dd523ee419818026f9fb5.tar.xz galleryd-6afb80ab754da137939dd523ee419818026f9fb5.zip | |
Merge pull request #11 from RanXom/feat/live-gallery
Feat/live-gallery: live gallery with automatic filesystem reloads
Diffstat (limited to 'internal/watcher')
| -rw-r--r-- | internal/watcher/config.go | 11 | ||||
| -rw-r--r-- | internal/watcher/debounce.go | 62 | ||||
| -rw-r--r-- | internal/watcher/debounce_test.go | 79 | ||||
| -rw-r--r-- | internal/watcher/doc.go | 7 | ||||
| -rw-r--r-- | internal/watcher/fsnotify.go | 72 | ||||
| -rw-r--r-- | internal/watcher/watch.go | 26 | ||||
| -rw-r--r-- | internal/watcher/watcher.go | 20 | ||||
| -rw-r--r-- | internal/watcher/watcher_test.go | 31 |
8 files changed, 308 insertions, 0 deletions
diff --git a/internal/watcher/config.go b/internal/watcher/config.go new file mode 100644 index 0000000..a59474f --- /dev/null +++ b/internal/watcher/config.go @@ -0,0 +1,11 @@ +package watcher + +import "time" + +// Config configures a filesystem watcher +type Config struct { + Roots []string + + Debounce time.Duration + OnChange func() +} 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/debounce_test.go b/internal/watcher/debounce_test.go new file mode 100644 index 0000000..007dbd4 --- /dev/null +++ b/internal/watcher/debounce_test.go @@ -0,0 +1,79 @@ +package watcher + +import ( + "sync/atomic" + "testing" + "time" +) + +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: + } + }, + }) + + go w.debounceLoop(t.Context()) + + for range 10 { + w.notify() + } + + 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) + } +} + +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: + } + }, + }) + + go w.debounceLoop(t.Context()) + + w.notify() + + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for first callback") + } + + w.notify() + + 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) + } +} 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/fsnotify.go b/internal/watcher/fsnotify.go new file mode 100644 index 0000000..b759c50 --- /dev/null +++ b/internal/watcher/fsnotify.go @@ -0,0 +1,72 @@ +package watcher + +import ( + "context" + "fmt" + "log" + "os" + + "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() + }() + + go w.debounceLoop(ctx) + + for _, root := range w.config.Roots { + if err := watchTree(fswatcher, root); 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) + + w.notify() + + 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 + } + + log.Printf("watcher error: %v", err) + } + } +} 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) + }) +} diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go new file mode 100644 index 0000000..303eced --- /dev/null +++ b/internal/watcher/watcher.go @@ -0,0 +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, + debounce: make(chan struct{}, 1), + } +} 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") + } +} |