yumosx commented on code in PR #3046:
URL: https://github.com/apache/dubbo-go/pull/3046#discussion_r2510353282


##########
cluster/loadbalance/p2c/loadbalance.go:
##########
@@ -75,16 +117,7 @@ func (l *p2cLoadBalance) Select(invokers []base.Invoker, 
invocation base.Invocat
        // The local metrics is available only for the earlier version.
        m := metrics.LocalMetrics
        // picks two nodes randomly
-       var i, j int
-       if len(invokers) == 2 {
-               i, j = 0, 1
-       } else {
-               i = rand.Intn(len(invokers))
-               j = i
-               for i == j {
-                       j = rand.Intn(len(invokers))
-               }
-       }
+       i, j := l.randomPicker(len(invokers))
        logger.Debugf("[P2C select] Two invokers were selected, invoker[%d]: 
%s, invoker[%d]: %s.",

Review Comment:
   嗯这也是一种思路,我没有试过,但是应该每次去预测一下,我的想法是单独抽出来这个`type randomPicker func(n int) (i, j 
int)` 
   
   这样在测试的时候就直接控制它的结果, 一劳永逸简单一点,
   
   之所以写这门复杂还有一点就是,math/rand 好像会爆安全问题, 所以只能写成 `secureRandomInt` 这种



##########
cluster/loadbalance/p2c/loadbalance.go:
##########
@@ -75,16 +117,7 @@ func (l *p2cLoadBalance) Select(invokers []base.Invoker, 
invocation base.Invocat
        // The local metrics is available only for the earlier version.
        m := metrics.LocalMetrics
        // picks two nodes randomly
-       var i, j int
-       if len(invokers) == 2 {
-               i, j = 0, 1
-       } else {
-               i = rand.Intn(len(invokers))
-               j = i
-               for i == j {
-                       j = rand.Intn(len(invokers))
-               }
-       }
+       i, j := l.randomPicker(len(invokers))
        logger.Debugf("[P2C select] Two invokers were selected, invoker[%d]: 
%s, invoker[%d]: %s.",

Review Comment:
   嗯这也是一种思路,我没有试过,但是应该每次要去预测一下,我的想法是单独抽出来这个`type randomPicker func(n int) (i, j 
int)` 
   
   这样在测试的时候就直接控制它的结果, 一劳永逸简单一点,
   
   之所以写这门复杂还有一点就是,math/rand 好像会爆安全问题, 所以只能写成 `secureRandomInt` 这种



-- 
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