@@ -33,21 +33,24 @@ func (ins *Instance) StartHealthMonitor() {
3333}
3434
3535func (ins * Instance ) checkAgentHealth (i int , agent string ) {
36- status , exists := ins .targetStatus [ agent ]
36+ val , exists := ins .targetStatus . Load ( agent )
3737 if ! exists {
38- ins .targetStatus [agent ] = & TargetStatus {healthy : true , lastSeen : time .Now ()}
39- status = ins .targetStatus [agent ]
38+ newStatus := & TargetStatus {
39+ healthy : true ,
40+ lastSeen : time .Now (),
41+ }
42+ val , _ = ins .targetStatus .LoadOrStore (agent , newStatus )
4043 }
44+ status := val .(* TargetStatus )
4145
4246 // Don't check too frequently if already marked as unhealthy
43- if ! status .healthy {
44- status .mu . RLock ()
45- timeSinceLastCheck := time .Since (status .lastSeen )
46- status .mu .RUnlock ()
47+ status .mu . RLock ()
48+ healthy := status .healthy
49+ timeSinceLastCheck := time .Since (status .lastSeen )
50+ status .mu .RUnlock ()
4751
48- if timeSinceLastCheck < time .Duration (ins .RecoveryInterval ) {
49- return
50- }
52+ if ! healthy && timeSinceLastCheck < time .Duration (ins .RecoveryInterval ) {
53+ return
5154 }
5255
5356 // Create a connection with shorter timeout for health checking
@@ -111,12 +114,22 @@ func (ins *Instance) checkAgentHealth(i int, agent string) {
111114}
112115
113116func (ins * Instance ) markAgentUnhealthy (agent string ) {
114- status , exists := ins .targetStatus [ agent ]
117+ val , exists := ins .targetStatus . Load ( agent )
115118 if ! exists {
116- ins .targetStatus [agent ] = & TargetStatus {healthy : false , lastSeen : time .Now (), failCount : ins .MaxFailCount }
117- return
119+ newStatus := & TargetStatus {
120+ healthy : false ,
121+ lastSeen : time .Now (),
122+ failCount : ins .MaxFailCount ,
123+ }
124+ var loaded bool
125+ val , loaded = ins .targetStatus .LoadOrStore (agent , newStatus )
126+ if ! loaded {
127+ return
128+ }
118129 }
119130
131+ // Existing status found, update it
132+ status := val .(* TargetStatus )
120133 status .mu .Lock ()
121134 defer status .mu .Unlock ()
122135
@@ -131,11 +144,13 @@ func (ins *Instance) markAgentUnhealthy(agent string) {
131144}
132145
133146func (ins * Instance ) isAgentHealthy (agent string ) bool {
134- status , exists := ins .targetStatus [ agent ]
147+ val , exists := ins .targetStatus . Load ( agent )
135148 if ! exists {
136149 return true // Default to considering it healthy if no status exists
137150 }
138151
152+ // Existing status found, update it
153+ status := val .(* TargetStatus )
139154 status .mu .RLock ()
140155 defer status .mu .RUnlock ()
141156 return status .healthy
0 commit comments