Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amend domain matcher with returning array of all matches #94

Merged
merged 1 commit into from
Aug 12, 2020

Conversation

Vigilans
Copy link
Contributor

Closes #92

采用了#92中的方案二:MatcherGroup返回所有匹配的记录,DomainMatcher保留所有插入的记录。

Comment on lines +113 to +116
ips := []net.Address{}
for _, id := range indices {
ips = append(ips, h.ips[id]...)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里DNS的static hosts现在若匹配到多个条目,会将IP整合到一起返回,不知是否合适。

idx := s.domainMatcher.Match(domain)
if idx > 0 {
indices := s.domainMatcher.Match(domain)
for _, idx := range indices {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里由于s.domainMatcher会返回多个匹配结果了,因此现在的Priority Matching可以顺序匹配多次,而非匹配一次失败后就直接Fallback到默认的轮询。

},
{
Domain: "x.y.com",
Result: []uint32{4, 6},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DomainMatcher新增了"x.y.com"匹配到两个结果(DNS服务器)的用例。

},
{
Domain: "x.y.com",
Result: []uint32{4, 6},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FullMatcher同样新增了"x.y.com"匹配到两个结果(DNS服务器)的用例。

Comment on lines +92 to 98
result = append(result, g.fullMatcher.Match(pattern)...)
result = append(result, g.domainMatcher.Match(pattern)...)
for _, e := range g.otherMatchers {
if e.m.Match(pattern) {
return e.id
result = append(result, e.id)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MatcherGroup现在会将fullMatcher, domainMatcher, otherMatchers全部尝试匹配一遍,并将结果整合在一起返回。返回的数组的优先级仍是 完整匹配 -> 子域名匹配 -> 正则匹配等。

由于domainMatcher设置时,fullMatcherotherMatchers常常为空,反之亦然,因此全部尝试匹配对性能的影响不是太大。

Comment on lines +882 to +914
{ // Will match server 1,2 and server 1 returns expected ip
ips, err := client.LookupIP("google.com")
if err != nil {
t.Fatal("unexpected error: ", err)
}

if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
t.Fatal(r)
}
}

{ // Will match server 1,2 and server 1 returns unexpected ip, then server 2 returns expected one
clientv4 := client.(feature_dns.IPv4Lookup)
ips, err := clientv4.LookupIPv4("ipv6.google.com")
if err != nil {
t.Fatal("unexpected error: ", err)
}

if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 7}}); r != "" {
t.Fatal(r)
}
}

{ // Will match server 1,2,3 and server 1,2 returns unexpected ip, then server 3 returns expected one
ips, err := client.LookupIP("api.google.com")
if err != nil {
t.Fatal("unexpected error: ", err)
}

if r := cmp.Diff(ips, []net.IP{{8, 8, 7, 7}}); r != "" {
t.Fatal(r)
}
}
Copy link
Contributor Author

@Vigilans Vigilans Aug 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DNS测试添加了三个用例:

  • 查询google.com,会匹配DNS 1和2,DNS 1返回的IP是Expected的,直接返回;
  • 查询ipv6.google.com,会匹配DNS 1和2,DNS 1返回的IP不是Expected的,这时并不直接Fallback到默认轮训,而是继续查询DNS 2,收到Expected IP并返回。
  • 查询api.google.com,会匹配DNS 1, 2和3,DNS 1和2返回的IP均不是Expected的,一直以Priority Matching查询到DNS 3,得到Expected IP并返回。 由于Full Matcher优先级更高,会直接匹配DNS 3

current.value = value
current.values = append(current.values, value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DomainMatcher的Trie树的结点不再以新值覆盖旧值,而是添加进值数组里。旧值在前,代表更高的优先级。

return m.matchers.Match(domain) > 0
return len(m.matchers.Match(domain)) > 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

路由规则现在变为查询该规则的Trie树后,返回的数组不为空即匹配。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal for amending domain matching logic
2 participants