improve performance on domain matcher

pull/1366/head
Darien Raymond 2018-11-02 12:14:41 +01:00
parent 787f37363b
commit 9de03d088e
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 36 additions and 4 deletions

View File

@ -51,22 +51,38 @@ func (g *DomainMatcherGroup) addMatcher(m domainMatcher, value uint32) {
} }
func (g *DomainMatcherGroup) Match(domain string) uint32 { func (g *DomainMatcherGroup) Match(domain string) uint32 {
if len(domain) == 0 {
return 0
}
current := g.root current := g.root
if current == nil { if current == nil {
return 0 return 0
} }
parts := breakDomain(domain) nextPart := func(idx int) int {
for i := len(parts) - 1; i >= 0; i-- { for i := idx - 1; i >= 0; i-- {
part := parts[i] if domain[i] == '.' {
if current.sub == nil { return i
}
}
return -1
}
idx := len(domain)
for {
if idx == -1 || current.sub == nil {
break break
} }
nidx := nextPart(idx)
part := domain[nidx+1 : idx]
next := current.sub[part] next := current.sub[part]
if next == nil { if next == nil {
break break
} }
current = next current = next
idx = nidx
} }
return current.value return current.value
} }

View File

@ -34,6 +34,22 @@ func TestDomainMatcherGroup(t *testing.T) {
Domain: "c.a.b.com", Domain: "c.a.b.com",
Result: 4, Result: 4,
}, },
{
Domain: "c.a..b.com",
Result: 0,
},
{
Domain: ".com",
Result: 0,
},
{
Domain: "com",
Result: 0,
},
{
Domain: "",
Result: 0,
},
} }
for _, testCase := range testCases { for _, testCase := range testCases {