码迷,mamicode.com
首页 > 编程语言 > 详细

平滑加权轮询算法

时间:2019-01-25 12:54:46      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:size   turn   conf   test   string   class   ret   index   col   

        static void Test1()
        {
            var sv = new ServerConfig[] {
                new ServerConfig{Name="A", Weight = 4},
                new ServerConfig{Name="B", Weight = 2},
                new ServerConfig{Name="C", Weight = 1}
            };
            int index = 0;
            int sum = sv.Sum(m => m.Weight);
            for (int i = 0; i < 100; i++)
            {
                index = NextServerIndex(sv);
                System.Console.WriteLine("{0}{1}", sv[index].Name, sv[index].Weight);

            }
        }

        public struct ServerConfig
        {
            //初始权重
            public int Weight { get; set; }

            //当前权重
            public int Current { get; set; }

            //服务名称
            public string Name { get; set; }
        }

        public static int NextServerIndex(ServerConfig[] serverConfigArray)
        {
            int index = -1;
            int total = 0;
            int size = serverConfigArray.Count();
            for (int i = 0; i < size; i++)
            {
                serverConfigArray[i].Current += serverConfigArray[i].Weight;
                total += serverConfigArray[i].Weight;
                if (index == -1 || serverConfigArray[index].Current < serverConfigArray[i].Current)
                {
                    index = i;
                }
            }
            serverConfigArray[index].Current -= total;
            return index;
        }

 

平滑加权轮询算法

标签:size   turn   conf   test   string   class   ret   index   col   

原文地址:https://www.cnblogs.com/liuxiaoji/p/10318853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!