From 28de36fb2daeb8a3d6ba678d674ab6b04d664404 Mon Sep 17 00:00:00 2001 From: Abdul Qadeer Date: Thu, 25 Feb 2021 16:52:50 +0300 Subject: [PATCH] Populate metrics initially in cache before returning (#21) Signed-off-by: Abdul Qadeer (cherry picked from commit ef1b4d7efdb54e677e9e7a7b4c5af31bd51f1f53) --- pkg/watcher/watcher.go | 5 ++++- pkg/watcher/watcher_test.go | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index c8384ba..2244134 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -140,12 +140,15 @@ func (w *Watcher) StartWatching() { windowWatcher := func(duration string) { for { fetchOnce(duration) - time.Sleep(time.Minute) // This is assuming fetching of metrics won't exceed more than 1 minute. If it happens we need to throttle rate of fetches + // This is assuming fetching of metrics won't exceed more than 1 minute. If it happens we need to throttle rate of fetches + time.Sleep(time.Minute) } } durations := [3]string{FifteenMinutes, TenMinutes, FiveMinutes} for _, duration := range durations { + // Populate cache initially before returning + fetchOnce(duration) go windowWatcher(duration) } diff --git a/pkg/watcher/watcher_test.go b/pkg/watcher/watcher_test.go index 18eb38a..47373a5 100644 --- a/pkg/watcher/watcher_test.go +++ b/pkg/watcher/watcher_test.go @@ -22,7 +22,6 @@ import ( "net/url" "os" "testing" - "time" "github.com/francoispqt/gojay" "github.com/stretchr/testify/assert" @@ -125,7 +124,6 @@ func TestMain(m *testing.M) { client := NewTestMetricsServerClient() w = NewWatcher(client) w.StartWatching() - time.Sleep(time.Second * 5) // buffer time fetch metrics for all windows ret := m.Run() os.Exit(ret)