Skip to content

Commit

Permalink
Added universal lock for cache eject.
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-doherty committed Sep 15, 2024
1 parent a850ef5 commit 38d4016
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Binary file modified cmd/gonic/gonic
Binary file not shown.
9 changes: 7 additions & 2 deletions transcode/transcoder_caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type CachingTranscoder struct {
transcoder Transcoder
limitMB int
locks keyedMutex
cleanLock sync.RWMutex
}

var _ Transcoder = (*CachingTranscoder)(nil)
Expand All @@ -29,6 +30,9 @@ func NewCachingTranscoder(t Transcoder, cachePath string, limitMB int) *CachingT
}

func (t *CachingTranscoder) Transcode(ctx context.Context, profile Profile, in string, out io.Writer) error {
t.cleanLock.RLock()
defer t.cleanLock.RUnlock()

// don't try cache partial transcodes
if profile.Seek() > 0 {
return t.transcoder.Transcode(ctx, profile, in, out)
Expand Down Expand Up @@ -70,6 +74,9 @@ func (t *CachingTranscoder) Transcode(ctx context.Context, profile Profile, in s
}

func (t *CachingTranscoder) CacheEject() error {
t.cleanLock.Lock()
defer t.cleanLock.Unlock()

// Delete LRU cache files that exceed size limit. Use last modified time.
type file struct {
path string
Expand Down Expand Up @@ -106,9 +113,7 @@ func (t *CachingTranscoder) CacheEject() error {
curFile := files[0]
files = files[1:]
total -= curFile.info.Size()
unlock := t.locks.Lock(curFile.path)
err = os.Remove(curFile.path)
unlock()
if err != nil {
return fmt.Errorf("remove cache file: %w", err)
}
Expand Down

0 comments on commit 38d4016

Please sign in to comment.