平滑加权轮询算法
Posted liuxiaoji
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了平滑加权轮询算法相关的知识,希望对你有一定的参考价值。
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; }
以上是关于平滑加权轮询算法的主要内容,如果未能解决你的问题,请参考以下文章