diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-14 10:57:01 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-14 10:57:01 +0530 |
| commit | ad97c2f3222dcdb8c124747feb4564ca9069fa1e (patch) | |
| tree | 7bbd6fb8551456529a0ca28121821c3f5fa431e5 /internal | |
| parent | 826434a8597122847f643abe835d5c6e5dd74c4c (diff) | |
| download | galleryd-ad97c2f3222dcdb8c124747feb4564ca9069fa1e.tar.xz galleryd-ad97c2f3222dcdb8c124747feb4564ca9069fa1e.zip | |
refactor(service): synchronize gallery access
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/service/gallery.go | 4 | ||||
| -rw-r--r-- | internal/service/gallery_service.go | 3 | ||||
| -rw-r--r-- | internal/service/photo.go | 3 | ||||
| -rw-r--r-- | internal/service/reload.go | 4 | ||||
| -rw-r--r-- | internal/service/service.go | 2 |
5 files changed, 14 insertions, 2 deletions
diff --git a/internal/service/gallery.go b/internal/service/gallery.go index 30abefe..67d0106 100644 --- a/internal/service/gallery.go +++ b/internal/service/gallery.go @@ -1,6 +1,8 @@ package service import ( + "sync" + "github.com/RanXom/galleryd/internal/gallery" "github.com/RanXom/galleryd/internal/scanner" ) @@ -10,5 +12,7 @@ type galleryService struct { scanner *scanner.Scanner builder *gallery.Builder + mu sync.RWMutex + index galleryIndex } diff --git a/internal/service/gallery_service.go b/internal/service/gallery_service.go index e500bec..23a8e85 100644 --- a/internal/service/gallery_service.go +++ b/internal/service/gallery_service.go @@ -9,6 +9,9 @@ import ( // Gallery scans the configured roots and builds the gallery. func (s *galleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { + s.mu.RLock() + defer s.mu.RUnlock() + select { case <-ctx.Done(): return nil, ctx.Err() diff --git a/internal/service/photo.go b/internal/service/photo.go index 018ba6b..7242d8e 100644 --- a/internal/service/photo.go +++ b/internal/service/photo.go @@ -14,6 +14,9 @@ func (s *galleryService) Photo( ctx context.Context, id string, ) (gallery.Photo, error) { + s.mu.RLock() + defer s.mu.RUnlock() + select { case <-ctx.Done(): return gallery.Photo{}, ctx.Err() diff --git a/internal/service/reload.go b/internal/service/reload.go index 98e4710..9460dbc 100644 --- a/internal/service/reload.go +++ b/internal/service/reload.go @@ -6,7 +6,7 @@ import ( "github.com/RanXom/galleryd/internal/gallery" ) -// Load scans the configured roots and rebuilds the in-memory gallery. +// Reload scans the configured roots and rebuilds the in-memory gallery. func (s *galleryService) Reload(ctx context.Context) error { files, err := s.scanner.Scan(ctx) if err != nil { @@ -27,7 +27,9 @@ func (s *galleryService) Reload(ctx context.Context) error { index.byID[photo.ID] = photo } + s.mu.Lock() s.index = index + s.mu.Unlock() return nil } diff --git a/internal/service/service.go b/internal/service/service.go index 063c0e7..0b9b253 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -8,7 +8,7 @@ import ( // GalleryService provides gallery operations. type GalleryService interface { - // Load scans the configured roots and builds the in-memory gallery. + // Reload scans the configured roots and rebuilds the in-memory gallery. Reload(ctx context.Context) error // Gallery returns the currently loaded gallery. |