ClusterTopClient.php
5.5 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
class ClusterTopClient extends TopClient {
private static $dnsconfig;
private static $syncDate = 0;
private static $applicationVar ;
private static $cfgDuration = 10;
public function __construct($appkey = "",$secretKey = ""){
ClusterTopClient::$applicationVar = new ApplicationVar;
$this->appkey = $appkey;
$this->secretKey = $secretKey ;
$saveConfig = ClusterTopClient::$applicationVar->getValue();
if($saveConfig){
$tmpConfig = $saveConfig['dnsconfig'];
ClusterTopClient::$dnsconfig = $this->object_to_array($tmpConfig);
unset($tmpConfig);
ClusterTopClient::$syncDate = $saveConfig['syncDate'];
if(!ClusterTopClient::$syncDate)
ClusterTopClient::$syncDate = 0;
}
}
public function __destruct(){
if(ClusterTopClient::$dnsconfig && ClusterTopClient::$syncDate){
ClusterTopClient::$applicationVar->setValue("dnsconfig",ClusterTopClient::$dnsconfig);
ClusterTopClient::$applicationVar->setValue("syncDate",ClusterTopClient::$syncDate);
ClusterTopClient::$applicationVar->write();
}
}
public function execute($request = null, $session = null,$bestUrl = null){
$currentDate = date('U');
$syncDuration = $this->getDnsConfigSyncDuration();
$bestUrl = $this->getBestVipUrl($this->gatewayUrl,$request->getApiMethodName(),$session);
if($currentDate - ClusterTopClient::$syncDate > $syncDuration * 60){
$httpdns = new HttpdnsGetRequest;
ClusterTopClient::$dnsconfig = json_decode(parent::execute($httpdns,null,$bestUrl)->result,true);
$syncDate = date('U');
ClusterTopClient::$syncDate = $syncDate ;
}
return parent::execute($request,$session,$bestUrl);
}
private function getDnsConfigSyncDuration(){
if(ClusterTopClient::$cfgDuration){
return ClusterTopClient::$cfgDuration;
}
if(!ClusterTopClient::$dnsconfig){
return ClusterTopClient::$cfgDuration;
}
$config = json_encode(ClusterTopClient::$dnsconfig);
if(!$config){
return ClusterTopClient::$cfgDuration;
}
$config = ClusterTopClient::$dnsconfig['config'];
$duration = $config['interval'];
ClusterTopClient::$cfgDuration = $duration;
return ClusterTopClient::$cfgDuration;
}
private function getBestVipUrl($url,$apiname = null,$session = null){
$config = ClusterTopClient::$dnsconfig['config'];
$degrade = $config['degrade'];
if(strcmp($degrade,'true') == 0){
return $url;
}
$currentEnv = $this->getEnvByApiName($apiname,$session);
$vip = $this->getVipByEnv($url,$currentEnv);
if($vip)
return $vip;
return $url;
}
private function getVipByEnv($comUrl,$currentEnv){
$urlSchema = parse_url($comUrl);
if(!$urlSchema)
return null;
if(!ClusterTopClient::$dnsconfig['env'])
return null;
if(!array_key_exists($currentEnv,ClusterTopClient::$dnsconfig['env']))
return null;
$hostList = ClusterTopClient::$dnsconfig['env'][$currentEnv];
if(!$hostList)
return null ;
$vipList = null;
foreach ($hostList as $key => $value) {
if(strcmp($key,$urlSchema['host']) == 0 && strcmp($value['proto'],$urlSchema['scheme']) == 0){
$vipList = $value;
break;
}
}
$vip = $this->getRandomWeightElement($vipList['vip']);
if($vip){
return $urlSchema['scheme']."://".$vip.$urlSchema['path'];
}
return null;
}
private function getEnvByApiName($apiName,$session=""){
$apiCfgArray = ClusterTopClient::$dnsconfig['api'];
if($apiCfgArray){
if(array_key_exists($apiName,$apiCfgArray)){
$apiCfg = $apiCfgArray[$apiName];
if(array_key_exists('user',$apiCfg)){
$userFlag = $apiCfg['user'];
$flag = $this->getUserFlag($session);
if($userFlag && $flag ){
return $this->getEnvBySessionFlag($userFlag,$flag);
}else{
return $this->getRandomWeightElement($apiCfg['rule']);
}
}
}
}
return $this->getDeafultEnv();
}
private function getUserFlag($session){
if($session && strlen($session) > 5){
if($session[0] == '6' || $session[0] == '7'){
return $session[strlen($session) -1];
}else if($session[0] == '5' || $session[0] == '8'){
return $session[5];
}
}
return null;
}
private function getEnvBySessionFlag($targetConfig,$flag){
if($flag){
$userConf = ClusterTopClient::$dnsconfig['user'];
$cfgArry = $userConf[$targetConfig];
foreach ($cfgArry as $key => $value) {
if(in_array($flag,$value))
return $key;
}
}else{
return null;
}
}
private function getRandomWeightElement($elements){
$totalWeight = 0;
if($elements){
foreach ($elements as $ele) {
$weight = $this->getElementWeight($ele);
$r = $this->randomFloat() * ($weight + $totalWeight);
if($r >= $totalWeight){
$selected = $ele;
}
$totalWeight += $weight;
}
if($selected){
return $this->getElementValue($selected);
}
}
return null;
}
private function getElementWeight($ele){
$params = explode('|', $ele);
return floatval($params[1]);
}
private function getElementValue($ele){
$params = explode('|', $ele);
return $params[0];
}
private function getDeafultEnv(){
return ClusterTopClient::$dnsconfig['config']['def_env'];
}
private static function startsWith($haystack, $needle) {
return $needle === "" || strpos($haystack, $needle) === 0;
}
private function object_to_array($obj)
{
$_arr= is_object($obj) ? get_object_vars($obj) : $obj;
foreach($_arr as $key=> $val)
{
$val= (is_array($val) || is_object($val))? $this->object_to_array($val) : $val;
$arr[$key] = $val;
}
return$arr;
}
private function randomFloat($min = 0, $max = 1) { return $min + mt_rand() / mt_getrandmax() * ($max - $min); }
}
?>