Round robin weights round-robin algorithm PHP implementation code, Roundrobin
Code first, with PHP scripting 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 < $i + +) { C1/>round_robin ($hosts, $result);} /* Output */print_r ($result);/* Round robin round */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] = a
[1] = b
[2] = C
[3] = a
[4] = a
[5] = b
[6] = a
[7] = C
[8] = b
[9] = a
)
Load Balancing server, its implementation algorithm is a round-robin weight round, is the back end of the server list, to each server weighted, representing the probability of its adoption.
This code to the most concise process stripping out, regardless of the back end hangs and so on, you can know how it is implemented, for reference only.
http://www.bkjia.com/PHPjc/1133053.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133053.html techarticle round robin weight round algorithm PHP implementation code, Roundrobin first on the code, using the PHP scripting language php/* * Copyright (C) Fathong *//* data initialization, Weight: Weight */$hosts [' a '] = A ... ..