From cbbb2ff24c2d549926b21a94680361af74d30281 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 25 Sep 2019 20:05:40 +0000 Subject: [PATCH] performance: in Docker reporter, reduce IP type conversions. The code was converting IP addresses to strings and back again. --- probe/docker/container.go | 16 +++++++--------- probe/docker/reporter.go | 13 ++++++++----- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/probe/docker/container.go b/probe/docker/container.go index 9c81a250f..f4c7f7d8a 100644 --- a/probe/docker/container.go +++ b/probe/docker/container.go @@ -226,19 +226,14 @@ func (c *container) NetworkMode() (string, bool) { return "", false } -func addScopeToIPs(hostID string, ips []string) []string { +func addScopeToIPs(hostID string, ips []net.IP) []string { ipsWithScopes := []string{} for _, ip := range ips { - ipsWithScopes = append(ipsWithScopes, report.MakeAddressNodeID(hostID, ip)) + ipsWithScopes = append(ipsWithScopes, report.MakeAddressNodeIDB(hostID, ip)) } return ipsWithScopes } -func isIPv4(addr string) bool { - ip := net.ParseIP(addr) - return ip != nil && ip.To4() != nil -} - func (c *container) NetworkInfo(localAddrs []net.IP) report.Sets { c.RLock() defer c.RUnlock() @@ -278,13 +273,16 @@ func (c *container) NetworkInfo(localAddrs []net.IP) report.Sets { // Filter out IPv6 addresses; nothing works with IPv6 yet ipv4s := []string{} + ipv4ips := []net.IP{} for _, ip := range ips { - if isIPv4(ip) { + ipaddr := net.ParseIP(ip) + if ipaddr != nil && ipaddr.To4() != nil { ipv4s = append(ipv4s, ip) + ipv4ips = append(ipv4ips, ipaddr) } } // Treat all Docker IPs as local scoped. - ipsWithScopes := addScopeToIPs(c.hostID, ipv4s) + ipsWithScopes := addScopeToIPs(c.hostID, ipv4ips) s := report.MakeSets() if len(networks) > 0 { diff --git a/probe/docker/reporter.go b/probe/docker/reporter.go index b41899d60..70dade7df 100644 --- a/probe/docker/reporter.go +++ b/probe/docker/reporter.go @@ -192,16 +192,19 @@ func (r *Reporter) Report() (report.Report, error) { return result, nil } -func getLocalIPs() ([]string, error) { +// Get local addresses both as strings and IP addresses, in matched slices +func getLocalIPs() ([]string, []net.IP, error) { ipnets, err := report.GetLocalNetworks() if err != nil { - return nil, err + return nil, nil, err } ips := []string{} + addrs := []net.IP{} for _, ipnet := range ipnets { ips = append(ips, ipnet.IP.String()) + addrs = append(addrs, ipnet.IP) } - return ips, nil + return ips, addrs, nil } func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology { @@ -222,10 +225,10 @@ func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology { // is recursive to deal with people who decide to be clever. { hostNetworkInfo := report.MakeSets() - if hostIPs, err := getLocalIPs(); err == nil { + if hostStrs, hostIPs, err := getLocalIPs(); err == nil { hostIPsWithScopes := addScopeToIPs(r.hostID, hostIPs) hostNetworkInfo = hostNetworkInfo. - Add(ContainerIPs, report.MakeStringSet(hostIPs...)). + Add(ContainerIPs, report.MakeStringSet(hostStrs...)). Add(ContainerIPsWithScopes, report.MakeStringSet(hostIPsWithScopes...)) }