Merge pull request #730 from milosgajdos83/advert-lastseen

Update node.lastSeen properly. Set node.lastSeen when processing advert
This commit is contained in:
Asim Aslam 2019-09-04 18:13:43 +01:00 committed by GitHub
commit 46a9767648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -288,6 +288,8 @@ func (n *network) processNetChan(l tunnel.Listener) {
// switch on type of message and take action // switch on type of message and take action
switch m.Header["Micro-Method"] { switch m.Header["Micro-Method"] {
case "connect": case "connect":
// mark the time the message has been received
now := time.Now()
pbNetConnect := &pbNet.Connect{} pbNetConnect := &pbNet.Connect{}
if err := proto.Unmarshal(m.Body, pbNetConnect); err != nil { if err := proto.Unmarshal(m.Body, pbNetConnect); err != nil {
log.Debugf("Network tunnel [%s] connect unmarshal error: %v", NetworkChannel, err) log.Debugf("Network tunnel [%s] connect unmarshal error: %v", NetworkChannel, err)
@ -298,20 +300,28 @@ func (n *network) processNetChan(l tunnel.Listener) {
continue continue
} }
n.Lock() n.Lock()
log.Debugf("Network connect message received from: %s", pbNetConnect.Node.Id)
// if the entry already exists skip adding it // if the entry already exists skip adding it
if _, ok := n.neighbours[pbNetConnect.Node.Id]; ok { if neighbour, ok := n.neighbours[pbNetConnect.Node.Id]; ok {
// update lastSeen timestamp
if n.neighbours[pbNetConnect.Node.Id].lastSeen.Before(now) {
neighbour.lastSeen = now
}
n.Unlock() n.Unlock()
continue continue
} }
// add a new neighbour; // add a new neighbour
// NOTE: new node does not have any neighbours // NOTE: new node does not have any neighbours
n.neighbours[pbNetConnect.Node.Id] = &node{ n.neighbours[pbNetConnect.Node.Id] = &node{
id: pbNetConnect.Node.Id, id: pbNetConnect.Node.Id,
address: pbNetConnect.Node.Address, address: pbNetConnect.Node.Address,
neighbours: make(map[string]*node), neighbours: make(map[string]*node),
lastSeen: now,
} }
n.Unlock() n.Unlock()
case "neighbour": case "neighbour":
// mark the time the message has been received
now := time.Now()
pbNetNeighbour := &pbNet.Neighbour{} pbNetNeighbour := &pbNet.Neighbour{}
if err := proto.Unmarshal(m.Body, pbNetNeighbour); err != nil { if err := proto.Unmarshal(m.Body, pbNetNeighbour); err != nil {
log.Debugf("Network tunnel [%s] neighbour unmarshal error: %v", NetworkChannel, err) log.Debugf("Network tunnel [%s] neighbour unmarshal error: %v", NetworkChannel, err)
@ -322,15 +332,19 @@ func (n *network) processNetChan(l tunnel.Listener) {
continue continue
} }
n.Lock() n.Lock()
log.Debugf("Network neighbour message received from: %s", pbNetNeighbour.Node.Id)
// only add the neighbour if it's not already in the neighbourhood // only add the neighbour if it's not already in the neighbourhood
if _, ok := n.neighbours[pbNetNeighbour.Node.Id]; !ok { if _, ok := n.neighbours[pbNetNeighbour.Node.Id]; !ok {
neighbour := &node{ n.neighbours[pbNetNeighbour.Node.Id] = &node{
id: pbNetNeighbour.Node.Id, id: pbNetNeighbour.Node.Id,
address: pbNetNeighbour.Node.Address, address: pbNetNeighbour.Node.Address,
neighbours: make(map[string]*node), neighbours: make(map[string]*node),
lastSeen: time.Now(), lastSeen: now,
} }
n.neighbours[pbNetNeighbour.Node.Id] = neighbour }
// update lastSeen timestamp
if n.neighbours[pbNetNeighbour.Node.Id].lastSeen.Before(now) {
n.neighbours[pbNetNeighbour.Node.Id].lastSeen = now
} }
// update/store the neighbour node neighbours // update/store the neighbour node neighbours
for _, pbNeighbour := range pbNetNeighbour.Neighbours { for _, pbNeighbour := range pbNetNeighbour.Neighbours {
@ -430,6 +444,7 @@ func (n *network) pruneNode(id string) error {
return err return err
} }
// delete the found routes // delete the found routes
log.Logf("Network deleting routes originated by router: %s", id)
for _, route := range routes { for _, route := range routes {
if err := n.Router.Table().Delete(route); err != nil && err != router.ErrRouteNotFound { if err := n.Router.Table().Delete(route); err != nil && err != router.ErrRouteNotFound {
return err return err
@ -452,8 +467,10 @@ func (n *network) prune() {
case <-prune.C: case <-prune.C:
n.Lock() n.Lock()
for id, node := range n.neighbours { for id, node := range n.neighbours {
nodeAge := time.Since(node.lastSeen) if id == n.options.Id {
if nodeAge > PruneTime { continue
}
if time.Since(node.lastSeen) > PruneTime {
log.Debugf("Network deleting node %s: reached prune time threshold", id) log.Debugf("Network deleting node %s: reached prune time threshold", id)
if err := n.pruneNode(id); err != nil { if err := n.pruneNode(id); err != nil {
log.Debugf("Network failed to prune the node %s: %v", id, err) log.Debugf("Network failed to prune the node %s: %v", id, err)
@ -555,14 +572,19 @@ func (n *network) processCtrlChan(l tunnel.Listener) {
// switch on type of message and take action // switch on type of message and take action
switch m.Header["Micro-Method"] { switch m.Header["Micro-Method"] {
case "advert": case "advert":
now := time.Now()
pbRtrAdvert := &pbRtr.Advert{} pbRtrAdvert := &pbRtr.Advert{}
if err := proto.Unmarshal(m.Body, pbRtrAdvert); err != nil { if err := proto.Unmarshal(m.Body, pbRtrAdvert); err != nil {
log.Debugf("Network fail to unmarshal advert message: %v", err) log.Debugf("Network fail to unmarshal advert message: %v", err)
continue continue
} }
// don't process your own messages
if pbRtrAdvert.Id == n.options.Id {
continue
}
// loookup advertising node in our neighbourhood // loookup advertising node in our neighbourhood
n.RLock() n.RLock()
log.Debugf("Network received advert message from: %s", pbRtrAdvert.Id)
advertNode, ok := n.neighbours[pbRtrAdvert.Id] advertNode, ok := n.neighbours[pbRtrAdvert.Id]
if !ok { if !ok {
// advertising node has not been registered as our neighbour, yet // advertising node has not been registered as our neighbour, yet
@ -570,6 +592,7 @@ func (n *network) processCtrlChan(l tunnel.Listener) {
advertNode = &node{ advertNode = &node{
id: pbRtrAdvert.Id, id: pbRtrAdvert.Id,
neighbours: make(map[string]*node), neighbours: make(map[string]*node),
lastSeen: now,
} }
n.neighbours[pbRtrAdvert.Id] = advertNode n.neighbours[pbRtrAdvert.Id] = advertNode
} }
@ -785,6 +808,7 @@ func (n *network) Connect() error {
Body: body, Body: body,
} }
log.Debugf("Network sending connect message: %s", node.Id)
if err := netClient.Send(&m); err != nil { if err := netClient.Send(&m); err != nil {
log.Debugf("Network failed to send connect messsage: %v", err) log.Debugf("Network failed to send connect messsage: %v", err)
} }