Copilot commented on code in PR #3435:
URL: https://github.com/apache/dubbo-go/pull/3435#discussion_r3426063157


##########
cluster/loadbalance/roundrobin/loadbalance.go:
##########
@@ -144,8 +144,8 @@ func (robin *weightedRoundRobin) Weight() int64 {
 }
 
 func (robin *weightedRoundRobin) setWeight(weight int64) {
-       robin.weight = weight
-       robin.current = 0
+       atomic.StoreInt64(&robin.weight, weight)
+       atomic.StoreInt64(&robin.current, 0)

Review Comment:
   `setWeight` updates `weight` and resets `current` via two independent atomic 
stores. If `increaseCurrent()` runs between these two stores, it can (a) 
increment using the old/new weight unexpectedly and/or (b) have its increment 
wiped out by the subsequent `current` reset. If the intent is that weight 
updates are applied atomically with the current reset, consider guarding 
`setWeight` and selection/increment operations with a lock (or otherwise 
ensuring these mutations happen under a single critical section), or 
redesigning state so the update can be applied as one atomic unit.



##########
cluster/loadbalance/roundrobin/loadbalance.go:
##########
@@ -157,7 +157,7 @@ func (robin *weightedRoundRobin) setLastUpdate(time 
*time.Time) {
 }
 
 func (robin *weightedRoundRobin) increaseCurrent() int64 {
-       return atomic.AddInt64(&robin.current, robin.weight)
+       return atomic.AddInt64(&robin.current, atomic.LoadInt64(&robin.weight))

Review Comment:
   `setWeight` updates `weight` and resets `current` via two independent atomic 
stores. If `increaseCurrent()` runs between these two stores, it can (a) 
increment using the old/new weight unexpectedly and/or (b) have its increment 
wiped out by the subsequent `current` reset. If the intent is that weight 
updates are applied atomically with the current reset, consider guarding 
`setWeight` and selection/increment operations with a lock (or otherwise 
ensuring these mutations happen under a single critical section), or 
redesigning state so the update can be applied as one atomic unit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to