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

[FIXED] Make sure on reverse match to compensate for wider target subjects. #4032

Merged
merged 1 commit into from Apr 6, 2023
Merged
Show file tree
Hide file tree
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
19 changes: 16 additions & 3 deletions server/sublist.go
@@ -1,4 +1,4 @@
// Copyright 2016-2020 The NATS Authors
// Copyright 2016-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -1529,13 +1529,13 @@ func (s *Sublist) ReverseMatch(subject string) *SublistResult {

result := &SublistResult{}

s.Lock()
s.RLock()
reverseMatchLevel(s.root, tokens, nil, result)
// Check for empty result.
if len(result.psubs) == 0 && len(result.qsubs) == 0 {
result = emptyResult
}
s.Unlock()
s.RUnlock()

return result
}
Expand All @@ -1553,9 +1553,22 @@ func reverseMatchLevel(l *level, toks []string, n *node, results *SublistResult)
for _, n := range l.nodes {
reverseMatchLevel(n.next, toks[i+1:], n, results)
}
if l.pwc != nil {
reverseMatchLevel(l.pwc.next, toks[i+1:], n, results)
}
if l.fwc != nil {
getAllNodes(l, results)
}
return
}
}
// If the sub tree has a fwc at this position, match as well.
if l.fwc != nil {
getAllNodes(l, results)
return
} else if l.pwc != nil {
reverseMatchLevel(l.pwc.next, toks[i+1:], n, results)
}
n = l.nodes[t]
if n == nil {
break
Expand Down
16 changes: 15 additions & 1 deletion server/sublist_test.go
@@ -1,4 +1,4 @@
// Copyright 2016-2020 The NATS Authors
// Copyright 2016-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -1478,6 +1478,20 @@ func TestSublistReverseMatch(t *testing.T) {
verifyMember(r.psubs, fooBarBazSub, t)
}

func TestSublistReverseMatchWider(t *testing.T) {
s := NewSublistWithCache()
sub := newSub("uplink.*.*.>")
s.Insert(sub)

r := s.ReverseMatch("uplink.1.*.*.>")
verifyLen(r.psubs, 1, t)
verifyMember(r.psubs, sub, t)

r = s.ReverseMatch("uplink.1.2.3.>")
verifyLen(r.psubs, 1, t)
verifyMember(r.psubs, sub, t)
}

func TestSublistMatchWithEmptyTokens(t *testing.T) {
for _, test := range []struct {
name string
Expand Down