Browse Source

refactor manage zombie logic

Anish Mukherjee 2 years ago
parent
commit
10ffb517c1
2 changed files with 13 additions and 3 deletions
  1. 4 2
      logic/zombie.go
  2. 9 1
      main.go

+ 4 - 2
logic/zombie.go

@@ -46,7 +46,7 @@ func CheckZombies(newnode *models.Node, mac net.HardwareAddr) {
 }
 }
 
 
 // ManageZombies - goroutine which adds/removes/deletes nodes from the zombie node quarantine list
 // ManageZombies - goroutine which adds/removes/deletes nodes from the zombie node quarantine list
-func ManageZombies(ctx context.Context) {
+func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
 	logger.Log(2, "Zombie management started")
 	logger.Log(2, "Zombie management started")
 	InitializeZombies()
 	InitializeZombies()
 	for {
 	for {
@@ -81,11 +81,13 @@ func ManageZombies(ctx context.Context) {
 						zombies = append(zombies[:i], zombies[i+1:]...)
 						zombies = append(zombies[:i], zombies[i+1:]...)
 						continue
 						continue
 					}
 					}
-					if time.Since(node.LastCheckIn) > time.Minute*ZOMBIE_DELETE_TIME {
+					if time.Since(node.LastCheckIn) > time.Minute*ZOMBIE_DELETE_TIME || time.Now().After(node.ExpirationDateTime) {
 						if err := DeleteNode(&node, true); err != nil {
 						if err := DeleteNode(&node, true); err != nil {
 							logger.Log(1, "error deleting zombie node", zombies[i].String(), err.Error())
 							logger.Log(1, "error deleting zombie node", zombies[i].String(), err.Error())
 							continue
 							continue
 						}
 						}
+						node.Action = models.NODE_DELETE
+						peerUpdate <- &node
 						logger.Log(1, "deleting zombie node", node.ID.String())
 						logger.Log(1, "deleting zombie node", node.ID.String())
 						zombies = append(zombies[:i], zombies[i+1:]...)
 						zombies = append(zombies[:i], zombies[i+1:]...)
 					}
 					}

+ 9 - 1
main.go

@@ -191,7 +191,15 @@ func runMessageQueue(wg *sync.WaitGroup) {
 	mq.SetupMQTT()
 	mq.SetupMQTT()
 	ctx, cancel := context.WithCancel(context.Background())
 	ctx, cancel := context.WithCancel(context.Background())
 	go mq.Keepalive(ctx)
 	go mq.Keepalive(ctx)
-	go logic.ManageZombies(ctx)
+	go func() {
+		peerUpdate := make(chan *models.Node)
+		go logic.ManageZombies(ctx, peerUpdate)
+		for nodeUpdate := range peerUpdate {
+			if err := mq.NodeUpdate(nodeUpdate); err != nil {
+				logger.Log(0, "failed to send peer update for deleted node: ", nodeUpdate.ID.String())
+			}
+		}
+	}()
 	go logic.PurgePendingNodes(ctx)
 	go logic.PurgePendingNodes(ctx)
 	quit := make(chan os.Signal, 1)
 	quit := make(chan os.Signal, 1)
 	signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
 	signal.Notify(quit, syscall.SIGTERM, os.Interrupt)