This is an automated email from the ASF dual-hosted git repository.
Alanxtl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/develop by this push:
new 04b27a9bf fix(loadbalance): fix data race in RoundRobin setWeight and
increaseCurrent (#3435)
04b27a9bf is described below
commit 04b27a9bf6ea5f6ec58406234473863345065097
Author: aias00 <[email protected]>
AuthorDate: Thu Jun 18 12:29:26 2026 +0800
fix(loadbalance): fix data race in RoundRobin setWeight and increaseCurrent
(#3435)
The weightedRoundRobin struct had mixed atomic and non-atomic access
to the weight and current fields:
- setWeight() used plain writes while Weight() used atomic.LoadInt64
- increaseCurrent() read weight with a plain read while other methods
used atomic operations on the same field
Replace all non-atomic accesses with atomic operations:
- setWeight: use atomic.StoreInt64 for both weight and current
- increaseCurrent: use atomic.LoadInt64 to read weight
Co-authored-by: Claude <[email protected]>
---
cluster/loadbalance/roundrobin/loadbalance.go | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/cluster/loadbalance/roundrobin/loadbalance.go
b/cluster/loadbalance/roundrobin/loadbalance.go
index 3530968b4..fcd151d18 100644
--- a/cluster/loadbalance/roundrobin/loadbalance.go
+++ b/cluster/loadbalance/roundrobin/loadbalance.go
@@ -145,8 +145,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)
}
func (robin *weightedRoundRobin) LastUpdate() *time.Time {
@@ -158,7 +158,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))
}
func (robin *weightedRoundRobin) Current(delta int64) {