Browse Source

NET-541 (#2544)

* NET-541

* Updated both zombie cleanup and expired nodes cleanup go routines to use tick timers for execution.

* NET-541 updated to ticker

* Changed from timer to ticker for periodic execution of zombie cleanup and expired nodes cleanup go routines.

---------

Co-authored-by: Abhishek K <[email protected]>
Farukh Khan 1 year ago
parent
commit
9870920aac
2 changed files with 10 additions and 3 deletions
  1. 4 2
      logic/nodes.go
  2. 6 1
      logic/zombie.go

+ 4 - 2
logic/nodes.go

@@ -425,12 +425,14 @@ 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
+	ticker := time.NewTicker(time.Hour)
 	for {
 		select {
 		case <-ctx.Done():
+			ticker.Stop()
 			return
-		case <-time.After(time.Hour):
-			// Delete Expired Nodes Every Hour
+		case <-ticker.C:
 			allnodes, err := GetAllNodes()
 			if err != nil {
 				slog.Error("failed to retrieve all nodes", "error", err.Error())

+ 6 - 1
logic/zombie.go

@@ -77,16 +77,21 @@ 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
+	ticker := time.NewTicker(time.Hour * ZOMBIE_TIMEOUT)
+
 	for {
 		select {
 		case <-ctx.Done():
+			ticker.Stop()
 			close(peerUpdate)
 			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 <-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-- {