public BrokerGroup Select(NewQmqClusterInfo cluster)
        {
            if (cluster.BrokerGroups.Count == 0)
            {
                return(null);
            }
            var groups = DictValueFilter.Filter(cluster.BrokerGroups, BrokerGroup.IsReadable);

            if (groups == null || groups.Count == 0)
            {
                return(null);
            }

            var totalWeight = 0;
            var sameWeight  = true;
            var lastWeight  = -1;

            foreach (var group in groups)
            {
                if (!weights.TryGetValue(group.Name, out int weight))
                {
                    weights.TryAdd(group.Name, DEFAULT_WEIGHT);
                    weight = DEFAULT_WEIGHT;
                }

                if (lastWeight != -1 && lastWeight != weight)
                {
                    sameWeight = false;
                }
                lastWeight   = weight;
                totalWeight += weight;
            }

            if (totalWeight == 0)
            {
                return(null);
            }

            if (totalWeight > 0 && !sameWeight)
            {
                int offset = StaticRandom.NextRand(totalWeight);
                foreach (var group in groups)
                {
                    weights.TryGetValue(group.Name, out int weight);
                    offset -= weight;
                    if (offset <= 0)
                    {
                        return(group);
                    }
                }
            }

            var index = StaticRandom.NextRand(groups.Count);

            return(groups[index]);
        }
Exemple #2
0
        public BrokerGroup Select(NewQmqClusterInfo cluster)
        {
            if (cluster.BrokerGroups.Count == 0)
            {
                return(null);
            }
            var groups = DictValueFilter.Filter(cluster.BrokerGroups, BrokerGroup.IsReadable);

            if (groups == null || groups.Count == 0)
            {
                return(null);
            }

            var count = groups.Count;

            return(groups[Math.Abs(Interlocked.Increment(ref index) % count)]);
        }