Skip to content

Instantly share code, notes, and snippets.

@armon
Created April 25, 2016 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armon/00294f5143aa7aa46d842187ea9cc00c to your computer and use it in GitHub Desktop.
Save armon/00294f5143aa7aa46d842187ea9cc00c to your computer and use it in GitHub Desktop.
func readPath(name string) {
p := GetPolicy(name)
DoSomething(p)
}
func writePath(name string) {
p := GetPolicy(name)
LockManager.Lock(name, func() {
DoSomethign(p)
})
}
func GetPolicy(name string) *Policy {
p := GetCachedPolicy(name)
if p == nil {
p = GetStoredPolicy(name)
}
if p != nil && p.NeedsUpgrade() {
LockManager.Lock(name, func() {
// Check if the lock was upgraded in a race
p := GetPolicy(name)
if !p.NeedsUpgrade() {
return p
}
// Do the upgrade
p.Upgrade()
// Update the cache
SetCachedPolicy(p)
return p
})
}
return p
}
func GetCachedPolicy(name) *Policy {
if cacheDisabled {
return nil
}
cache.RLock()
defer cache.RUnlock()
return cache[name]
}
func SetCachedPolicy(name, p *Policy) {
if cacheDisabled {
return
}
cache.Lock()
defer cache.Unlock()
cache[name] = p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment