zoukankan      html  css  js  c++  java
  • 按权重值轮训算法

    轮训算法:

        /// <summary>
        /// 轮训算法
        /// </summary>
        public class WeightBalance
        {
            private static readonly object LockObject = new object();
            private WeightBalance() { }
            static WeightBalance()
            {
                Instance = new WeightBalance();
            }
            public static WeightBalance Instance
            {
                get;
                private set;
            }
    
            public T Next<T>(List<T> list) where T : IWeightItem
            {
                if (list == null || !list.Any())
                {
                    throw new ArgumentNullException(nameof(list));
                }
                if (list.Count == 1)
                {
                    return list.First();
                }
                lock (LockObject)
                {
                    int total = list.Sum(i => i.Weight);
                    int maxIndex = 0;
                    int maxValue = 0;
                    int weight = 0;
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i].Attach != 0)
                        {
                            weight = list[i].Attach;
                        }
                        else
                        {
                            weight = list[i].Weight;
                            list[i].Attach = weight;
                        }
                        if (i == 0)
                        {
                            maxValue = weight;
                        }
                        else if (weight > maxValue)
                        {
                                maxIndex = i;
                                maxValue = weight;
                        }
                    }
    
                    list[maxIndex].Attach = list[maxIndex].Attach - total;
                    return list[maxIndex];
                }
            }
        }
    
        /// <summary>
        /// 被轮询的实体要实现此接口
        /// </summary>
        public interface IWeightItem
        {
            /// <summary>
            /// 权重值
            /// </summary>
            int Weight { get; set; }
            /// <summary>
            /// 附加值
            /// </summary>
            int Attach { get; set; }
        }

    客户端:

        class Program
        {
            static void Main(string[] args)
            {
                var connectionList = new List<SlaveConnection>{
                new SlaveConnection{ ConnectionString="server=.;Database=2;",Weight=2},
                new SlaveConnection{ ConnectionString="server=.;Database=5;",Weight=5},
                new SlaveConnection{ ConnectionString="server=.;Database=1;",Weight=1},
                new SlaveConnection{ ConnectionString="server=.;Database=3;",Weight=3},
                new SlaveConnection{ ConnectionString="server=.;Database=4;",Weight=4},
                };
                for (int i = 0; i < 30; i++)
                {
                    Console.WriteLine(WeightBalance.Instance.Next(connectionList).ConnectionString);
                }
                Console.ReadKey();
    
            }
        }
        public class SlaveConnection : IWeightItem
        {
            public string ConnectionString { get; set; }
    
            public int Weight { get; set; }
    
            public int Attach { get; set; }
    
        }

    输出结果:

  • 相关阅读:
    mysql基础知识
    项目开发步骤
    python 中的电子邮箱的操作
    django 运行python manage.py sqlall books 时报错 app has migration
    mysql报错锦集
    搭建spark中需要注意的问题
    pycharm5.0 激活方式
    python去除停用词(结巴分词下)
    Ubuntu下安装libsvm
    PCIE_DMA实例五:基于XILINX XDMA的PCIE高速采集卡
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/12862705.html
Copyright © 2011-2022 走看看