Round robin weights round-robin algorithm PHP implementation code, roundrobin_php tutorial

Source: Internet
Author: User

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 ... ..

  • 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.