From 698031fc2d9d9bf4ef9f5aa51253537c7039af5f Mon Sep 17 00:00:00 2001
From: "hang.jiang" <hang.jiang@daocloud.io>
Date: Tue, 4 Jul 2023 13:57:10 +0800
Subject: [PATCH] Stop ticker in time to avoid memory leak

Because it will cause memory leak if we do not stop ticker when the function has completed.

Signed-off-by: hang.jiang <hang.jiang@daocloud.io>
---
 pkg/nfd-topology-gc/nfd-nrt-gc.go                           | 1 +
 pkg/nfd-topology-updater/kubeletnotifier/kubeletnotifier.go | 1 +
 2 files changed, 2 insertions(+)

diff --git a/pkg/nfd-topology-gc/nfd-nrt-gc.go b/pkg/nfd-topology-gc/nfd-nrt-gc.go
index 86fbc313a..bb245ecdb 100644
--- a/pkg/nfd-topology-gc/nfd-nrt-gc.go
+++ b/pkg/nfd-topology-gc/nfd-nrt-gc.go
@@ -146,6 +146,7 @@ func (n *topologyGC) runGC() {
 // periodicGC runs garbage collector at every gcPeriod to make sure we haven't missed any node
 func (n *topologyGC) periodicGC(gcPeriod time.Duration) {
 	gcTrigger := time.NewTicker(gcPeriod)
+	defer gcTrigger.Stop()
 	for {
 		select {
 		case <-gcTrigger.C:
diff --git a/pkg/nfd-topology-updater/kubeletnotifier/kubeletnotifier.go b/pkg/nfd-topology-updater/kubeletnotifier/kubeletnotifier.go
index a1b0bffad..818985e03 100644
--- a/pkg/nfd-topology-updater/kubeletnotifier/kubeletnotifier.go
+++ b/pkg/nfd-topology-updater/kubeletnotifier/kubeletnotifier.go
@@ -70,6 +70,7 @@ func (n *Notifier) Run() {
 	timeEvents := make(<-chan time.Time)
 	if n.sleepInterval > 0 {
 		ticker := time.NewTicker(n.sleepInterval)
+		defer ticker.Stop()
 		timeEvents = ticker.C
 	}