diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-12-10 19:25:44 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-12-10 19:25:44 +0300 |
commit | 185d5e494eb32b14f5f9340d8bd5cb8b95657f3f (patch) | |
tree | 33ad970e6369debe1a00ddbd55807311dbf22833 /internal | |
parent | 2ad3c3a211fa7ca0109e7cc5caf1d9f0047b77ea (diff) |
Optimize gitlab source memstore read-preferring RW locking
Diffstat (limited to 'internal')
-rw-r--r-- | internal/source/gitlab/cache/memstore.go | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/internal/source/gitlab/cache/memstore.go b/internal/source/gitlab/cache/memstore.go index 49e920ae..08eccd94 100644 --- a/internal/source/gitlab/cache/memstore.go +++ b/internal/source/gitlab/cache/memstore.go @@ -9,28 +9,38 @@ import ( type memstore struct { store *cache.Cache - mux *sync.Mutex + mux *sync.RWMutex } func newMemStore() Store { return &memstore{ store: cache.New(longCacheExpiry, time.Minute), - mux: &sync.Mutex{}, + mux: &sync.RWMutex{}, } } +// LoadOrCreate writes or retrieves a domain entry from the cache in a +// thread-safe way, trying to make this read-preferring RW locking. func (m *memstore) LoadOrCreate(domain string) *Entry { + m.mux.RLock() + entry, exists := m.store.Get(domain) + m.mux.RUnlock() + + if exists { + return entry.(*Entry) + } + m.mux.Lock() defer m.mux.Unlock() - if entry, exists := m.store.Get(domain); exists { + if entry, exists = m.store.Get(domain); exists { return entry.(*Entry) } - entry := newCacheEntry(domain) - m.store.SetDefault(domain, entry) + newEntry := newCacheEntry(domain) + m.store.SetDefault(domain, newEntry) - return entry + return newEntry } func (m *memstore) ReplaceOrCreate(domain string, entry *Entry) *Entry { |