-
-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added config option to set size of transcode cache and cadence to enf… #520
Changes from 2 commits
44bdf93
89dd329
2b6913a
a850ef5
38d4016
03938ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,21 +7,24 @@ import ( | |||||
"io" | ||||||
"os" | ||||||
"path/filepath" | ||||||
"sort" | ||||||
"sync" | ||||||
"time" | ||||||
) | ||||||
|
||||||
const perm = 0o644 | ||||||
|
||||||
type CachingTranscoder struct { | ||||||
cachePath string | ||||||
transcoder Transcoder | ||||||
limitMb int | ||||||
locks keyedMutex | ||||||
} | ||||||
|
||||||
var _ Transcoder = (*CachingTranscoder)(nil) | ||||||
|
||||||
func NewCachingTranscoder(t Transcoder, cachePath string) *CachingTranscoder { | ||||||
return &CachingTranscoder{transcoder: t, cachePath: cachePath} | ||||||
func NewCachingTranscoder(t Transcoder, cachePath string, limitMb int) *CachingTranscoder { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks megabits to me. how about a big B for bytes
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
return &CachingTranscoder{transcoder: t, cachePath: cachePath, limitMb: limitMb} | ||||||
} | ||||||
|
||||||
func (t *CachingTranscoder) Transcode(ctx context.Context, profile Profile, in string, out io.Writer) error { | ||||||
|
@@ -52,6 +55,7 @@ func (t *CachingTranscoder) Transcode(ctx context.Context, profile Profile, in s | |||||
|
||||||
if i, err := cf.Stat(); err == nil && i.Size() > 0 { | ||||||
_, _ = io.Copy(out, cf) | ||||||
_ = os.Chtimes(path, time.Now(), time.Now()) // Touch for LRU cache purposes | ||||||
return nil | ||||||
} | ||||||
|
||||||
|
@@ -64,6 +68,41 @@ func (t *CachingTranscoder) Transcode(ctx context.Context, profile Profile, in s | |||||
return nil | ||||||
} | ||||||
|
||||||
func (t *CachingTranscoder) CacheEject() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could return an error, which we can just log in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
// Delete LRU cache files that exceed size limit. Use last modified time. | ||||||
type file struct { | ||||||
path string | ||||||
info os.FileInfo | ||||||
} | ||||||
|
||||||
var files []file | ||||||
var total int64 = 0 | ||||||
|
||||||
_ = filepath.Walk(t.cachePath, func(path string, info os.FileInfo, err error) error { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use the new (and faster i belive) filepath.WalkDir here? and return an error too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
if !info.IsDir() { | ||||||
files = append(files, file{path, info}) | ||||||
total += info.Size() | ||||||
} | ||||||
return nil | ||||||
}) | ||||||
|
||||||
sort.Slice(files, func(i, j int) bool { | ||||||
return files[i].info.ModTime().Before(files[j].info.ModTime()) | ||||||
}) | ||||||
|
||||||
for total > int64(t.limitMb)*1024*1024 { | ||||||
curFile := files[0] | ||||||
files = files[1:] | ||||||
total -= curFile.info.Size() | ||||||
unlock := t.locks.Lock(curFile.path) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the t.locks is a keyedMutex but the keys are profile strings (ffmpeg commands with args and paths) not paths. so this has little effect i think maybe we should have a global clean lock which the normal transcode thing respects too? something like
that way the cleaning process locks the entire cache, while multiple goroutines looking to read the cache work fine, just not while we're cleaning wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call and my apologies. Done. |
||||||
_ = os.Remove(curFile.path) | ||||||
unlock() | ||||||
} | ||||||
} | ||||||
|
||||||
func cacheKey(cmd string, args []string) string { | ||||||
// the cache is invalid whenever transcode command (which includes the | ||||||
// absolute filepath, bit rate args, replay gain args, etc.) changes | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this was committed accidentally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. Fixed.