From fd2c7d264af4406202a383c96998ea799ba43306 Mon Sep 17 00:00:00 2001 From: Marcel Fest Date: Tue, 23 Apr 2024 16:39:05 +0200 Subject: [PATCH] Reduce logical complexity --- pkg/monitoring/collector.go | 12 ++---------- pkg/monitoring/frr.go | 33 +++------------------------------ pkg/monitoring/nl.go | 32 ++------------------------------ 3 files changed, 7 insertions(+), 70 deletions(-) diff --git a/pkg/monitoring/collector.go b/pkg/monitoring/collector.go index eb18302e..d7e17bcb 100644 --- a/pkg/monitoring/collector.go +++ b/pkg/monitoring/collector.go @@ -46,18 +46,10 @@ type typedFactoryDesc struct { } type basicCollector struct { - name string - mu sync.Mutex - wg sync.WaitGroup - channels []chan<- prometheus.Metric - logger logr.Logger + name string + logger logr.Logger } -// only use with Lock called before. -func (c *basicCollector) clearChannels() { - defer c.mu.Unlock() - c.channels = []chan<- prometheus.Metric{} -} func (d *typedFactoryDesc) mustNewConstMetric(value float64, labels ...string) prometheus.Metric { return prometheus.MustNewConstMetric(d.desc, d.valueType, value, labels...) } diff --git a/pkg/monitoring/frr.go b/pkg/monitoring/frr.go index abd2991d..9c5df27c 100644 --- a/pkg/monitoring/frr.go +++ b/pkg/monitoring/frr.go @@ -3,7 +3,6 @@ package monitoring import ( "strconv" "strings" - "sync" "github.com/prometheus/client_golang/prometheus" "github.com/telekom/das-schiff-network-operator/pkg/frr" @@ -239,36 +238,10 @@ func (c *frrCollector) updateBGPNeighbors(ch chan<- prometheus.Metric, bgpNeighb } } } -func (c *frrCollector) updateChannels(vrfs []frr.VrfVniSpec, routes []route.Information, neighbors frr.BGPVrfSummary) { - for _, ch := range c.channels { - c.updateVrfs(ch, vrfs) - c.updateRoutes(ch, routes) - c.updateBGPNeighbors(ch, neighbors) - } -} func (c *frrCollector) Update(ch chan<- prometheus.Metric) error { - c.mu.Lock() - c.channels = append(c.channels, ch) - if len(c.channels) == 1 { - c.wg = sync.WaitGroup{} - c.wg.Add(1) - // Ensure all other function calls will wait. - c.mu.Unlock() - routes, neighbors, vrfs := func() ([]route.Information, frr.BGPVrfSummary, []frr.VrfVniSpec) { - return c.getRoutes(), c.getBGPNeighbors(), c.getVrfs() - }() - go func(routes []route.Information, neighbors frr.BGPVrfSummary, vrfs []frr.VrfVniSpec) { - c.mu.Lock() - // unlock is done after return using defer. - defer c.wg.Done() - c.updateChannels(vrfs, routes, neighbors) - }(routes, neighbors, vrfs) - // unlock is done in this function. - defer c.clearChannels() - } else { - c.mu.Unlock() - } - c.wg.Wait() + c.updateVrfs(ch, c.getVrfs()) + c.updateRoutes(ch, c.getRoutes()) + c.updateBGPNeighbors(ch, c.getBGPNeighbors()) return nil } diff --git a/pkg/monitoring/nl.go b/pkg/monitoring/nl.go index 078b59f9..aca73c77 100644 --- a/pkg/monitoring/nl.go +++ b/pkg/monitoring/nl.go @@ -2,7 +2,6 @@ package monitoring import ( "fmt" - "sync" "github.com/prometheus/client_golang/prometheus" "github.com/telekom/das-schiff-network-operator/pkg/nl" @@ -80,35 +79,8 @@ func (c *netlinkCollector) updateNeighbors(ch chan<- prometheus.Metric, neighbor } } -func (c *netlinkCollector) updateChannels(neighbors []nl.NeighborInformation, routes []route.Information) { - for _, ch := range c.channels { - c.updateNeighbors(ch, neighbors) - c.updateRoutes(ch, routes) - } -} - func (c *netlinkCollector) Update(ch chan<- prometheus.Metric) error { - c.mu.Lock() - c.channels = append(c.channels, ch) - if len(c.channels) == 1 { - c.wg = sync.WaitGroup{} - c.wg.Add(1) - // Ensure all other function calls will wait. - c.mu.Unlock() - routes, neighbors := func() ([]route.Information, []nl.NeighborInformation) { - return c.getRoutes(), c.getNeighbors() - }() - go func(routes []route.Information, neighbors []nl.NeighborInformation) { - c.mu.Lock() - // unlock is done after return using defer. - defer c.wg.Done() - c.updateChannels(neighbors, routes) - }(routes, neighbors) - // unlock is done in this function. - defer c.clearChannels() - } else { - c.mu.Unlock() - } - c.wg.Wait() + c.updateNeighbors(ch, c.getNeighbors()) + c.updateRoutes(ch, c.getRoutes()) return nil }