performance: in Docker reporter, reduce IP type conversions.

The code was converting IP addresses to strings and back again.
This commit is contained in:
Bryan Boreham
2019-09-25 20:15:34 +00:00
parent 2941850a75
commit cbbb2ff24c
2 changed files with 15 additions and 14 deletions
+7 -9
View File
@@ -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 {
+8 -5
View File
@@ -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...))
}