How to implement weighted round robin using c#?

百般思念 提交于 2019-11-30 20:15:36

问题


If I have some servers: 192.168.100.1, 192.168.100.2, 192.168.100.3, 192.168.100.4... and weights of them are: 5, 1, 2, 3

I want to implement load balancing, but how can I implement weighted round robin using C#?


回答1:


Say you have servers a, b, c, d. And you have corresponding weights 5, 1, 2, 3. You can do weighted round robin in the following way:

Random rand = new Random(seed);

void processRequest(Request r){

    // assume rand.next() returns a uniformly distributed integer >= 0
    int i = rand.next() % 11; // 11 is sum of weights

    if(i <= 4)      // process r with server a
    else if(i == 5) // process r with server b
    else if(i <= 7) // process r with server c
    else            // process r with server d
}

rand.next() % 11 returns a uniformly distributed integer in the range [0, 10] (inclusive). We process the request with server a for five of the possible values [0, 4]. We process the request with server b for only one possible value 5 and so on.

Pay special attention to the particular random method you use and the seed value.



来源:https://stackoverflow.com/questions/8494876/how-to-implement-weighted-round-robin-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!