gc.go 1.6 KB

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