Просмотр исходного кода

NET-541

* Updated both zombie cleanup and expired nodes cleanup go routines to use tick timers for execution.
Farukh Khan 2 лет назад
Родитель
Сommit
7797f4ea1c
2 измененных файлов с 19 добавлено и 3 удалено
  1. 9 2
      logic/nodes.go
  2. 10 1
      logic/zombie.go

+ 9 - 2
logic/nodes.go

@@ -448,12 +448,18 @@ func GetAllNodesAPI(nodes []models.Node) []models.ApiNode {
 
 // DeleteExpiredNodes - goroutine which deletes nodes which are expired
 func DeleteExpiredNodes(ctx context.Context, peerUpdate chan *models.Node) {
+	// Delete Expired Nodes Every Hour
+	duration := time.Hour
+	delay := time.NewTimer(duration)
+
 	for {
 		select {
 		case <-ctx.Done():
+			if !delay.Stop() {
+				<-delay.C
+			}
 			return
-		case <-time.After(time.Hour):
-			// Delete Expired Nodes Every Hour
+		case <-delay.C:
 			allnodes, err := GetAllNodes()
 			if err != nil {
 				slog.Error("failed to retrieve all nodes", "error", err.Error())
@@ -471,6 +477,7 @@ func DeleteExpiredNodes(ctx context.Context, peerUpdate chan *models.Node) {
 					slog.Info("deleting expired node", "nodeid", node.ID.String())
 				}
 			}
+			delay.Reset(duration)
 		}
 	}
 }

+ 10 - 1
logic/zombie.go

@@ -77,16 +77,24 @@ func checkForZombieHosts(h *models.Host) {
 func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
 	logger.Log(2, "Zombie management started")
 	InitializeZombies()
+
+	// Zombie Nodes Cleanup Four Times a Day
+	duration := time.Hour * ZOMBIE_TIMEOUT
+	delay := time.NewTimer(duration)
+
 	for {
 		select {
 		case <-ctx.Done():
 			close(peerUpdate)
+			if !delay.Stop() {
+				<-delay.C
+			}
 			return
 		case id := <-newZombie:
 			zombies = append(zombies, id)
 		case id := <-newHostZombie:
 			hostZombies = append(hostZombies, id)
-		case <-time.After(time.Hour * ZOMBIE_TIMEOUT): // run this check 4 times a day
+		case <-delay.C: // run this check 4 times a day
 			logger.Log(3, "checking for zombie nodes")
 			if len(zombies) > 0 {
 				for i := len(zombies) - 1; i >= 0; i-- {
@@ -126,6 +134,7 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
 					}
 				}
 			}
+			delay.Reset(duration)
 		}
 	}
 }