From ef1e06c262d0d40e9451d3f59372099e592e6ec7 Mon Sep 17 00:00:00 2001 From: neaggarw Date: Fri, 11 Jun 2021 14:29:27 -0700 Subject: [PATCH 1/4] Avoid creating timer each time SyncHostVersion is called, instead use timer channel --- cns/service/main.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cns/service/main.go b/cns/service/main.go index fff500df7d..0df91fa143 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -743,9 +743,12 @@ func InitializeMultiTenantController(httpRestService cns.HTTPService, cnsconfig rootCxt := context.Background() go func() { // Periodically poll vfp programmed NC version from NMAgent + timerChannel := time.NewTicker(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond).C for { - <-time.NewTicker(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond).C - httpRestServiceImpl.SyncHostNCVersion(rootCxt, cnsconfig.ChannelMode, cnsconfig.SyncHostNCTimeoutMs) + select { + case <-timerChannel: + httpRestServiceImpl.SyncHostNCVersion(rootCxt, cnsconfig.ChannelMode, cnsconfig.SyncHostNCTimeoutMs) + } } }() @@ -842,9 +845,12 @@ func InitializeCRDState(httpRestService cns.HTTPService, cnsconfig configuration rootCxt := context.Background() go func() { // Periodically poll vfp programmed NC version from NMAgent + timerChannel := time.NewTicker(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond).C for { - <-time.NewTicker(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond).C - httpRestServiceImplementation.SyncHostNCVersion(rootCxt, cnsconfig.ChannelMode, cnsconfig.SyncHostNCTimeoutMs) + select { + case <-timerChannel: + httpRestServiceImplementation.SyncHostNCVersion(rootCxt, cnsconfig.ChannelMode, cnsconfig.SyncHostNCTimeoutMs) + } } logger.Printf("[Azure CNS] Exiting SyncHostNCVersion") From 52818b4e90e581e5385fb447a14ea7b6923513b4 Mon Sep 17 00:00:00 2001 From: neaggarw Date: Mon, 14 Jun 2021 09:21:03 -0700 Subject: [PATCH 2/4] incorporate feedback --- cns/service/main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cns/service/main.go b/cns/service/main.go index 0df91fa143..e1072eb55d 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -845,11 +845,13 @@ func InitializeCRDState(httpRestService cns.HTTPService, cnsconfig configuration rootCxt := context.Background() go func() { // Periodically poll vfp programmed NC version from NMAgent - timerChannel := time.NewTicker(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond).C + ticker := time.NewTicker(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond) for { select { - case <-timerChannel: + case <-ticker.C: httpRestServiceImplementation.SyncHostNCVersion(rootCxt, cnsconfig.ChannelMode, cnsconfig.SyncHostNCTimeoutMs) + case <-rootCxt.Done(): + return } } From c680f22471b71b030fd1eae1723910f18bab69cc Mon Sep 17 00:00:00 2001 From: neaggarw Date: Mon, 14 Jun 2021 11:45:25 -0700 Subject: [PATCH 3/4] Use time.Tick instead of NewTicker --- cns/service/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cns/service/main.go b/cns/service/main.go index e1072eb55d..a8fcd5b1e2 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -845,10 +845,10 @@ func InitializeCRDState(httpRestService cns.HTTPService, cnsconfig configuration rootCxt := context.Background() go func() { // Periodically poll vfp programmed NC version from NMAgent - ticker := time.NewTicker(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond) + tickerChannel := time.Tick(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond) for { select { - case <-ticker.C: + case <-tickerChannel: httpRestServiceImplementation.SyncHostNCVersion(rootCxt, cnsconfig.ChannelMode, cnsconfig.SyncHostNCTimeoutMs) case <-rootCxt.Done(): return From d3314a1a7b79af36ceaf9c38a2c9ef0949c2fc0a Mon Sep 17 00:00:00 2001 From: neaggarw Date: Mon, 14 Jun 2021 12:57:29 -0700 Subject: [PATCH 4/4] Handle SyncNodeStatus too --- cns/service/main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cns/service/main.go b/cns/service/main.go index a8fcd5b1e2..c6f2f20f7b 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -567,9 +567,12 @@ func main() { } go func(ep, vnet, node string) { // Periodically poll DNC for node updates + tickerChannel := time.Tick(time.Duration(cnsconfig.ManagedSettings.NodeSyncIntervalInSeconds) * time.Second) for { - <-time.NewTicker(time.Duration(cnsconfig.ManagedSettings.NodeSyncIntervalInSeconds) * time.Second).C - httpRestService.SyncNodeStatus(ep, vnet, node, json.RawMessage{}) + select { + case <-tickerChannel: + httpRestService.SyncNodeStatus(ep, vnet, node, json.RawMessage{}) + } } }(privateEndpoint, infravnet, nodeID) } @@ -743,11 +746,13 @@ func InitializeMultiTenantController(httpRestService cns.HTTPService, cnsconfig rootCxt := context.Background() go func() { // Periodically poll vfp programmed NC version from NMAgent - timerChannel := time.NewTicker(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond).C + tickerChannel := time.Tick(cnsconfig.SyncHostNCVersionIntervalMs * time.Millisecond) for { select { - case <-timerChannel: + case <-tickerChannel: httpRestServiceImpl.SyncHostNCVersion(rootCxt, cnsconfig.ChannelMode, cnsconfig.SyncHostNCTimeoutMs) + case <-rootCxt.Done(): + return } } }()