diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-05-20 12:18:48 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-05-21 17:36:53 +0300 |
commit | 4badd2aae14bdcfd42d233264620e28e2e1c63b5 (patch) | |
tree | 52c4f165c60990c0563412424c208a3a8ef12edf | |
parent | c202c0760cecf7b29ddb9ec46a794e208f4da82b (diff) |
catfile: Change layout of BatchCache struct to show mutex relation
By taking a look at the BatchCache structure, it's not immediately
obvious what the embedded mutex is actually guarding. Refactor the
structure and rename the mutex to make it clear.
-rw-r--r-- | internal/git/catfile/batch_cache.go | 24 | ||||
-rw-r--r-- | internal/git/catfile/batch_cache_test.go | 8 | ||||
-rw-r--r-- | internal/git/catfile/batch_test.go | 4 |
3 files changed, 17 insertions, 19 deletions
diff --git a/internal/git/catfile/batch_cache.go b/internal/git/catfile/batch_cache.go index 08af66b4d..81a18c813 100644 --- a/internal/git/catfile/batch_cache.go +++ b/internal/git/catfile/batch_cache.go @@ -61,15 +61,10 @@ type entry struct { // an entry gets added it gets an expiry time based on a fixed TTL. A // monitor goroutine periodically evicts expired entries. type BatchCache struct { - entries []*entry - sync.Mutex - // maxLen is the maximum number of keys in the cache maxLen int - // ttl is the fixed ttl for cache entries ttl time.Duration - // injectSpawnErrors is used for testing purposes only. If set to true, then spawned batch // processes will simulate spawn errors. injectSpawnErrors bool @@ -79,6 +74,9 @@ type BatchCache struct { totalCatfileProcesses prometheus.Counter catfileLookupCounter *prometheus.CounterVec catfileCacheMembers prometheus.Gauge + + entriesMutex sync.Mutex + entries []*entry } // NewCache creates a new catfile process cache. @@ -211,8 +209,8 @@ func (bc *BatchCache) returnWhenDone(done <-chan struct{}, cacheKey key, c *batc // add adds a key, value pair to bc. If there are too many keys in bc // already add will evict old keys until the length is OK again. func (bc *BatchCache) add(k key, b *batch) { - bc.Lock() - defer bc.Unlock() + bc.entriesMutex.Lock() + defer bc.entriesMutex.Unlock() if i, ok := bc.lookup(k); ok { bc.catfileCacheCounter.WithLabelValues("duplicate").Inc() @@ -235,8 +233,8 @@ func (bc *BatchCache) len() int { return len(bc.entries) } // checkout removes a value from bc. After use the caller can re-add the value with bc.Add. func (bc *BatchCache) checkout(k key) (*batch, bool) { - bc.Lock() - defer bc.Unlock() + bc.entriesMutex.Lock() + defer bc.entriesMutex.Unlock() i, ok := bc.lookup(k) if !ok { @@ -254,8 +252,8 @@ func (bc *BatchCache) checkout(k key) (*batch, bool) { // enforceTTL evicts all entries older than now, assuming the entry // expiry times are increasing. func (bc *BatchCache) enforceTTL(now time.Time) { - bc.Lock() - defer bc.Unlock() + bc.entriesMutex.Lock() + defer bc.entriesMutex.Unlock() for bc.len() > 0 && now.After(bc.head().expiry) { bc.evictHead() @@ -264,8 +262,8 @@ func (bc *BatchCache) enforceTTL(now time.Time) { // Evict evicts all cached processes from the cache. func (bc *BatchCache) Evict() { - bc.Lock() - defer bc.Unlock() + bc.entriesMutex.Lock() + defer bc.entriesMutex.Unlock() for bc.len() > 0 { bc.evictHead() diff --git a/internal/git/catfile/batch_cache_test.go b/internal/git/catfile/batch_cache_test.go index c797e396a..885978a7c 100644 --- a/internal/git/catfile/batch_cache_test.go +++ b/internal/git/catfile/batch_cache_test.go @@ -158,8 +158,8 @@ func TestAutoExpiry(t *testing.T) { } func requireCacheValid(t *testing.T, bc *BatchCache) { - bc.Lock() - defer bc.Unlock() + bc.entriesMutex.Lock() + defer bc.entriesMutex.Unlock() for _, ent := range bc.entries { v := ent.value @@ -172,8 +172,8 @@ func testValue() *batch { return &batch{} } func testKey(i int) key { return key{sessionID: fmt.Sprintf("key-%d", i)} } func keys(bc *BatchCache) []key { - bc.Lock() - defer bc.Unlock() + bc.entriesMutex.Lock() + defer bc.entriesMutex.Unlock() var result []key for _, ent := range bc.entries { diff --git a/internal/git/catfile/batch_test.go b/internal/git/catfile/batch_test.go index d0a2766d8..a7df2d846 100644 --- a/internal/git/catfile/batch_test.go +++ b/internal/git/catfile/batch_test.go @@ -443,7 +443,7 @@ func numGitChildren(t *testing.T) int { } func cacheSize(bc *BatchCache) int { - bc.Lock() - defer bc.Unlock() + bc.entriesMutex.Lock() + defer bc.entriesMutex.Unlock() return bc.len() } |