about summary refs log tree commit diff
path: root/tools/nixery/server/builder/cache.go
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-09-08T21·21+0100
committerVincent Ambo <github@tazj.in>2019-09-10T10·32+0100
commit051eb77b3de81d9393e5c5443c06b62b6abf1535 (patch)
treee72ad463c1829bd49d874b705e8316f48f6d59be /tools/nixery/server/builder/cache.go
parent980f5e218761fa340b746e6336db62abf63c953a (diff)
refactor(server): Use package source specific cache keys
Use the PackageSource.CacheKey function introduced in the previous
commit to determine the key at which a manifest should be cached in
the local cache.

Due to this change, manifests for moving target sources are no longer
cached and the recency threshold logic has been removed.
Diffstat (limited to 'tools/nixery/server/builder/cache.go')
-rw-r--r--tools/nixery/server/builder/cache.go49
1 files changed, 19 insertions, 30 deletions
diff --git a/tools/nixery/server/builder/cache.go b/tools/nixery/server/builder/cache.go
index 0014789afff5..8ade88037265 100644
--- a/tools/nixery/server/builder/cache.go
+++ b/tools/nixery/server/builder/cache.go
@@ -14,26 +14,15 @@
 package builder
 
 import (
+	"github.com/google/nixery/config"
 	"sync"
-	"time"
 )
 
-// recencyThreshold is the amount of time that a manifest build will be cached
-// for. When using the channel mechanism for retrieving nixpkgs, Nix will
-// occasionally re-fetch the channel so things can in fact change while the
-// instance is running.
-const recencyThreshold = time.Duration(6) * time.Hour
-
-type manifestEntry struct {
-	built time.Time
-	path  string
-}
-
 type void struct{}
 
 type BuildCache struct {
 	mmtx   sync.RWMutex
-	mcache map[string]manifestEntry
+	mcache map[string]string
 
 	lmtx   sync.RWMutex
 	lcache map[string]void
@@ -41,7 +30,7 @@ type BuildCache struct {
 
 func NewCache() BuildCache {
 	return BuildCache{
-		mcache: make(map[string]manifestEntry),
+		mcache: make(map[string]string),
 		lcache: make(map[string]void),
 	}
 }
@@ -63,33 +52,33 @@ func (c *BuildCache) sawLayer(hash string) {
 	c.lcache[hash] = void{}
 }
 
-// Has this manifest been built already? If yes, we can reuse the
-// result given that the build happened recently enough.
-func (c *BuildCache) manifestFromCache(image *Image) (string, bool) {
-	c.mmtx.RLock()
+// Retrieve a cached manifest if the build is cacheable and it exists.
+func (c *BuildCache) manifestFromCache(src config.PkgSource, image *Image) (string, bool) {
+	key := src.CacheKey(image.Packages, image.Tag)
+	if key == "" {
+		return "", false
+	}
 
-	entry, ok := c.mcache[image.Name+image.Tag]
+	c.mmtx.RLock()
+	path, ok := c.mcache[key]
 	c.mmtx.RUnlock()
 
 	if !ok {
 		return "", false
 	}
 
-	if time.Since(entry.built) > recencyThreshold {
-		return "", false
-	}
-
-	return entry.path, true
+	return path, true
 }
 
-// Adds the result of a manifest build to the cache.
-func (c *BuildCache) cacheManifest(image *Image, path string) {
-	entry := manifestEntry{
-		built: time.Now(),
-		path:  path,
+// Adds the result of a manifest build to the cache, if the manifest
+// is considered cacheable.
+func (c *BuildCache) cacheManifest(src config.PkgSource, image *Image, path string) {
+	key := src.CacheKey(image.Packages, image.Tag)
+	if key == "" {
+		return
 	}
 
 	c.mmtx.Lock()
-	c.mcache[image.Name+image.Tag] = entry
+	c.mcache[key] = path
 	c.mmtx.Unlock()
 }