Fix mutexes in the cacher (again)
This commit is contained in:
@@ -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.
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user