-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathtopicselector.go
More file actions
103 lines (83 loc) · 2.61 KB
/
topicselector.go
File metadata and controls
103 lines (83 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package mercure
import (
"regexp"
"strings"
"github.com/maypok86/otter/v2"
"github.com/yosida95/uritemplate/v3"
)
// DefaultTopicSelectorStoreCacheSize is the default maximum number of entries in the cache.
const DefaultTopicSelectorStoreCacheSize = 2_560_000
type matchCacheKey struct {
topicSelector string
topic string
}
// TopicSelectorStore caches compiled templates to improve memory and CPU usage.
type TopicSelectorStore struct {
matchCache *otter.Cache[matchCacheKey, bool]
templateCache *otter.Cache[string, *regexp.Regexp]
}
// NewTopicSelectorStore creates a TopicSelectorStore.
// If cacheSize > 0, match results and compiled templates are cached.
func NewTopicSelectorStore(cacheSize int) (*TopicSelectorStore, error) {
if cacheSize <= 0 {
return &TopicSelectorStore{}, nil
}
matchCache, err := otter.New[matchCacheKey, bool](&otter.Options[matchCacheKey, bool]{
MaximumSize: cacheSize,
})
if err != nil {
return nil, err //nolint:wrapcheck
}
templateCache, err := otter.New[string, *regexp.Regexp](&otter.Options[string, *regexp.Regexp]{
MaximumSize: cacheSize / 10, // Templates are fewer but larger
})
if err != nil {
return nil, err //nolint:wrapcheck
}
return &TopicSelectorStore{matchCache: matchCache, templateCache: templateCache}, nil
}
func (tss *TopicSelectorStore) match(topic, topicSelector string) bool {
// Always do an exact matching comparison first
// Also check if the topic selector is the reserved keyword *
if topicSelector == "*" || topic == topicSelector {
return true
}
k := matchCacheKey{topicSelector: topicSelector, topic: topic}
if tss.matchCache != nil {
if value, found := tss.matchCache.GetIfPresent(k); found {
return value
}
}
r := tss.getRegexp(topicSelector)
if r == nil {
return false
}
// Use template.Regexp() instead of template.Match() for performance
// See https://github.com/yosida95/uritemplate/pull/7
match := r.MatchString(topic)
if tss.matchCache != nil {
tss.matchCache.Set(k, match)
}
return match
}
// getRegexp retrieves regexp for this template selector.
func (tss *TopicSelectorStore) getRegexp(topicSelector string) *regexp.Regexp {
// If it's definitely not a URI template, skip to save some resources
if !strings.Contains(topicSelector, "{") {
return nil
}
if tss.templateCache != nil {
if r, found := tss.templateCache.GetIfPresent(topicSelector); found {
return r
}
}
// If an error occurs, it's a raw string
if tpl, err := uritemplate.New(topicSelector); err == nil {
r := tpl.Regexp()
if tss.templateCache != nil {
tss.templateCache.Set(topicSelector, r)
}
return r
}
return nil
}