Release - Load Balancer PHP Basis

11/27/2012 20:17 JPGaming#1
Musste gerade lachen, weil ich ein älteres (jedoch funktionierendes) Script für einen Load Balancer auf PHP Basis gefunden habe. Dies verwende ich nicht mehr, da es nicht ganz das gelbe vom Ei ist. Vielleicht hat der ein oder andere dafür Verwendung?! Ich denke, der Quellcode ist recht selbsterklärend, sollten jedoch Fragen entstehen tut euch keinen Zwang an.

Klasse selber
PHP Code:
<?php defined('SYSPATH') or die('No direct script access.');
class 
Loadbalancer
{
        
/*Holds counter for active load balacer*/
        
public $balancer_counter null;
        
/*Current base url*/
        
public $balance_base_url null;
        
/*Array for later getting online balancer*/
        
public $balancers_online = array();
        
/*Array for later getting offline balancer*/
        
public $balancers_offline = array();
        
/*Holds the pre before url base, s = default*/
        
public $pre_url 's';
                
        public function 
return_current($advanced_mode false)
        {
            if(!
is_null($this->balancer_counter) && !is_null($this->balance_base_url)) {
                for(
$i 1$i < ($this->balancer_counter+1); $i++) {
                    
$call_url $this->pre_url.$i.'.'.$this->balance_base_url;
                    try {
                        
fsockopen($call_url80$errno$errstr30);
                        
array_push($this->balancers_online$call_url);
                    } catch(
Exception $e) {
                        
array_push($this->balancers_offline$call_url);      
                    }  
                }
            } else {
                return 
'balance_counter || balance_base_url is empty.';                
            } 
            
            
$balancing_array = array();
            
            if(
$advanced_mode === true) {
                
$dir getcwd();
                if(
file_exists($dir.'\balancing.txt')) {             
                    
$file fopen($dir.'\balancing.txt''r');
                    while(
$singlecluster fgets($file1024)) {
                        
$singlecluster str_replace(' '''$singlecluster);
                        if(empty(
$balancing_array[$singlecluster])) {
                            
$balancing_array[$singlecluster] = 1;
                        } else {
                            
$balancing_array[$singlecluster]++;
                        }
                    }
                } else {
                    
$file fopen($dir.'\balancing.txt''a');
                    for(
$i=0;$i<$this->balancer_counter;$i++) {
                        
fwrite($file, ($i+1)."\r\n");
                    }
                    
fclose($file);                        
                }
            }
            
            try{
                if(
$advanced_mode === true) {
                    
$lowest['cluser'] = 99999999;
                    
$lowest['joins'] = 99999999;
                    foreach(
$balancing_array as $key => $value) {
                        if(
$value $lowest['joins']) {
                            
$lowest['cluster'] = $key;
                            
$lowest['joins'] = $value
                        }                         
                    }
                    
$random_cluster_point = (int) $lowest['cluster'];
                    
                    if(!
is_numeric($random_cluster_point)) {
                        
$random_cluster_point 1;
                    }
                    
                    
$dir getcwd();
                    
$file fopen($dir.'\balancing.txt''a');
                    
fwrite($file, ($random_cluster_point)."\r\n");
                    
fclose($file);
                } else {
                    
$random_cluster_point rand(0, (count($this->balancers_online))-1); 
                }
                return 
$this->balancers_online[$random_cluster_point-1];
            } catch (
Exception  $e) {
                return 
'No Clusterpoint online!';                
            }
        }
}
Call this per
PHP Code:
/*Calling new Loadbalancer (CLASS)*/
$loadbalancer_file = new Loadbalancer;

/*Set Loadbalancer Cluster Count*/
$loadbalancer_file->balancer_counter 3;

/*Balancer URL*/
$loadbalancer_file->balance_base_url 'change_this_url.com';

/*Balancer Pre URL*/
$loadbalancer_file->pre_url 's'// -> s1., s2., ..                         

/*Get Current Loadbalancer, PARAMETER: Advanced Mode (slower!)*/                         
$this->template->current_loadbalancer $loadbalancer_file->return_current(true); 
Mehrmals Aufruf ist möglich, sodass es auch verschiedene Balancer für bestimmte Sachen geben kann.