Split internal/imgcache into the image proxy and the cache (closes #317)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The new package internal/imageproxy holds Proxy, which was imgcache.Service, with its Config, ImageResponse, CacheStatus and errors. imageproxy.New makes and registers the counters of upstream fetches and transforms. internal/imgcache holds the cache only, with its hit and miss counters, and imgcache.CacheConfig is imgcache.Config. The cache reads and writes each kind it keeps with one pair of methods: Variant and PutVariant, Source and PutSource, Failure and PutFailure. Source replaces LookupSource followed by GetSourceContent. imgcache.go and module.go are gone. The proxy's tests moved with it. The cache's tests check that the reconciliation pass removes an empty or cut file and its row. The proxy's tests remove them by hand, then check that Get fetches or converts again. Model: opus-5-5
This commit was merged in pull request #334.
This commit is contained in:
@@ -67,7 +67,7 @@ func newEvictionTestCache(t *testing.T, maxBytes int64) (*Cache, string) {
|
||||
db := evictionTestDB(t)
|
||||
|
||||
// maxBytes zero turns the disk cache off, as cache_max_bytes: 0 does.
|
||||
cache, err := NewCache(db, CacheConfig{
|
||||
cache, err := NewCache(db, Config{
|
||||
StateDir: tmpDir,
|
||||
NegativeTTL: 5 * time.Minute,
|
||||
MaxBytes: maxBytes,
|
||||
@@ -98,11 +98,11 @@ func storeEvictionTestSource(
|
||||
},
|
||||
}
|
||||
|
||||
key, err := cache.StoreSource(
|
||||
context.Background(), req, bytes.NewReader(content),
|
||||
key, err := cache.PutSource(
|
||||
context.Background(), req.Source, bytes.NewReader(content),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("StoreSource(%s%s) failed: %v", host, path, err)
|
||||
t.Fatalf("PutSource(%s%s) failed: %v", host, path, err)
|
||||
}
|
||||
|
||||
return key
|
||||
@@ -115,9 +115,9 @@ func storeEvictionTestVariant(
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
err := cache.StoreVariant(t.Context(), key, bytes.NewReader(content))
|
||||
err := cache.PutVariant(t.Context(), key, bytes.NewReader(content))
|
||||
if err != nil {
|
||||
t.Fatalf("StoreVariant(%s) failed: %v", key, err)
|
||||
t.Fatalf("PutVariant(%s) failed: %v", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -423,20 +423,20 @@ func assertDisabledCacheWritesAreNoOps(
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
err := cache.StoreVariant(
|
||||
err := cache.PutVariant(
|
||||
ctx, imagerequest.CacheKey(req), bytes.NewReader([]byte("data")),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("StoreVariant on disabled cache must be a no-op, got error: %v", err)
|
||||
t.Fatalf("PutVariant on disabled cache must be a no-op, got error: %v", err)
|
||||
}
|
||||
|
||||
key, err := cache.StoreSource(ctx, req, bytes.NewReader([]byte("data")))
|
||||
key, err := cache.PutSource(ctx, req.Source, bytes.NewReader([]byte("data")))
|
||||
if err != nil {
|
||||
t.Fatalf("StoreSource on disabled cache must be a no-op, got error: %v", err)
|
||||
t.Fatalf("PutSource on disabled cache must be a no-op, got error: %v", err)
|
||||
}
|
||||
|
||||
if key != "" {
|
||||
t.Errorf("StoreSource on disabled cache returned key %q, want empty", key)
|
||||
t.Errorf("PutSource on disabled cache returned key %q, want empty", key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -449,13 +449,13 @@ func assertDisabledCacheReadsAlwaysMiss(
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
key, err := cache.LookupSource(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatalf("LookupSource on disabled cache failed: %v", err)
|
||||
reader, _, err := cache.Source(ctx, req.Source)
|
||||
if !errors.Is(err, ErrNotFound) {
|
||||
t.Fatalf("Source on disabled cache error = %v, want %v", err, ErrNotFound)
|
||||
}
|
||||
|
||||
if key != "" {
|
||||
t.Errorf("LookupSource on disabled cache = %q, want empty", key)
|
||||
if reader != nil {
|
||||
t.Errorf("Source on disabled cache = reader %v, want none", reader)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,7 +544,7 @@ func TestEvictionRunsUnderWritePressure(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestEvictionRunsOnPeriodicSchedule writes three variant files straight
|
||||
// to disk, bypassing StoreVariant, so they have no accounting rows and no
|
||||
// to disk, bypassing PutVariant, so they have no accounting rows and no
|
||||
// write-pressure notification fires. Only a periodic reconciliation pass
|
||||
// can then adopt them, and only the eviction pass that follows it can
|
||||
// evict them.
|
||||
@@ -697,7 +697,7 @@ func TestStartEvictionReconcilesAccountingWithDisk(t *testing.T) {
|
||||
// TestPeriodicReconciliationAdoptsFileThatAppearsAfterStartup proves
|
||||
// reconciliation is not a one-shot startup-only pass: it must also run
|
||||
// on the periodic ticker, so a variant file that lands on disk with no
|
||||
// accounting row well after startup (e.g. because StoreVariant's
|
||||
// accounting row well after startup (e.g. because PutVariant's
|
||||
// best-effort accounting insert failed under transient contention, or
|
||||
// any other cause of an untracked file appearing during steady-state
|
||||
// operation) is still adopted into accounting eventually, rather than
|
||||
@@ -733,7 +733,7 @@ func TestPeriodicReconciliationAdoptsFileThatAppearsAfterStartup(t *testing.T) {
|
||||
|
||||
// Simulate a variant whose accounting insert failed after the
|
||||
// process was already running and serving requests: the content
|
||||
// file is written directly, bypassing StoreVariant's (and thus its
|
||||
// file is written directly, bypassing PutVariant's (and thus its
|
||||
// accounting insert) entirely, exactly as would happen if that
|
||||
// insert had failed and only the file write had succeeded.
|
||||
untracked := bytes.Repeat([]byte{0x41}, 900)
|
||||
|
||||
Reference in New Issue
Block a user