Skip to content

Commit

Permalink
nsqlookupd: add lock pretection for cachedFindProducersResults
Browse files Browse the repository at this point in the history
  • Loading branch information
andyxning committed Nov 1, 2019
1 parent b796df8 commit 7d96abd
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions nsqlookupd/registration_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type RegistrationDB struct {
sync.RWMutex
registrationMap map[Registration]ProducerMap

cachedMutex sync.RWMutex
cachedFindProducersResults *cache.Cache
}

Expand Down Expand Up @@ -184,15 +185,26 @@ func (r *RegistrationDB) FindRegistrations(category string, key string, subkey s
}

func (r *RegistrationDB) FindProducers(category string, key string, subkey string) Producers {
r.RLock()
defer r.RUnlock()

r.cachedMutex.RLock()
cachedKey := fmt.Sprintf("%s:%s:%s", category, key, subkey)

if val, found := r.cachedFindProducersResults.Get(cachedKey); found {
r.cachedMutex.Unlock()
return val.(Producers)
}

r.cachedMutex.Unlock()

r.cachedMutex.Lock()
defer r.cachedMutex.Unlock()

if val, found := r.cachedFindProducersResults.Get(cachedKey); found {
return val.(Producers)
}

r.RLock()
defer r.RUnlock()

if !r.needFilter(key, subkey) {
k := Registration{category, key, subkey}
r.cachedFindProducersResults.Set(cachedKey, ProducerMap2Slice(r.registrationMap[k]), cache.DefaultExpiration)
Expand Down

0 comments on commit 7d96abd

Please sign in to comment.