Skip to content

Commit 6bb1ae2

Browse files
aledbfroboquat
authored andcommitted
Refactor probe maps to use sync.Map
1 parent 6c4c785 commit 6bb1ae2

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

components/ws-manager/pkg/manager/monitor.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ type Monitor struct {
7373
eventpool *workpool.EventWorkerPool
7474
ticker *time.Ticker
7575

76-
probeMap map[string]context.CancelFunc
77-
probeMapLock sync.Mutex
76+
probeMap sync.Map
7877

7978
initializerMap sync.Map
8079
finalizerMap sync.Map
@@ -95,9 +94,8 @@ func (m *Manager) CreateMonitor() (*Monitor, error) {
9594

9695
log.WithField("interval", monitorInterval).Info("starting workspace monitor")
9796
res := Monitor{
98-
manager: m,
99-
ticker: time.NewTicker(monitorInterval),
100-
probeMap: make(map[string]context.CancelFunc),
97+
manager: m,
98+
ticker: time.NewTicker(monitorInterval),
10199

102100
OnError: func(err error) {
103101
log.WithError(err).Error("workspace monitor error")
@@ -188,12 +186,11 @@ func (m *Monitor) onPodEvent(evt watch.Event) error {
188186
if evt.Type == watch.Deleted {
189187
// If we're still probing this workspace (because it was stopped by someone other than the monitor while we
190188
// were probing), stop doing that.
191-
m.probeMapLock.Lock()
192-
if cancelProbe, ok := m.probeMap[pod.Name]; ok {
189+
if cancelProbeFunc, ok := m.probeMap.Load(pod.Name); ok {
190+
cancelProbe := cancelProbeFunc.(context.CancelFunc)
193191
cancelProbe()
194-
delete(m.probeMap, pod.Name)
192+
m.probeMap.Delete(pod.Name)
195193
}
196-
m.probeMapLock.Unlock()
197194

198195
// We're handling a pod event, thus Kubernetes gives us the pod we're handling. However, this is also a deleted
199196
// event which means the pod doesn't actually exist anymore. We need to reflect that in our status compution, hence
@@ -727,16 +724,13 @@ func (m *Monitor) probeWorkspaceReady(ctx context.Context, pod *corev1.Pod) (res
727724

728725
// Probe preparation, i.e. checking if a probe exists already and if it doesn't registering a new one has to be atomic with
729726
// regards to the probeMapLock. Ensure both operations are within the same locked section.
730-
m.probeMapLock.Lock()
731-
_, alreadyProbing := m.probeMap[pod.Name]
727+
_, alreadyProbing := m.probeMap.Load(pod.Name)
732728
if alreadyProbing {
733-
m.probeMapLock.Unlock()
734729
return nil, nil
735730
}
736731

737732
ctx, cancelProbe := context.WithTimeout(ctx, 30*time.Minute)
738-
m.probeMap[pod.Name] = cancelProbe
739-
m.probeMapLock.Unlock()
733+
m.probeMap.Store(pod.Name, cancelProbe)
740734

741735
// The probe run will block until either the probe finds the pod ready or the probe itself is stopped.
742736
// Because of that it's best to run probeWorkspaceReady as a go routine.
@@ -755,9 +749,7 @@ func (m *Monitor) probeWorkspaceReady(ctx context.Context, pod *corev1.Pod) (res
755749
span.LogFields(tracelog.String("result", string(probeResult)))
756750

757751
// we're done probing: deregister probe from probe map
758-
m.probeMapLock.Lock()
759-
delete(m.probeMap, pod.Name)
760-
m.probeMapLock.Unlock()
752+
m.probeMap.Delete(pod.Name)
761753

762754
cancelProbe()
763755

0 commit comments

Comments
 (0)