You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/07/16 05:44:54 UTC

[dubbo-go] branch 3.0 updated: Fix: fix rand.Seed() duplicate concurrent calls (#1958)

This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 27d68abe9 Fix: fix rand.Seed() duplicate concurrent calls (#1958)
27d68abe9 is described below

commit 27d68abe9cad62b6b1952103f84e061e15c66c0e
Author: Yepeng Zhang <42...@users.noreply.github.com>
AuthorDate: Sat Jul 16 13:44:50 2022 +0800

    Fix: fix rand.Seed() duplicate concurrent calls (#1958)
    
    * fix: fix rand.Seed() duplicate concurrent calls
    
    * fix: fix rand.Seed() in p2c/loadbalance_test.go
---
 cluster/loadbalance/p2c/loadbalance.go                           | 2 +-
 cluster/loadbalance/p2c/loadbalance_test.go                      | 9 ++++++++-
 .../instance/random/random_service_instance_selector.go          | 2 +-
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/cluster/loadbalance/p2c/loadbalance.go b/cluster/loadbalance/p2c/loadbalance.go
index 7a044cc55..12f2a37c5 100644
--- a/cluster/loadbalance/p2c/loadbalance.go
+++ b/cluster/loadbalance/p2c/loadbalance.go
@@ -44,6 +44,7 @@ var (
 )
 
 func init() {
+	rand.Seed(randSeed())
 	extension.SetLoadbalance(constant.LoadBalanceKeyP2C, newP2CLoadBalance)
 }
 
@@ -78,7 +79,6 @@ func (l *p2cLoadBalance) Select(invokers []protocol.Invoker, invocation protocol
 	if len(invokers) == 2 {
 		i, j = 0, 1
 	} else {
-		rand.Seed(randSeed())
 		i = rand.Intn(len(invokers))
 		j = i
 		for i == j {
diff --git a/cluster/loadbalance/p2c/loadbalance_test.go b/cluster/loadbalance/p2c/loadbalance_test.go
index 17092fba7..ff27ed0a6 100644
--- a/cluster/loadbalance/p2c/loadbalance_test.go
+++ b/cluster/loadbalance/p2c/loadbalance_test.go
@@ -18,6 +18,7 @@
 package p2c
 
 import (
+	"math/rand"
 	"testing"
 )
 
@@ -37,16 +38,18 @@ import (
 func TestLoadBalance(t *testing.T) {
 	lb := newP2CLoadBalance()
 	invocation := protoinvoc.NewRPCInvocation("TestMethod", []interface{}{}, nil)
-	randSeed = func() int64 {
+	randSeed := func() int64 {
 		return 0
 	}
 
 	t.Run("no invokers", func(t *testing.T) {
+		rand.Seed(randSeed())
 		ivk := lb.Select([]protocol.Invoker{}, invocation)
 		assert.Nil(t, ivk)
 	})
 
 	t.Run("one invoker", func(t *testing.T) {
+		rand.Seed(randSeed())
 		url0, _ := common.NewURL("dubbo://192.168.1.0:20000/com.ikurento.user.UserProvider")
 
 		ivkArr := []protocol.Invoker{
@@ -57,6 +60,7 @@ func TestLoadBalance(t *testing.T) {
 	})
 
 	t.Run("two invokers", func(t *testing.T) {
+		rand.Seed(randSeed())
 		ctrl := gomock.NewController(t)
 		defer ctrl.Finish()
 
@@ -86,6 +90,7 @@ func TestLoadBalance(t *testing.T) {
 	})
 
 	t.Run("multiple invokers", func(t *testing.T) {
+		rand.Seed(randSeed())
 		ctrl := gomock.NewController(t)
 		defer ctrl.Finish()
 
@@ -117,6 +122,7 @@ func TestLoadBalance(t *testing.T) {
 	})
 
 	t.Run("metrics i not found", func(t *testing.T) {
+		rand.Seed(randSeed())
 		ctrl := gomock.NewController(t)
 		defer ctrl.Finish()
 
@@ -144,6 +150,7 @@ func TestLoadBalance(t *testing.T) {
 	})
 
 	t.Run("metrics j not found", func(t *testing.T) {
+		rand.Seed(randSeed())
 		ctrl := gomock.NewController(t)
 		defer ctrl.Finish()
 
diff --git a/registry/servicediscovery/instance/random/random_service_instance_selector.go b/registry/servicediscovery/instance/random/random_service_instance_selector.go
index 599fb4d80..82dbe85ce 100644
--- a/registry/servicediscovery/instance/random/random_service_instance_selector.go
+++ b/registry/servicediscovery/instance/random/random_service_instance_selector.go
@@ -30,6 +30,7 @@ import (
 )
 
 func init() {
+	rand.Seed(time.Now().UnixNano())
 	extension.SetServiceInstanceSelector("random", NewRandomServiceInstanceSelector)
 }
 
@@ -47,7 +48,6 @@ func (r *RandomServiceInstanceSelector) Select(url *common.URL, serviceInstances
 	if len(serviceInstances) == 1 {
 		return serviceInstances[0]
 	}
-	rand.Seed(time.Now().UnixNano())
 	index := rand.Intn(len(serviceInstances))
 	return serviceInstances[index]
 }