github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/builder-next/worker/gc.go (about)

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