feat(cache): purge soft-deleted package rows after a retention period

The cleanup worker now hard-deletes package rows whose deleted_at is
older than cache.deleted_retention. The default is 30 days. A value of
0 keeps the rows. The purge runs in bounded batches, with at most 100
batches per cleanup pass, and removes child rows with the package.

History tables have no foreign key to packages, and stats_snapshots
holds its own counts, so the purge leaves both untouched. The cleanup
log line now reports purged_deleted and deleted_retention.
This commit is contained in:
2026-11-23 14:55:26 +00:00
parent 1512230ab3
commit ba8bc392ec
13 changed files with 360 additions and 7 deletions
+30
View File
@@ -85,6 +85,7 @@ type Config struct {
DefaultTTL time.Duration // Freshness TTL for metadata entries
CleanupInterval time.Duration // How often to run cleanup
IdleTTL time.Duration // Remove entries not requested for this long (0 = off)
DeletedRetention time.Duration // Hard-delete soft-deleted rows older than this (0 = keep forever)
EvictionThreshold float64 // Trigger eviction when usage > threshold (0.0-1.0)
MaxConcurrent int // Max concurrent upstream fetches
MaxPackageSize int64 // Maximum package size in bytes (0 = default 2GB)
@@ -97,6 +98,9 @@ const defaultScanWaitTimeout = 30 * time.Second
// cleanupBatchSize bounds how many rows one cleanup query reads.
const cleanupBatchSize = metadata.DefaultBatchSize
// maxPurgeBatches bounds the soft-delete purge of one cleanup pass; the next pass continues.
const maxPurgeBatches = 100
// getAttempts bounds how often Get re-resolves an entry whose blob vanished.
const getAttempts = 2
@@ -917,14 +921,40 @@ func (m *Manager) cleanup(ctx context.Context) {
func(p *metadata.Package) bool { return p.LastAccessed.Before(cutoff) })
}
purged := m.purgeDeleted(ctx, start)
log.Info().
Int("expired", expired.count).
Int("idle", idle.count).
Int64("bytes_freed", expired.bytes+idle.bytes).
Dur("idle_ttl", m.config.IdleTTL).
Int64("purged_deleted", purged).
Dur("deleted_retention", m.config.DeletedRetention).
Msg("Cleanup completed")
}
// purgeDeleted hard-deletes soft-deleted rows past DeletedRetention in bounded
// batches and returns how many it removed.
func (m *Manager) purgeDeleted(ctx context.Context, now time.Time) int64 {
if m.config.DeletedRetention <= 0 {
return 0
}
cutoff := now.Add(-m.config.DeletedRetention)
var total int64
for i := 0; i < maxPurgeBatches && ctx.Err() == nil; i++ {
n, err := m.metadata.PurgeDeletedPackages(ctx, cutoff, cleanupBatchSize)
if err != nil {
log.Error().Err(err).Msg("Failed to purge soft-deleted packages")
break
}
total += n
if n < cleanupBatchSize {
break
}
}
return total
}
// generateStorageKey generates a storage key for a package
func (m *Manager) generateStorageKey(registry, name, version string) string {
return fmt.Sprintf("%s/%s/%s", registry, name, version)