Created
April 25, 2016 20:30
-
-
Save armon/00294f5143aa7aa46d842187ea9cc00c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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