C # version of smooth weighted round-robin balancing

Source: Internet
Author: User

Nginx based on the implementation of the polling algorithm, it not only realizes the weight-based polling algorithm, but also realizes the smoothing algorithm.
The so-called smooth, that is, in a period of time, not only the server is selected by the number of distributions and their weights consistent, and the scheduling algorithm is also more evenly select the server, and will not concentrate for a period of time only select a high-weighted server.
If the use of random algorithm selection or ordinary weight-based polling algorithm, it is relatively easy to cause a service set is called too much pressure.

For example, for example, a group of servers with weights, {a:5, b:1, c:1) Nginx's smooth polling algorithm chooses a sequence of { a, a, b, a, c, a, a }
This is clearly { c, b, a, a, a, a, a } smoother and more reasonable than the sequence, and does not result in a centralized access to the server.

The C # version code is as follows:

/// <summary>2 ///Weight Object3 /// </summary>4 Public classWeightobject5 {6 /// <summary>7 ///Current Weight8 /// </summary>9 Public intCurrentweight {Set;Get; }Ten One /// <summary> A ///effective - /// </summary> - Public intEffectiveweight {Set;Get; } the - - /// <summary> - ///Weights + /// </summary> - Public intWeight {Set;Get; } + A at } - - Public classBalancingassignhelper - { - - /// <summary> in ///algorithm: - ///On each peer selection we increase current_weight of each eligible peer by its weight, to ///Select Peer with greatest current_weight and reduce their current_weight by total number of weight points distribute Da Mong Peers. + Public StaticT next<t> (list<t> servers)whereT:weightobject - { the if(Servers = =NULL|| Servers. Count = =0) * { $ return NULL;Panax Notoginseng } - if(Servers. Count = =1) the { + returnservers[0]; A } the +T best =NULL; - $ //calculate the sum of weights $ intTotal =0; - for(inti =0; I < servers. Count; i++) - { the -T W =Servers[i];Wuyi the if(W = =NULL) - { Wu Continue; - } About $W.currentweight + =W.effectiveweight; -Total + =W.effectiveweight; - - if(W.effectiveweight <w.weight) A { +w.effectiveweight++; the } - $ if(Best = =NULL|| W.currentweight >Best . Currentweight) the { theBest =W; the } the - } in the if(Best = =NULL) the { About return NULL; the } the theBest. Currentweight-=Total ; + returnBest ; - } the }Bayi the //1. Declare an entity class that inherits Randomobject, such as: * the Public classServerweightobject:weightobject - { - /// <summary> the ///name the /// </summary> the Public stringName {Set;Get; } the /// <summary> - ///Description the /// </summary> the Public stringDescription {Set;Get; } the //... Other related fields/properties94 the PublicServerweightobject () the { theEffectiveweight =Weight;98 } About } - 101 102 /// <summary>103 ///DEMO104 /// </summary> the Public classBalancingassigndemo106 {107 108 Public voidMain ()109 { the 111 //1. Initialize the call data, such as: thelist<serverweightobject> list =NewList<serverweightobject>();113List. ADD (Newserverweightobject {Name ="A", Weight =2 }); theList. ADD (Newserverweightobject {Name ="B", Weight =3 }); theList. ADD (Newserverweightobject {Name ="C", Weight =5}); the //list. ADD (new Serverweightobject {Name = "D", Weight = 3});117 //list. ADD (new Serverweightobject {Name = "E", Weight = 4});118 //list. ADD (new Serverweightobject {Name = "F", Weight = 5});119 //list. ADD (new Serverweightobject {Name = "G", Weight = $); - for(inti =0; I < -; i++)121 {122System.Diagnostics.Debug.Write (balancingassignhelper.next<serverweightobject>(list). Name);123 }124 } the}

C # version of smooth weighted round-robin balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.