You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by ke...@apache.org on 2022/10/14 04:34:18 UTC

[shenyu] branch master updated: replace with computeIfAbsent. (#4076)

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

kevinclair pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new e65a3b204 replace with computeIfAbsent. (#4076)
e65a3b204 is described below

commit e65a3b2041eeeb3d33c9e1b658fbaf428ccf2d6d
Author: Kevin Clair <ke...@apache.org>
AuthorDate: Fri Oct 14 12:34:07 2022 +0800

    replace with computeIfAbsent. (#4076)
    
    Co-authored-by: xiaoyu <xi...@apache.org>
---
 .../shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java       | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java
index 43403d44a..d62160ab0 100644
--- a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java
+++ b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java
@@ -50,13 +50,12 @@ public class RoundRobinLoadBalancer extends AbstractLoadBalancer {
         WeightedRoundRobin selectedWeightedRoundRobin = null;
         for (Upstream upstream : upstreamList) {
             String rKey = upstream.getUrl();
-            WeightedRoundRobin weightedRoundRobin = map.get(rKey);
             int weight = getWeight(upstream);
-            if (Objects.isNull(weightedRoundRobin)) {
-                weightedRoundRobin = new WeightedRoundRobin();
-                weightedRoundRobin.setWeight(weight);
-                map.putIfAbsent(rKey, weightedRoundRobin);
-            }
+            WeightedRoundRobin weightedRoundRobin = map.computeIfAbsent(rKey, k -> {
+                WeightedRoundRobin roundRobin = new WeightedRoundRobin();
+                roundRobin.setWeight(weight);
+                return roundRobin;
+            });
             if (weight != weightedRoundRobin.getWeight()) {
                 // weight changed.
                 weightedRoundRobin.setWeight(weight);