forked from hetao29/slightphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRest_Http.php
More file actions
105 lines (101 loc) · 2.74 KB
/
Rest_Http.php
File metadata and controls
105 lines (101 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| SlightPHP Framework |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation. You should have received a copy of the |
| GNU General Public License along with this program. If not, see |
| http://www.gnu.org/licenses/. |
| Copyright (C) 2008-2009. All Rights Reserved. |
+-----------------------------------------------------------------------+
| Supports: http://www.slightphp.com |
+-----------------------------------------------------------------------+
}}}*/
/**
* @package SlightPHP
* @subpackage SRest
*/
namespace SlightPHP;
class Rest_Http{
static private $_servers = array();
static private $_serverCt = 0;
/**
* @param array['host']
* @param array['timeout']
* @param array['weight']
*/
function init($params=array()){
if(!empty($params['host'])){
self::addServer(
$params['host'],
isset($params['path'])?$params['path']:"",
isset($params['weight'])?$params['weight']:1,
isset($params['timeout'])?$params['timeout']:1
);
}
}
/**
* @param array $servers
*/
static function addServers($servers){
self::$_servers=array();
foreach($servers as $server) self::init($server);
}
/**
* consistent hashing
*/
public static function addServer($host,$path="",$weight=1,$timeout=1){
$weight = $weight*10;
for($i=1;$i<=$weight;$i++){
$serverid = self::hash($host.":".$path.":".$i);
self::$_servers[]=array(
"host"=>$host,
"path"=>$path,
"weight"=>$weight,
"id"=>$serverid,
"timeout"=>$timeout,
);
}
usort(self::$_servers,"self::_sort");
self::$_serverCt=count(self::$_servers);
}
private static function _sort($a,$b){
return $a['id']>$b['id'];
}
public static function getServer($key){
$key = self::hash($key);
$left = 0;
$right=self::$_serverCt-1;
$index = 0;
while($left<$right-1){
$middle = (int)(($left+$right)/2);
if($key <= self::$_servers[$left]['id']){
$index = $left;
break;
}
if($key>= self::$_servers[$right]['id']){
$index = $right;
break;
}
$t = self::$_servers[$middle]['id'];
if($key==$t){
$index = $middle;
}
if($key>$t){
$left = $middle;
$index = $right;
}else {
$right=$middle;
$index = $middle;
}
}
$server = self::$_servers[$index];
$server['index'] = $index;
return $server;
}
private static function hash($str){
return crc32($str);
}
}