GCache2: Have a consistent mutex locking policy
erwanor opened this issue · 0 comments
erwanor commented
Example:
func (c *ARC) Remove(key interface{}) bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.remove(key)
}
However, for the Get
method we do:
func (c *ARC) Get(key interface{}) (interface{}, error) {
v, err := c.get(key, false)
if err == KeyNotFoundError {
return c.getWithLoader(key, true)
}
return v, err
}
further down the execution path, we see that the mutex is locked in getValue
:
func (c *ARC) getValue(key interface{}, onLoad bool) (interface{}, error) {
c.mu.Lock()
defer c.mu.Unlock()
We should be careful about this. Without a consistent policy about where to lock/unlock a mutex in the execution path it can be easy making a mistake that compromise the concurrency-safe property of the library.