From 015a28338407940217ec89f22b980d9b48580045 Mon Sep 17 00:00:00 2001 From: Marcel Fest Date: Tue, 23 Apr 2024 17:35:46 +0200 Subject: [PATCH] Remove go routines and hidden unlock --- pkg/monitoring/collector.go | 1 - pkg/monitoring/frr.go | 24 +++++++++++------------- pkg/monitoring/nl.go | 23 ++++++++++------------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/pkg/monitoring/collector.go b/pkg/monitoring/collector.go index eb18302e..f98e3970 100644 --- a/pkg/monitoring/collector.go +++ b/pkg/monitoring/collector.go @@ -55,7 +55,6 @@ type basicCollector struct { // 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 { diff --git a/pkg/monitoring/frr.go b/pkg/monitoring/frr.go index abd2991d..be46462a 100644 --- a/pkg/monitoring/frr.go +++ b/pkg/monitoring/frr.go @@ -253,22 +253,20 @@ func (c *frrCollector) Update(ch chan<- prometheus.Metric) error { 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() + + routes := c.getRoutes() + vrfs := c.getVrfs() + neighbors := c.getBGPNeighbors() + + c.mu.Lock() + c.updateChannels(vrfs, routes, neighbors) + c.clearChannels() + c.wg.Done() + c.mu.Unlock() } else { c.mu.Unlock() + c.wg.Wait() } - c.wg.Wait() return nil } diff --git a/pkg/monitoring/nl.go b/pkg/monitoring/nl.go index 078b59f9..cb39c602 100644 --- a/pkg/monitoring/nl.go +++ b/pkg/monitoring/nl.go @@ -93,22 +93,19 @@ func (c *netlinkCollector) Update(ch chan<- prometheus.Metric) error { 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() + + routes := c.getRoutes() + neighbors := c.getNeighbors() + + c.mu.Lock() + c.updateChannels(neighbors, routes) + c.clearChannels() + c.wg.Done() + c.mu.Unlock() } else { c.mu.Unlock() + c.wg.Wait() } - c.wg.Wait() return nil }