From 04ceb0cc87443b63548d6d9bbb58341ca1efc2c5 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sat, 14 Apr 2018 17:34:47 +0000 Subject: [PATCH] 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. --- report/networks.go | 4 ++-- vendor/github.com/k-sone/critbitgo/net.go | 21 +++++++++++++++++---- vendor/manifest | 6 +++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/report/networks.go b/report/networks.go index d1b97ac738..4315f131f9 100644 --- a/report/networks.go +++ b/report/networks.go @@ -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. diff --git a/vendor/github.com/k-sone/critbitgo/net.go b/vendor/github.com/k-sone/critbitgo/net.go index 7e155b245d..9c822e40db 100644 --- a/vendor/github.com/k-sone/critbitgo/net.go +++ b/vendor/github.com/k-sone/critbitgo/net.go @@ -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 } diff --git a/vendor/manifest b/vendor/manifest index 8c743c1e63..7e9f07ba83 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -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 }, {