Skip to content

Commit

Permalink
Reduce logical complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Cellebyte committed Apr 23, 2024
1 parent 430a58e commit fd2c7d2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 70 deletions.
12 changes: 2 additions & 10 deletions pkg/monitoring/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
33 changes: 3 additions & 30 deletions pkg/monitoring/frr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
32 changes: 2 additions & 30 deletions pkg/monitoring/nl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package monitoring

import (
"fmt"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/telekom/das-schiff-network-operator/pkg/nl"
Expand Down Expand Up @@ -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
}

0 comments on commit fd2c7d2

Please sign in to comment.