CheckServer.class.php
2.3 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
<?php
class Util_Server_Adapter_CheckServer {
/**
* 存储Tracker延迟时间
*/
//const TIMEOUT = 15;
const TIMEOUT = 2;
/**
* 服务器不好的情况下检查时间(5分钟)
*
* @var Integer
*/
private static $expire = 300;
/**
* 服务器好的情况下检查时间(20分钟)
*
* @var Integer
*/
private static $goodExpire = 1200;
/**
* 检验Server
*
* @param String $host
* @param Integer $port
* @param String $type
* @return bool
*/
public static function check( $host, $port = '3306', $type = 'db') {
if('memcache' == $type){
return true;
}
if( empty ($host) || empty ($port) || empty ($type) ) {
throw new Util_Server_Exception ('下列参数不合法: host:'.$host.' port:'.$port.' type:'.$type );
}
$statusKeyName = 'check://'.$type.'_'.$host.'_'.$port.'_status';
$lastModified = time();
$key = $host.':'.$port;
$apcObj = array();
if ( Util_Cache_Adapter_Apc::isEnabled () ) {
$apcObj = Util_Cache_Adapter_Apc::get ( $statusKeyName );
#服务器状态为true
if ( !empty ( $apcObj [ $key ] ) && $apcObj [ $key ]['status'] == true && ($lastModified - $apcObj [ $key ]['lastModified']) <= self::$goodExpire ) {
return $apcObj [ $key ]['status'];
}
#服务器状态为false
if ( !empty ( $apcObj [ $key ] ) && $apcObj [ $key ]['status'] == false && ($lastModified - $apcObj [ $key ]['lastModified']) <= self::$expire ) {
return $apcObj [ $key ]['status'];
}
if ( empty( $apcObj )){
$apcObj = array();
}
}
$errno = 0;
$errstr = '';
$status = true;
$fs = @fsockopen ( $host, $port, $errno, $errstr, self::TIMEOUT );
if ( !$fs ) {
@fclose ( $fs );
$status = false;
}
if ( Util_Cache_Adapter_Apc::isEnabled () ) {
$apcObj = array();//清除值
$apcObj[ $host.':'.$port ] = array (
'status' => $status,
'lastModified' => $lastModified
);
Util_Cache_Adapter_Apc::set ( $statusKeyName, new ArrayObject ( $apcObj ) );
}
return $status;
}
}