Skip to content

Commit

Permalink
Improve performance of interface name lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Geiselhardt-Herms committed Aug 18, 2023
1 parent 5b708f4 commit 4278d90
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func getQdiscMsgs(c *netlink.Conn) ([]netlink.Message, error) {
}

// See https://tools.ietf.org/html/rfc3549#section-3.1.3
func parseMessage(msg netlink.Message) (QdiscInfo, error) {
func parseMessage(msg netlink.Message, ifaceNamesByID map[int]string) (QdiscInfo, error) {
var m QdiscInfo
var s TC_Stats
var s2 TC_Stats2
Expand Down Expand Up @@ -257,13 +257,23 @@ func parseMessage(msg netlink.Message) (QdiscInfo, error) {
}
}

iface, err := net.InterfaceByIndex(int(ifaceIdx))
m.IfaceName = ifaceNamesByID[int(ifaceIdx)]

if err == nil {
m.IfaceName = iface.Name
return m, err
}

func getInterfaceNames() (map[int]string, error) {
ifas, err := net.Interfaces()
if err != nil {
return nil, err
}

return m, err
ifNamesByID := make(map[int]string)
for _, ifa := range ifas {
ifNamesByID[ifa.Index] = ifa.Name
}

return ifNamesByID, nil
}

func getAndParse(c *netlink.Conn) ([]QdiscInfo, error) {
Expand All @@ -275,8 +285,13 @@ func getAndParse(c *netlink.Conn) ([]QdiscInfo, error) {
return nil, err
}

ifNamesByID, err := getInterfaceNames()
if err != nil {
return nil, err
}

for _, msg := range msgs {
m, err := parseMessage(msg)
m, err := parseMessage(msg, ifNamesByID)

if err != nil {
return nil, err
Expand Down

0 comments on commit 4278d90

Please sign in to comment.