1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-05-08 13:39:08 -04:00

improve performance of domain matcher

This commit is contained in:
Darien Raymond
2018-08-19 21:04:15 +02:00
parent 2ab6da28ba
commit edcf564dd7
7 changed files with 234 additions and 106 deletions

View File

@@ -1,6 +1,12 @@
package strmatcher
import "regexp"
import (
"regexp"
"sync"
"time"
"v2ray.com/core/common/task"
)
type Matcher interface {
Match(string) bool
@@ -36,6 +42,10 @@ func (t Type) New(pattern string) (Matcher, error) {
}
}
type IndexMatcher interface {
Match(pattern string) uint32
}
type matcherEntry struct {
m Matcher
id uint32
@@ -44,6 +54,7 @@ type matcherEntry struct {
type MatcherGroup struct {
count uint32
fullMatchers map[string]uint32
domainMatcher DomainMatcherGroup
otherMatchers []matcherEntry
}
@@ -58,9 +69,12 @@ func (g *MatcherGroup) Add(m Matcher) uint32 {
c := g.count
g.count++
if fm, ok := m.(fullMatcher); ok {
g.fullMatchers[string(fm)] = c
} else {
switch tm := m.(type) {
case fullMatcher:
g.fullMatchers[string(tm)] = c
case domainMatcher:
g.domainMatcher.Add(string(tm), c)
default:
g.otherMatchers = append(g.otherMatchers, matcherEntry{
m: m,
id: c,
@@ -87,3 +101,60 @@ func (g *MatcherGroup) Match(pattern string) uint32 {
func (g *MatcherGroup) Size() uint32 {
return g.count
}
type cacheEntry struct {
timestamp time.Time
result uint32
}
type CachedMatcherGroup struct {
sync.Mutex
group *MatcherGroup
cache map[string]cacheEntry
cleanup *task.Periodic
}
func NewCachedMatcherGroup(g *MatcherGroup) *CachedMatcherGroup {
r := &CachedMatcherGroup{
group: g,
cache: make(map[string]cacheEntry),
}
r.cleanup = &task.Periodic{
Interval: time.Second * 30,
Execute: func() error {
r.Lock()
defer r.Unlock()
expire := time.Now().Add(-1 * time.Second * 60)
for p, e := range r.cache {
if e.timestamp.Before(expire) {
delete(r.cache, p)
}
}
return nil
},
}
return r
}
func (g *CachedMatcherGroup) Match(pattern string) uint32 {
g.Lock()
defer g.Unlock()
r, f := g.cache[pattern]
if f {
r.timestamp = time.Now()
g.cache[pattern] = r
return r.result
}
mr := g.group.Match(pattern)
g.cache[pattern] = cacheEntry{
result: mr,
timestamp: time.Now(),
}
return mr
}