From 69af34ffed18feb1e838d8b18fdd8efff1f67640 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Thu, 6 Feb 2020 01:05:50 -0800 Subject: [PATCH] Separated cns heartbeat to separate thread. --- cns/logger/heartbeat.go | 29 +++++++++++++++++++---------- cns/service/main.go | 6 ++++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cns/logger/heartbeat.go b/cns/logger/heartbeat.go index 4fb973a91a..15ee429f23 100644 --- a/cns/logger/heartbeat.go +++ b/cns/logger/heartbeat.go @@ -27,20 +27,31 @@ const ( var codeRegex = regexp.MustCompile(`Code:(\w*)`) +func SendHeartBeat(heartbeatIntervalInMins int, stopheartbeat chan bool) { + heartbeat := time.NewTicker(time.Minute * time.Duration(heartbeatIntervalInMins)).C + metric := aitelemetry.Metric{ + Name: HeartBeatMetricStr, + // This signifies 1 heartbeat is sent. Sum of this metric will give us number of heartbeats received + Value: 1.0, + CustomDimensions: make(map[string]string), + } + for { + select { + case <-heartbeat: + SendMetric(metric) + case <-stopheartbeat: + return + } + } +} + // SendCnsTelemetry - handles cns telemetry reports -func SendCnsTelemetry(reports chan interface{}, heartbeatIntervalInMins int, telemetryStopProcessing chan bool) { +func SendToTelemetryService(reports chan interface{}, telemetryStopProcessing chan bool) { CONNECT: tb := telemetry.NewTelemetryBuffer("") tb.ConnectToTelemetryService(telemetryNumRetries, telemetryWaitTimeInMilliseconds) if tb.Connected { - heartbeat := time.NewTicker(time.Minute * time.Duration(heartbeatIntervalInMins)).C - metric := aitelemetry.Metric{ - Name: HeartBeatMetricStr, - // This signifies 1 heartbeat is sent. Sum of this metric will give us number of heartbeats received - Value: 1.0, - CustomDimensions: make(map[string]string), - } reportMgr := telemetry.ReportManager{ ContentType: telemetry.ContentType, @@ -52,8 +63,6 @@ CONNECT: for { select { - case <-heartbeat: - SendMetric(metric) case msg := <-reports: codeStr := codeRegex.FindString(msg.(string)) if len(codeStr) > errorcodePrefix { diff --git a/cns/service/main.go b/cns/service/main.go index 62eb45bd50..4870ea2804 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -38,6 +38,7 @@ var version string // Reports channel var reports = make(chan interface{}) var telemetryStopProcessing = make(chan bool) +var stopheartbeat = make(chan bool) // Command line arguments for CNS. var args = acn.ArgumentList{ @@ -308,8 +309,8 @@ func main() { } if !disableTelemetry { - go logger.SendCnsTelemetry(reports, cnsconfig.TelemetrySettings.HeartBeatIntervalInMins, - telemetryStopProcessing) + go logger.SendToTelemetryService(reports, telemetryStopProcessing) + go logger.SendHeartBeat(cnsconfig.TelemetrySettings.HeartBeatIntervalInMins, stopheartbeat) } var netPlugin network.NetPlugin @@ -384,6 +385,7 @@ func main() { if !disableTelemetry { telemetryStopProcessing <- true + stopheartbeat <- true } // Cleanup.