Skip to content

Commit

Permalink
Merge pull request #1006 from godaddy/keycache-locking-improvements
Browse files Browse the repository at this point in the history
[go] KeyCache Locking Improvements
  • Loading branch information
aka-bo authored Dec 13, 2023
2 parents 8285224 + 1efd5fd commit 7fa38cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go/appencryption/.versionfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.0
0.4.1
10 changes: 10 additions & 0 deletions go/appencryption/key_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,19 @@ func isReloadRequired(entry cacheEntry, checkInterval time.Duration) bool {
// is not present in the cache it will retrieve the key using the provided loader
// and store the key if an error is not returned.
func (c *keyCache) GetOrLoad(id KeyMeta, loader func(KeyMeta) (*internal.CryptoKey, error)) (*cachedCryptoKey, error) {
c.rw.RLock()
k, ok := c.getFresh(id)
c.rw.RUnlock()

if ok {
return tracked(k), nil
}

c.rw.Lock()
defer c.rw.Unlock()

// exit early if the key doesn't need to be reloaded just in case it has
// been loaded by rw lock in front of us
if k, ok := c.getFresh(id); ok {
return tracked(k), nil
}
Expand Down

0 comments on commit 7fa38cd

Please sign in to comment.