• Charlie Vieth's avatar
    pkg/cache: fix race in Add() and Evict() (#3013) · 031dfede
    Charlie Vieth authored
    * pkg/cache: fix race in Add() and Evict()
    
    This fixes a race in Add() when the shard is at max capacity and the key
    being added is already stored.  Previously, the shard would evict a
    random value - when all it needed to do was replace an existing value.
    There was a race in how Evict() picked which key to remove, which would
    cause concurrent calls to Evict() to remove the same key.
    
    Additionally, this commit removes a lot of the lock contention and a
    race around Add() and Evict() by changing them to immediately hold the
    write lock.  Previously, they would check conditions with the read lock
    held and not re-check those conditions once the write lock was acquired
    (this is a race).
    
    * pkg/cache: code review comments
    
    * pkg/cache: simplify Add() logic
    031dfede
cache.go 2.5 KB