Skip to content

Commit

Permalink
Remove go routines and hidden unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Cellebyte committed Apr 23, 2024
1 parent 6bc1aec commit 015a283
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
1 change: 0 additions & 1 deletion pkg/monitoring/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 11 additions & 13 deletions pkg/monitoring/frr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 10 additions & 13 deletions pkg/monitoring/nl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 015a283

Please sign in to comment.