-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatcher.go
More file actions
184 lines (156 loc) · 3.29 KB
/
matcher.go
File metadata and controls
184 lines (156 loc) · 3.29 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package prometheus
import (
"regexp"
"sync"
)
// EndpointMatcher matches request paths to endpoint patterns
// Uses LRU cache to minimize regex matching overhead
type EndpointMatcher struct {
rules []compiledRule
cache *endpointCache
fallback string
mu sync.RWMutex
}
type compiledRule struct {
regex *regexp.Regexp
name string
}
// NewEndpointMatcher creates a new endpoint matcher from configuration
func NewEndpointMatcher(config EndpointPatternsConfig) (*EndpointMatcher, error) {
if !config.Enabled {
return &EndpointMatcher{
fallback: "other",
cache: newEndpointCache(1),
}, nil
}
rules := make([]compiledRule, 0, len(config.Rules))
for _, rule := range config.Rules {
regex, err := regexp.Compile(rule.Pattern)
if err != nil {
return nil, err
}
rules = append(rules, compiledRule{
regex: regex,
name: rule.Name,
})
}
cacheSize := config.CacheSize
if cacheSize <= 0 {
cacheSize = 10000
}
return &EndpointMatcher{
rules: rules,
cache: newEndpointCache(cacheSize),
fallback: "other",
}, nil
}
// Match returns the endpoint pattern for a given path
// Uses cached results when available, falls back to regex matching
func (em *EndpointMatcher) Match(path string) string {
// Check cache first (read lock)
if cached, ok := em.cache.Get(path); ok {
return cached
}
// Try rules
em.mu.RLock()
defer em.mu.RUnlock()
for _, rule := range em.rules {
if rule.regex.MatchString(path) {
em.cache.Set(path, rule.name)
return rule.name
}
}
// No match - use fallback
em.cache.Set(path, em.fallback)
return em.fallback
}
// endpointCache is a simple LRU cache for endpoint matching results
type endpointCache struct {
capacity int
items map[string]*cacheNode
head *cacheNode
tail *cacheNode
mu sync.RWMutex
}
type cacheNode struct {
key string
value string
prev *cacheNode
next *cacheNode
}
func newEndpointCache(capacity int) *endpointCache {
return &endpointCache{
capacity: capacity,
items: make(map[string]*cacheNode, capacity),
}
}
func (c *endpointCache) Get(key string) (string, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
if node, ok := c.items[key]; ok {
return node.value, true
}
return "", false
}
func (c *endpointCache) Set(key, value string) {
c.mu.Lock()
defer c.mu.Unlock()
// Check if key exists
if node, ok := c.items[key]; ok {
node.value = value
c.moveToFront(node)
return
}
// Create new node
node := &cacheNode{
key: key,
value: value,
}
// Add to cache
if c.head == nil {
c.head = node
c.tail = node
} else {
node.next = c.head
c.head.prev = node
c.head = node
}
c.items[key] = node
// Evict if over capacity
if len(c.items) > c.capacity {
c.removeTail()
}
}
func (c *endpointCache) moveToFront(node *cacheNode) {
if node == c.head {
return
}
// Remove from current position
if node.prev != nil {
node.prev.next = node.next
}
if node.next != nil {
node.next.prev = node.prev
}
if node == c.tail {
c.tail = node.prev
}
// Move to front
node.prev = nil
node.next = c.head
c.head.prev = node
c.head = node
}
func (c *endpointCache) removeTail() {
if c.tail == nil {
return
}
delete(c.items, c.tail.key)
if c.tail.prev != nil {
c.tail.prev.next = nil
c.tail = c.tail.prev
} else {
c.head = nil
c.tail = nil
}
}