update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
130
utils/cache/file_haunter.go
vendored
Normal file
130
utils/cache/file_haunter.go
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/djherbis/fscache"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
)
|
||||
|
||||
type haunterKV struct {
|
||||
key string
|
||||
value fscache.Entry
|
||||
info fscache.FileInfo
|
||||
}
|
||||
|
||||
// NewFileHaunter returns a simple haunter which runs every "period"
|
||||
// and scrubs older files when the total file size is over maxSize or
|
||||
// total item count is over maxItems. It also removes empty (invalid) files.
|
||||
// If maxItems or maxSize are 0, they won't be checked
|
||||
//
|
||||
// Based on fscache.NewLRUHaunter
|
||||
func NewFileHaunter(name string, maxItems int, maxSize uint64, period time.Duration) fscache.LRUHaunter {
|
||||
return &fileHaunter{
|
||||
name: name,
|
||||
period: period,
|
||||
maxItems: maxItems,
|
||||
maxSize: maxSize,
|
||||
}
|
||||
}
|
||||
|
||||
type fileHaunter struct {
|
||||
name string
|
||||
period time.Duration
|
||||
maxItems int
|
||||
maxSize uint64
|
||||
}
|
||||
|
||||
func (j *fileHaunter) Next() time.Duration {
|
||||
return j.period
|
||||
}
|
||||
|
||||
func (j *fileHaunter) Scrub(c fscache.CacheAccessor) (keysToReap []string) {
|
||||
var count int
|
||||
var size uint64
|
||||
var okFiles []haunterKV
|
||||
|
||||
log.Trace("Running cache cleanup", "cache", j.name, "maxSize", humanize.Bytes(j.maxSize))
|
||||
c.EnumerateEntries(func(key string, e fscache.Entry) bool {
|
||||
if e.InUse() {
|
||||
return true
|
||||
}
|
||||
|
||||
fileInfo, err := c.Stat(e.Name())
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if fileInfo.Size() == 0 {
|
||||
log.Trace("Removing invalid empty file", "file", e.Name())
|
||||
keysToReap = append(keysToReap, key)
|
||||
}
|
||||
|
||||
count++
|
||||
size = size + uint64(fileInfo.Size())
|
||||
okFiles = append(okFiles, haunterKV{
|
||||
key: key,
|
||||
value: e,
|
||||
info: fileInfo,
|
||||
})
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
sort.Slice(okFiles, func(i, j int) bool {
|
||||
iLastRead := okFiles[i].info.AccessTime()
|
||||
jLastRead := okFiles[j].info.AccessTime()
|
||||
|
||||
return iLastRead.Before(jLastRead)
|
||||
})
|
||||
|
||||
collectKeysToReapFn := func() bool {
|
||||
var key *string
|
||||
var err error
|
||||
key, count, size, err = j.removeFirst(&okFiles, count, size)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if key != nil {
|
||||
keysToReap = append(keysToReap, *key)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
log.Trace("Current cache stats", "cache", j.name, "size", humanize.Bytes(size), "numItems", count)
|
||||
|
||||
if j.maxItems > 0 {
|
||||
for count > j.maxItems {
|
||||
if !collectKeysToReapFn() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if j.maxSize > 0 {
|
||||
for size > j.maxSize {
|
||||
if !collectKeysToReapFn() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(keysToReap) > 0 {
|
||||
log.Trace("Removing items from cache", "cache", j.name, "numItems", len(keysToReap))
|
||||
}
|
||||
return keysToReap
|
||||
}
|
||||
|
||||
func (j *fileHaunter) removeFirst(items *[]haunterKV, count int, size uint64) (*string, int, uint64, error) {
|
||||
var f haunterKV
|
||||
|
||||
f, *items = (*items)[0], (*items)[1:]
|
||||
|
||||
count--
|
||||
size = size - uint64(f.info.Size())
|
||||
|
||||
return &f.key, count, size, nil
|
||||
}
|
||||
Reference in New Issue
Block a user