From d4a9a169f170c7ffc3924db2357b1c8ac9adaee1 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Sun, 20 Mar 2016 16:25:33 +0100 Subject: [PATCH] Optimize updating nodes --- main.go | 23 +++++++++++++---------- models/nodes.go | 24 +++++++++++++++++------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index aa8ee0b..8170752 100644 --- a/main.go +++ b/main.go @@ -78,19 +78,22 @@ func main() { // called for every parsed announced-message func onReceive(addr net.UDPAddr, res *data.ResponseData) { - if val := res.Neighbours; val != nil { - nodes.Get(val.NodeId).Neighbours = val - } - + // Search for NodeID + var nodeId string if val := res.NodeInfo; val != nil { - nodes.Get(val.NodeId).Nodeinfo = val + nodeId = val.NodeId + } else if val := res.Neighbours; val != nil { + nodeId = val.NodeId + } else if val := res.Statistics; val != nil { + nodeId = val.NodeId } - if val := res.Statistics; val != nil { - nodes.Get(val.NodeId).Statistics = val + // Updates nodes if NodeID found + if nodeId != "" { + nodes.Update(nodeId, res) + } - if statsDb != nil { - statsDb.Add(val) - } + if val := res.Statistics; val != nil && statsDb != nil { + statsDb.Add(val) } } diff --git a/models/nodes.go b/models/nodes.go index 6066c41..f9e636b 100644 --- a/models/nodes.go +++ b/models/nodes.go @@ -42,8 +42,8 @@ func NewNodes() *Nodes { return nodes } -// Get a Node by nodeid -func (nodes *Nodes) Get(nodeID string) *Node { +// Update a Node +func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) { now := time.Now() nodes.Lock() @@ -51,10 +51,7 @@ func (nodes *Nodes) Get(nodeID string) *Node { if node == nil { node = &Node{ - Firstseen: now, - Nodeinfo: &data.NodeInfo{}, - Statistics: &data.Statistics{}, - Neighbours: &data.Neighbours{}, + Firstseen: now, } nodes.List[nodeID] = node } @@ -62,7 +59,20 @@ func (nodes *Nodes) Get(nodeID string) *Node { node.Lastseen = now - return node + // Update neighbours + if val := res.Neighbours; val != nil { + node.Neighbours = val + } + + // Update nodeinfo + if val := res.NodeInfo; val != nil { + node.Nodeinfo = val + } + + // Update statistics + if val := res.Statistics; val != nil { + node.Statistics = val + } } // Saves the cached DB to json file periodically