Count what the cache holds in the default cache_max_bytes (closes #184)
check / check (push) Failing after 2s

The default limit was 75% of the space free at startup. The cache's own
files are not free space, so a fuller cache got a smaller limit after a
restart and eviction then deleted most of it. The default is now 75% of
the sum of the free space and what the cache already holds by its own
size accounting, at least 500 MiB. The cache works it out when it
opens, after the database is open, so the computation and its tests
moved from internal/config to internal/imgcache; the config only
records whether cache_max_bytes was set.

Model: opus-5-5
This commit is contained in:
2026-10-04 14:27:31 +00:00
parent be6c715b36
commit f1455ae361
11 changed files with 352 additions and 307 deletions
+22 -3
View File
@@ -37,11 +37,16 @@ type CacheConfig struct {
NegativeTTL time.Duration
// MaxBytes is the disk cache size limit in bytes that eviction
// enforces. Zero means no limit is enforced (no eviction). The
// config layer supplies the computed default when the operator
// omits cache_max_bytes.
// enforces. Zero means no limit is enforced (no eviction).
MaxBytes int64
// UseDefaultMaxBytes makes NewCache replace MaxBytes with the
// default limit: 75% of the sum of the space free on the filesystem
// holding the cache and the bytes the cache already holds, at least
// DefaultCacheMaxBytesFloor. The config layer sets this when the
// operator omits cache_max_bytes.
UseDefaultMaxBytes bool
// DisableDiskCache turns the disk cache off entirely: no cache
// directories are created, lookups always miss, stores are
// no-ops, and no eviction machinery runs. The config layer sets
@@ -145,6 +150,20 @@ func NewCache(db *sql.DB, config CacheConfig) (*Cache, error) {
c.variants = variants
c.srcMetadata = srcMetadata
if config.UseDefaultMaxBytes {
limit, err := c.computeDefaultMaxBytes(context.Background(), defaultFreeSpaceProbe)
if err != nil {
return nil, err
}
c.config.MaxBytes = limit
log.Info("computed default cache size limit from free space and cache contents",
"cache_max_bytes", limit,
"cache_dir", filepath.Join(config.StateDir, "cache"),
)
}
return c, nil
}