Optimize updating nodes

This commit is contained in:
Julian Kornberger 2016-03-20 16:25:33 +01:00
parent 519b7ee292
commit d4a9a169f1
2 changed files with 30 additions and 17 deletions

23
main.go
View File

@ -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)
}
}

View File

@ -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