Refactor: A faster DomainMatcher implementation (#348)

Co-authored-by: DarthVader <61409963+darsvador@users.noreply.github.com>
This commit is contained in:
秋のかえで 2021-04-18 13:21:17 +08:00 committed by GitHub
parent bf94fb53ca
commit 7b7084f825
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 912 additions and 52 deletions

View file

@ -66,6 +66,24 @@ type DomainMatcher struct {
matchers strmatcher.IndexMatcher
}
func NewMphMatcherGroup(domains []*Domain) (*DomainMatcher, error) {
g := strmatcher.NewMphMatcherGroup()
for _, d := range domains {
matcherType, f := matcherTypeMap[d.Type]
if !f {
return nil, newError("unsupported domain type", d.Type)
}
_, err := g.AddPattern(d.Value, matcherType)
if err != nil {
return nil, err
}
}
g.Build()
return &DomainMatcher{
matchers: g,
}, nil
}
func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
g := new(strmatcher.MatcherGroup)
for _, d := range domains {
@ -82,7 +100,7 @@ func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
}
func (m *DomainMatcher) ApplyDomain(domain string) bool {
return len(m.matchers.Match(domain)) > 0
return len(m.matchers.Match(strings.ToLower(domain))) > 0
}
// Apply implements Condition.
@ -91,7 +109,7 @@ func (m *DomainMatcher) Apply(ctx routing.Context) bool {
if len(domain) == 0 {
return false
}
return m.ApplyDomain(strings.ToLower(domain))
return m.ApplyDomain(domain)
}
type MultiGeoIPMatcher struct {