Sfoglia il codice sorgente

NET-541 updated to ticker

* Changed from timer to ticker for periodic execution of zombie cleanup and expired nodes cleanup go routines.
Farukh Khan 2 anni fa
parent
commit
146beec18f
2 ha cambiato i file con 6 aggiunte e 15 eliminazioni
  1. 3 8
      logic/nodes.go
  2. 3 7
      logic/zombie.go

+ 3 - 8
logic/nodes.go

@@ -449,17 +449,13 @@ 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)
-
+	ticker := time.NewTicker(time.Hour)
 	for {
 		select {
 		case <-ctx.Done():
-			if !delay.Stop() {
-				<-delay.C
-			}
+			ticker.Stop()
 			return
-		case <-delay.C:
+		case <-ticker.C:
 			allnodes, err := GetAllNodes()
 			if err != nil {
 				slog.Error("failed to retrieve all nodes", "error", err.Error())
@@ -477,7 +473,6 @@ func DeleteExpiredNodes(ctx context.Context, peerUpdate chan *models.Node) {
 					slog.Info("deleting expired node", "nodeid", node.ID.String())
 				}
 			}
-			delay.Reset(duration)
 		}
 	}
 }

+ 3 - 7
logic/zombie.go

@@ -79,22 +79,19 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
 	InitializeZombies()
 
 	// Zombie Nodes Cleanup Four Times a Day
-	duration := time.Hour * ZOMBIE_TIMEOUT
-	delay := time.NewTimer(duration)
+	ticker := time.NewTicker(time.Hour * ZOMBIE_TIMEOUT)
 
 	for {
 		select {
 		case <-ctx.Done():
+			ticker.Stop()
 			close(peerUpdate)
-			if !delay.Stop() {
-				<-delay.C
-			}
 			return
 		case id := <-newZombie:
 			zombies = append(zombies, id)
 		case id := <-newHostZombie:
 			hostZombies = append(hostZombies, id)
-		case <-delay.C: // run this check 4 times a day
+		case <-ticker.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-- {
@@ -134,7 +131,6 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
 					}
 				}
 			}
-			delay.Reset(duration)
 		}
 	}
 }