Skip to content

Commit

Permalink
Don't take sublist write lock in match if sublist cache disabled
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
neilalexander committed Sep 27, 2023
1 parent 4c17eeb commit bafdefb
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions server/sublist.go
Expand Up @@ -573,25 +573,34 @@ func (s *Sublist) match(subject string, doLock bool) *SublistResult {
// Hold the read lock to avoid race between match and store.
var n int

cacheEnabled := s.cache != nil
if doLock {
s.Lock()
if cacheEnabled {
s.Lock()
} else {
s.RLock()
}
}

matchLevel(s.root, tokens, result)
// Check for empty result.
if len(result.psubs) == 0 && len(result.qsubs) == 0 {
result = emptyResult
}
if s.cache != nil {
if cacheEnabled {
s.cache[subject] = result
n = len(s.cache)
}
if doLock {
s.Unlock()
if cacheEnabled {
s.Unlock()
} else {
s.RUnlock()
}
}

// Reduce the cache count if we have exceeded our set maximum.
if n > slCacheMax && atomic.CompareAndSwapInt32(&s.ccSweep, 0, 1) {
if cacheEnabled && n > slCacheMax && atomic.CompareAndSwapInt32(&s.ccSweep, 0, 1) {
go s.reduceCacheCount()
}

Expand Down

0 comments on commit bafdefb

Please sign in to comment.