Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't take sublist write lock in match if sublist cache disabled #4594

Merged
merged 1 commit into from Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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