Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,19 @@ func (c *Config) Validate() error {

const defaultMetadataTTL = 5 * time.Minute //nolint:mnd // sensible default

// ParseMaxSize returns the maximum cache size in bytes.
// Returns 0 if unset or explicitly disabled (meaning unlimited).
func (c *Config) ParseMaxSize() int64 {
if c.Storage.MaxSize == "" || c.Storage.MaxSize == "0" {
return 0
}
size, err := ParseSize(c.Storage.MaxSize)
if err != nil {
return 0
}
return size
}

// ParseMetadataTTL returns the metadata TTL duration.
// Returns 5 minutes if unset, 0 if explicitly disabled.
func (c *Config) ParseMetadataTTL() time.Duration {
Expand Down
25 changes: 25 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,31 @@ func TestLoadFileNotFound(t *testing.T) {
}
}

func TestParseMaxSize(t *testing.T) {
tests := []struct {
name string
maxSize string
want int64
}{
{"empty means unlimited", "", 0},
{"zero means unlimited", "0", 0},
{"10GB", "10GB", 10 * 1024 * 1024 * 1024},
{"500MB", "500MB", 500 * 1024 * 1024},
{"invalid returns 0", "invalid", 0},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := Default()
cfg.Storage.MaxSize = tt.maxSize
got := cfg.ParseMaxSize()
if got != tt.want {
t.Errorf("ParseMaxSize() = %d, want %d", got, tt.want)
}
})
}
}

func TestParseMetadataTTL(t *testing.T) {
tests := []struct {
name string
Expand Down
105 changes: 105 additions & 0 deletions internal/server/eviction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package server

import (
"context"
"log/slog"
"time"

"github.com/git-pkgs/proxy/internal/database"
"github.com/git-pkgs/proxy/internal/storage"
)

const (
evictionInterval = 1 * time.Minute
evictionBatch = 50
)

func (s *Server) startEvictionLoop(ctx context.Context) {
maxSize := s.cfg.ParseMaxSize()
if maxSize <= 0 {
return
}

s.logger.Info("cache eviction enabled", "max_size", s.cfg.Storage.MaxSize)

ticker := time.NewTicker(evictionInterval)
defer ticker.Stop()

s.runEviction(ctx, maxSize)

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.runEviction(ctx, maxSize)
}
}
}

func (s *Server) runEviction(ctx context.Context, maxSize int64) {
evictLRU(ctx, s.db, s.storage, s.logger, maxSize)
}

func evictLRU(ctx context.Context, db *database.DB, store storage.Storage, logger *slog.Logger, maxSize int64) {
totalSize, err := db.GetTotalCacheSize()
if err != nil {
logger.Warn("eviction: failed to get cache size", "error", err)
return
}

if totalSize <= maxSize {
return
}

logger.Info("eviction: cache size exceeds limit, evicting",
"current_size", totalSize, "max_size", maxSize)

evicted := 0
freedBytes := int64(0)

for totalSize-freedBytes > maxSize {
artifacts, err := db.GetLeastRecentlyUsedArtifacts(evictionBatch)
if err != nil {
logger.Warn("eviction: failed to get LRU artifacts", "error", err)
return
}
if len(artifacts) == 0 {
break
}

for _, art := range artifacts {
if totalSize-freedBytes <= maxSize {
break
}

if !art.StoragePath.Valid {
continue
}

if err := store.Delete(ctx, art.StoragePath.String); err != nil {
logger.Warn("eviction: failed to delete from storage",
"path", art.StoragePath.String, "error", err)
continue
}

if err := db.ClearArtifactCache(art.VersionPURL, art.Filename); err != nil {
logger.Warn("eviction: failed to clear artifact record",
"version_purl", art.VersionPURL, "filename", art.Filename, "error", err)
continue
}

size := int64(0)
if art.Size.Valid {
size = art.Size.Int64
}
freedBytes += size
evicted++
}
}

if evicted > 0 {
logger.Info("eviction: completed",
"evicted", evicted, "freed_bytes", freedBytes)
}
}
Loading