aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/service/gallery.go4
-rw-r--r--internal/service/gallery_service.go3
-rw-r--r--internal/service/photo.go3
-rw-r--r--internal/service/reload.go4
-rw-r--r--internal/service/service.go2
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.