Fix mutexes in the cacher (again)

This commit is contained in:
2026-02-12 03:02:16 +03:00
parent 9e59fa18e4
commit 1b44637606
2 changed files with 5 additions and 6 deletions

View File

@@ -36,13 +36,13 @@ func (c *Cacher) getFile(sourcePath string) (*models.CacheItem, error) {
cacheFilePath := filepath.Join(c.cacheDir, cacheKey+".m4a") cacheFilePath := filepath.Join(c.cacheDir, cacheKey+".m4a")
c.itemsMutex.Lock() c.itemsMutex.Lock()
defer c.itemsMutex.Unlock()
// Check if file information exists in cache // Check if file information exists in cache
if item, ok := c.items[cacheKey]; ok { if item, ok := c.items[cacheKey]; ok {
if _, err := os.Stat(item.Path); err != nil { if _, err := os.Stat(item.Path); err == nil {
// File exists in cache and on disk // File exists in cache and on disk
item.Updated = time.Now().UTC() item.Updated = time.Now().UTC()
c.itemsMutex.Unlock()
c.updateCachedStat(sourcePath, item.Size) c.updateCachedStat(sourcePath, item.Size)
@@ -64,6 +64,7 @@ func (c *Cacher) getFile(sourcePath string) (*models.CacheItem, error) {
} }
c.items[cacheKey] = item c.items[cacheKey] = item
c.currentSize += cachedFileInfo.Size() c.currentSize += cachedFileInfo.Size()
c.itemsMutex.Unlock()
c.updateCachedStat(sourcePath, item.Size) c.updateCachedStat(sourcePath, item.Size)
@@ -82,6 +83,7 @@ func (c *Cacher) getFile(sourcePath string) (*models.CacheItem, error) {
// Convert file // Convert file
size, err := c.transcoder.Convert(sourcePath, cacheFilePath) size, err := c.transcoder.Convert(sourcePath, cacheFilePath)
if err != nil { if err != nil {
c.itemsMutex.Unlock()
return nil, fmt.Errorf("%w: %w (%w)", ErrCacher, ErrFailedToTranscodeFile, err) return nil, fmt.Errorf("%w: %w (%w)", ErrCacher, ErrFailedToTranscodeFile, err)
} }
@@ -93,6 +95,7 @@ func (c *Cacher) getFile(sourcePath string) (*models.CacheItem, error) {
} }
c.items[cacheKey] = item c.items[cacheKey] = item
c.currentSize += size c.currentSize += size
c.itemsMutex.Unlock()
c.updateCachedStat(sourcePath, size) c.updateCachedStat(sourcePath, size)
// TODO: run cleanup on inotify events. // TODO: run cleanup on inotify events.

View File

@@ -13,10 +13,6 @@ import (
// getStat returns file size without triggering conversion (for ls/stat). // getStat returns file size without triggering conversion (for ls/stat).
func (c *Cacher) GetStat(sourcePath string) (int64, error) { func (c *Cacher) GetStat(sourcePath string) (int64, error) {
c.statMutex.RLock()
defer c.statMutex.RUnlock()
// First check cache
if size, ok := c.getCachedStat(sourcePath); ok { if size, ok := c.getCachedStat(sourcePath); ok {
return size, nil return size, nil
} }