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 02d48dd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions server/sublist.go
Expand Up @@ -540,6 +540,7 @@ func (s *Sublist) match(subject string, doLock bool) *SublistResult {
if doLock {
s.RLock()
}
cacheEnabled := s.cache != nil
r, ok := s.cache[subject]
if doLock {
s.RUnlock()
Expand Down Expand Up @@ -574,24 +575,32 @@ func (s *Sublist) match(subject string, doLock bool) *SublistResult {
var n int

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 02d48dd

Please sign in to comment.