@@ -73,8 +73,7 @@ type Monitor struct {
73
73
eventpool * workpool.EventWorkerPool
74
74
ticker * time.Ticker
75
75
76
- probeMap map [string ]context.CancelFunc
77
- probeMapLock sync.Mutex
76
+ probeMap sync.Map
78
77
79
78
initializerMap sync.Map
80
79
finalizerMap sync.Map
@@ -95,9 +94,8 @@ func (m *Manager) CreateMonitor() (*Monitor, error) {
95
94
96
95
log .WithField ("interval" , monitorInterval ).Info ("starting workspace monitor" )
97
96
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 ),
101
99
102
100
OnError : func (err error ) {
103
101
log .WithError (err ).Error ("workspace monitor error" )
@@ -188,12 +186,11 @@ func (m *Monitor) onPodEvent(evt watch.Event) error {
188
186
if evt .Type == watch .Deleted {
189
187
// If we're still probing this workspace (because it was stopped by someone other than the monitor while we
190
188
// 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 )
193
191
cancelProbe ()
194
- delete ( m .probeMap , pod .Name )
192
+ m .probeMap . Delete ( pod .Name )
195
193
}
196
- m .probeMapLock .Unlock ()
197
194
198
195
// We're handling a pod event, thus Kubernetes gives us the pod we're handling. However, this is also a deleted
199
196
// 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
727
724
728
725
// Probe preparation, i.e. checking if a probe exists already and if it doesn't registering a new one has to be atomic with
729
726
// 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 )
732
728
if alreadyProbing {
733
- m .probeMapLock .Unlock ()
734
729
return nil , nil
735
730
}
736
731
737
732
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 )
740
734
741
735
// The probe run will block until either the probe finds the pod ready or the probe itself is stopped.
742
736
// 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
755
749
span .LogFields (tracelog .String ("result" , string (probeResult )))
756
750
757
751
// 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 )
761
753
762
754
cancelProbe ()
763
755
0 commit comments