Skip to content
This repository has been archived by the owner on Oct 3, 2019. It is now read-only.

Commit

Permalink
Remove ping, add cache warmup instead
Browse files Browse the repository at this point in the history
  • Loading branch information
csstaub committed Jun 1, 2016
1 parent abd52a2 commit 6012a1e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
37 changes: 16 additions & 21 deletions cache.go
Expand Up @@ -62,6 +62,22 @@ func NewCache(backend SecretBackend, timeouts Timeouts, logConfig log.Config, no
return &Cache{logger, NewSecretMap(timeouts, now), backend, timeouts, now}
}

// Warmup reads the secret list from the backend to prime the cache.
// Should only be called after creating a new cache on startup.
func (c *Cache) Warmup() {
// Attempt to warmup cache
newMap := NewSecretMap(c.timeouts, c.now)
secrets, ok := c.backend.SecretList()
if ok {
for _, backendSecret := range secrets {
newMap.Put(backendSecret.Name, backendSecret)
}
c.secretMap.Overwrite(newMap)
} else {
c.Warnf("Failed to warmup cache on startup")
}
}

// Clear empties the internal cache. This function does not honor the
// delayed deletion contract. The function is called when the user deletes
// .clear_cache.
Expand Down Expand Up @@ -277,24 +293,3 @@ func (c *Cache) backendSecretList() chan []Secret {
}()
return secretsc
}

// Ping backend on startup
// TODO: convert this to a regular ping
func (c *Cache) pingBackend() bool {
secrets, ok := c.backend.SecretList()
if !ok {
return false
}

// Create a copy of the current map and mark all the elements as deleted.
newMap := NewSecretMap(c.timeouts, c.now)
newMap.Overwrite(c.secretMap)
newMap.DeleteAll()
for _, backendSecret := range secrets {
newMap.Put(backendSecret.Name, backendSecret)
}
// TODO: this code isn't concurrency safe! We could write it so that in the worst case a secret gets marked as deleted instead of
// getting dropped on the floor.
c.secretMap.Overwrite(newMap)
return true
}
12 changes: 1 addition & 11 deletions main.go
Expand Up @@ -39,7 +39,6 @@ var (
caFile = app.Flag("ca", "PEM-encoded CA certificates file").PlaceHolder("FILE").Required().String()
asuser = app.Flag("asuser", "Default user to own files").Default("keywhiz").String()
asgroup = app.Flag("group", "Default group to own files").Default("keywhiz").String()
ping = app.Flag("ping", "Enable startup ping to server").Default("false").Bool()
debug = app.Flag("debug", "Enable debugging output").Default("false").Bool()
timeout = app.Flag("timeout", "Timeout for communication with server").Default("20s").Duration()
metricsURL = app.Flag("metrics-url", "Collect metrics and POST them periodically to the given URL (via HTTP/JSON).").PlaceHolder("URL").String()
Expand Down Expand Up @@ -111,16 +110,7 @@ func main() {
}
}()

// Prime cache: we retrieve the initial secrets list right away, so that
// we can make sure we're ready to show contents as soon as we get mounted.
if *ping {
ok := kwfs.Cache.pingBackend()
if !ok {
fmt.Fprintf(os.Stderr, "unable to talk to backend")
os.Exit(1)
}
}

kwfs.Cache.Warmup()
server.Serve()
logger.Infof("Exiting")
}
Expand Down

0 comments on commit 6012a1e

Please sign in to comment.