Skip to content

Commit

Permalink
In client and leafnode results cache, populate new entry after pruning (
Browse files Browse the repository at this point in the history
#5760)

Otherwise we might add the entry into the cache and then immediately
prune it afterwards, depending on luck/map iteration order.

Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
derekcollison authored Aug 7, 2024
2 parents 8543f3d + 5907334 commit 0a8f982
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4016,9 +4016,8 @@ func (c *client) processInboundClientMsg(msg []byte) (bool, bool) {
// Match may use the subject here to populate a cache, so can not use bytesToString here.
r = acc.sl.Match(string(c.pa.subject))
if len(r.psubs)+len(r.qsubs) > 0 {
c.in.results[string(c.pa.subject)] = r
// Prune the results cache. Keeps us from unbounded growth. Random delete.
if len(c.in.results) > maxResultCacheSize {
if len(c.in.results) >= maxResultCacheSize {
n := 0
for subject := range c.in.results {
delete(c.in.results, subject)
Expand All @@ -4027,6 +4026,8 @@ func (c *client) processInboundClientMsg(msg []byte) (bool, bool) {
}
}
}
// Then add the new cache entry.
c.in.results[string(c.pa.subject)] = r
}
}

Expand Down
5 changes: 3 additions & 2 deletions server/leafnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2698,9 +2698,8 @@ func (c *client) processInboundLeafMsg(msg []byte) {
// Go back to the sublist data structure.
if !ok {
r = c.acc.sl.Match(subject)
c.in.results[subject] = r
// Prune the results cache. Keeps us from unbounded growth. Random delete.
if len(c.in.results) > maxResultCacheSize {
if len(c.in.results) >= maxResultCacheSize {
n := 0
for subj := range c.in.results {
delete(c.in.results, subj)
Expand All @@ -2709,6 +2708,8 @@ func (c *client) processInboundLeafMsg(msg []byte) {
}
}
}
// Then add the new cache entry.
c.in.results[subject] = r
}

// Collect queue names if needed.
Expand Down

0 comments on commit 0a8f982

Please sign in to comment.