Skip to content

Commit

Permalink
Revert "Reduce logical complexity"
Browse files Browse the repository at this point in the history
This reverts commit fd2c7d2.
  • Loading branch information
Cellebyte committed Apr 23, 2024
1 parent fd2c7d2 commit 6bc1aec
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
12 changes: 10 additions & 2 deletions pkg/monitoring/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ type typedFactoryDesc struct {
}

type basicCollector struct {
name string
logger logr.Logger
name string
mu sync.Mutex
wg sync.WaitGroup
channels []chan<- prometheus.Metric
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: 30 additions & 3 deletions pkg/monitoring/frr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 @@ -238,10 +239,36 @@ 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.updateVrfs(ch, c.getVrfs())
c.updateRoutes(ch, c.getRoutes())
c.updateBGPNeighbors(ch, c.getBGPNeighbors())
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()
return nil
}
32 changes: 30 additions & 2 deletions pkg/monitoring/nl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package monitoring

import (
"fmt"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/telekom/das-schiff-network-operator/pkg/nl"
Expand Down Expand Up @@ -79,8 +80,35 @@ 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.updateNeighbors(ch, c.getNeighbors())
c.updateRoutes(ch, c.getRoutes())
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()
return nil
}

0 comments on commit 6bc1aec

Please sign in to comment.