-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Conversation
ips := []net.Address{} | ||
for _, id := range indices { | ||
ips = append(ips, h.ips[id]...) | ||
} |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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}, |
There was a problem hiding this comment.
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}, |
There was a problem hiding this comment.
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服务器)的用例。
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) | ||
} | ||
} |
There was a problem hiding this comment.
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
设置时,fullMatcher
与otherMatchers
常常为空,反之亦然,因此全部尝试匹配对性能的影响不是太大。
{ // 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) | ||
} | ||
} |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
路由规则现在变为查询该规则的Trie树后,返回的数组不为空即匹配。
Closes #92
采用了#92中的方案二:
MatcherGroup
返回所有匹配的记录,DomainMatcher
保留所有插入的记录。