The php implementation code of the roundrobin weight round robin algorithm, roundrobin. The php implementation code of the roundrobin weight round robin algorithm. roundrobin first carries the code and uses the php script language php ** Copyright (C) FatHong ** for data initialization. weight: weight * $ hosts [a] a round robin weight round robin algorithm php implementation code, roundrobin
First, use the php script language.
<? Php/** Copyright (C) FatHong * // * data initialization, weight: weight */$ hosts ['A'] = array ('weight' => 5, 'current _ weight' => 0, 'count' => 0); $ hosts ['B'] = array ('weight' => 3, 'current _ weight' => 0, 'count' => 0); $ hosts ['c'] = array ('weight' => 2, 'current _ weight '=> 0, 'count' => 0); $ result = array ();/* simulate 10 times */for ($ I = 0; $ I <10; $ I ++) {round_robin ($ hosts, $ result);}/* output result */print_r ($ result ); /* round robin */function round_robin (& $ hosts, & $ result) {$ total = 0; $ best = null; foreach ($ hosts as $ key => $ item) {$ current = & $ hosts [$ key]; $ weight = $ current ['weight']; $ current ['current _ weight '] + = $ weight; $ total + = $ weight; if ($ best = null) | ($ hosts [$ best] ['current _ weight '] <$ current ['current _ weight']) {$ best = $ key ;}} $ hosts [$ best] ['current _ weight ']-= $ total; $ hosts [$ best] ['count'] ++; $ result [] = $ best ;}
Output result:
Array
(
[0] =>
[1] => B
[2] => c
[3] =>
[4] =>
[5] => B
[6] =>
[7] => c
[8] => B
[9] =>
)
In the server load balancer server, the implementation algorithm is round-robin Weighted round robin, that is, in the back-end server list, each server is assigned a weight, representing its probability of being used.
This code separates the most concise process and does not consider backend suspension. you can see how it is implemented for your reference only.
Http://www.bkjia.com/PHPjc/1133053.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133053.htmlTechArticleround round robin weights algorithm php implementation code, roundrobin first on the code, using php script language php/** Copyright (C) FatHong * // * data initialization, weight: weight */$ hosts ['A'] =...