Faster path to check an IP address against known networks

We modify the critbitgo library to skip creating a route object we don't use.

The weaveworks-local modification can be removed if
https://github.com/k-sone/critbitgo/pull/7 is merged.
This commit is contained in:
Bryan Boreham
2018-04-14 17:34:47 +00:00
parent b8dae7768d
commit 04ceb0cc87
3 changed files with 22 additions and 9 deletions

View File

@@ -34,8 +34,8 @@ func (n Networks) AddCIDR(cidr string) error {
// Contains returns true if IP is in Networks.
func (n Networks) Contains(ip net.IP) bool {
network, _, _ := n.MatchIP(ip)
return network != nil
contained, _ := n.ContainedIP(ip)
return contained
}
// LocalAddresses returns a list of the local IP addresses.

View File

@@ -97,9 +97,25 @@ func (n *Net) MatchCIDR(s string) (route *net.IPNet, value interface{}, err erro
return
}
// Return a bool indicating whether a route would be found
func (n *Net) ContainedIP(ip net.IP) (contained bool, err error) {
k, _, err := n.matchIP(ip)
contained = k != nil
return
}
// Return a specific route by using the longest prefix matching.
// If `ip` is invalid IP, or a route is not found, `route` is nil.
func (n *Net) MatchIP(ip net.IP) (route *net.IPNet, value interface{}, err error) {
k, v, err := n.matchIP(ip)
if k != nil {
route = netKeyToIPNet(k)
value = v
}
return
}
func (n *Net) matchIP(ip net.IP) (k []byte, v interface{}, err error) {
var isV4 bool
ip, isV4, err = netValidateIP(ip)
if err != nil {
@@ -111,10 +127,7 @@ func (n *Net) MatchIP(ip net.IP) (route *net.IPNet, value interface{}, err error
} else {
mask = mask128
}
if k, v := n.match(netIPNetToKey(ip, mask)); k != nil {
route = netKeyToIPNet(k)
value = v
}
k, v = n.match(netIPNetToKey(ip, mask))
return
}

6
vendor/manifest vendored
View File

@@ -849,10 +849,10 @@
},
{
"importpath": "github.com/k-sone/critbitgo",
"repository": "https://github.com/k-sone/critbitgo",
"repository": "https://github.com/weaveworks/critbitgo",
"vcs": "git",
"revision": "327359a051d71948cb756142d112c7df283fac4d",
"branch": "master",
"revision": "aa814e9447366a571819e37affacbdc60e3af218",
"branch": "contained",
"notests": true
},
{