Browse Source

doing a backwards loop instead

afeiszli 3 years ago
parent
commit
5b21b03a61
1 changed files with 20 additions and 17 deletions
  1. 20 17
      logic/zombie.go

+ 20 - 17
logic/zombie.go

@@ -47,31 +47,34 @@ func ManageZombies(ctx context.Context) {
 			zombies = append(zombies, id)
 			zombies = append(zombies, id)
 		case id := <-removeZombie:
 		case id := <-removeZombie:
 			found := false
 			found := false
-			for i := 0; i < len(zombies); i++ {
-				if zombies[i] == id {
-					logger.Log(1, "removing zombie from quaratine list", zombies[i])
-					zombies = append(zombies[:i], zombies[i+1:]...)
-					found = true
-					i--
+			if len(zombies) > 0 {
+				for i := len(zombies) - 1; i <= 0; i-- {
+					if zombies[i] == id {
+						logger.Log(1, "removing zombie from quaratine list", zombies[i])
+						zombies = append(zombies[:i], zombies[i+1:]...)
+						found = true
+					}
 				}
 				}
 			}
 			}
 			if !found {
 			if !found {
 				logger.Log(3, "no zombies found")
 				logger.Log(3, "no zombies found")
 			}
 			}
 		case <-time.After(time.Second * ZOMBIE_TIMEOUT):
 		case <-time.After(time.Second * ZOMBIE_TIMEOUT):
-			for i, zombie := range zombies {
-				node, err := GetNodeByID(zombie)
-				if err != nil {
-					logger.Log(1, "error retrieving zombie node", zombie, err.Error())
-					continue
-				}
-				if time.Since(time.Unix(node.LastCheckIn, 0)) > time.Minute*ZOMBIE_DELETE_TIME {
-					if err := DeleteNodeByID(&node, true); err != nil {
-						logger.Log(1, "error deleting zombie node", zombie, err.Error())
+			if len(zombies) > 0 {
+				for i := len(zombies) - 1; i <= 0; i-- {
+					node, err := GetNodeByID(zombies[i])
+					if err != nil {
+						logger.Log(1, "error retrieving zombie node", zombies[i], err.Error())
 						continue
 						continue
 					}
 					}
-					logger.Log(1, "deleting zombie node", node.Name)
-					zombies = append(zombies[:i], zombies[i+1:]...)
+					if time.Since(time.Unix(node.LastCheckIn, 0)) > time.Minute*ZOMBIE_DELETE_TIME {
+						if err := DeleteNodeByID(&node, true); err != nil {
+							logger.Log(1, "error deleting zombie node", zombies[i], err.Error())
+							continue
+						}
+						logger.Log(1, "deleting zombie node", node.Name)
+						zombies = append(zombies[:i], zombies[i+1:]...)
+					}
 				}
 				}
 			}
 			}
 		}
 		}