gc.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package worker
  2. import (
  3. "math"
  4. "github.com/moby/buildkit/client"
  5. )
  6. const defaultCap int64 = 2e9 // 2GB
  7. // tempCachePercent represents the percentage ratio of the cache size in bytes to temporarily keep for a short period of time (couple of days)
  8. // over the total cache size in bytes. Because there is no perfect value, a mathematically pleasing one was chosen.
  9. // The value is approximately 13.8
  10. const tempCachePercent = math.E * math.Pi * math.Phi
  11. // DefaultGCPolicy returns a default builder GC policy
  12. func DefaultGCPolicy(p string, defaultKeepBytes int64) []client.PruneInfo {
  13. keep := defaultKeepBytes
  14. if defaultKeepBytes == 0 {
  15. keep = detectDefaultGCCap(p)
  16. }
  17. tempCacheKeepBytes := int64(math.Round(float64(keep) / 100. * float64(tempCachePercent)))
  18. const minTempCacheKeepBytes = 512 * 1e6 // 512MB
  19. if tempCacheKeepBytes < minTempCacheKeepBytes {
  20. tempCacheKeepBytes = minTempCacheKeepBytes
  21. }
  22. return []client.PruneInfo{
  23. // if build cache uses more than 512MB delete the most easily reproducible data after it has not been used for 2 days
  24. {
  25. Filter: []string{"type==source.local,type==exec.cachemount,type==source.git.checkout"},
  26. KeepDuration: 48 * 3600, // 48h
  27. KeepBytes: tempCacheKeepBytes,
  28. },
  29. // remove any data not used for 60 days
  30. {
  31. KeepDuration: 60 * 24 * 3600, // 60d
  32. KeepBytes: keep,
  33. },
  34. // keep the unshared build cache under cap
  35. {
  36. KeepBytes: keep,
  37. },
  38. // if previous policies were insufficient start deleting internal data to keep build cache under cap
  39. {
  40. All: true,
  41. KeepBytes: keep,
  42. },
  43. }
  44. }