Skip to content

Commit

Permalink
Make StartProbe and StopProbe thread safe
Browse files Browse the repository at this point in the history
This currently isn't an issue as we run sequentially, but could become
an issue in the future.
  • Loading branch information
glrf committed Jul 27, 2023
1 parent 516045b commit b2bfe9c
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/sliexporter/probes/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/go-logr/logr"
Expand All @@ -12,9 +13,11 @@ import (

// Manager manages a collection of Probers the check connectivity to AppCat services.
type Manager struct {
hist prometheus.ObserverVec
log logr.Logger

mu *sync.Mutex
probers map[key]context.CancelFunc
hist prometheus.ObserverVec
log logr.Logger

newTicker func() (<-chan time.Time, func())
}
Expand Down Expand Up @@ -51,9 +54,10 @@ func NewManager(l logr.Logger) Manager {
}, []string{"service", "namespace", "name", "reason", "organization"})

return Manager{
probers: map[key]context.CancelFunc{},
hist: hist,
log: l,
mu: &sync.Mutex{},
probers: map[key]context.CancelFunc{},
newTicker: newTickerChan,
}
}
Expand All @@ -66,6 +70,9 @@ func (m Manager) Collector() prometheus.Collector {
// StartProbe will send a probe once every second using the provided prober.
// If a prober with the same ProbeInfo already runs, it will stop the running prober.
func (m Manager) StartProbe(p Prober) {
m.mu.Lock()
defer m.mu.Unlock()

l := m.log.WithValues("namespace", p.GetInfo().Namespace, "name", p.GetInfo().Name)
probeKey := getKey(p.GetInfo())
cancel, ok := m.probers[probeKey]
Expand All @@ -83,6 +90,9 @@ func (m Manager) StartProbe(p Prober) {
// StopProbe will stop the prober with the provided ProbeInfo.
// Is a Noop if none is running.
func (m Manager) StopProbe(pi ProbeInfo) {
m.mu.Lock()
defer m.mu.Unlock()

probeKey := getKey(pi)
cancel, ok := m.probers[probeKey]
if ok {
Expand Down

0 comments on commit b2bfe9c

Please sign in to comment.