【问题标题】:Get servers data - gateway timeout获取服务器数据 - 网关超时
【发布时间】:2021-07-12 20:17:14
【问题描述】:

我有下面的课程来检查服务器(如果服务器在线,则获取信息,玩家号码等)循环。大约有 250 多台服务器要检查,我在执行脚本时遇到网关超时。

所有服务器都有搜索查询

SearchQuery = $mysqli -> query ( 'SELECT * FROM `list_ots` ORDER BY `list_ots`.`id`' );

while ($Row = $SearchQuery -> fetch_assoc())
    {
        $check_ban=$mysqli->query('SELECT count(*) FROM `list_bans` where `server`="'.$Row['id'].'"')->fetch_assoc();
        if($check_ban['count(*)']!=0){
            continue;
        }

        checkServer($Row);
    }

这就是类 OTSChecker 和函数 getData() 为每个服务器执行的。 问题是 - 这个脚本执行时间太长了 有没有可能让它更快(getData 函数)? 我添加了类似

ini_set('max_execution_time', 600); 

但它不起作用

<?php
class OTSChecker
{

public $ConnData;
private $OTData=array(), $XML=array(), $Uptime = array(), $TimeOut;
public function __construct($IP, $Port=7171)
{
    $this -> ConnData = array('IP' => $IP, 'Port' => $Port);
}

public function SocketTimeOut( $inTimeOut )
{
    $this -> TimeOut = intval ( $inTimeOut );   
}

public function GetData()
{
    $info = chr(6) . chr(0) . chr(255) . chr(255) . 'info';
    // fsocketopen cannot be with ssl becouse then it gives 0 
    $Sock = @fsockopen( $this->ConnData['IP'] , $this->ConnData['Port'] , $errno, $errstr, 10);

    if ( is_resource ( $Sock ) )
    {
        if ( isset ( $this -> TimeOut ) )
        {
            stream_set_timeout( $Sock , $this -> TimeOut );
            stream_set_blocking( $Sock , FALSE );
        }

        @fwrite( $Sock , $info );

        $Data = NuLL;

        while ( !feof ( $Sock ) )
        {
            $Data .= fgets( $Sock , 1024 );
        }

        @fclose( $Sock );

        

        if ( $Data != NuLL )
        {
            $this->XML = simplexml_load_string ( $Data );
        }

        $this -> OTData['status'] = 'Online';
    } else {
        $this -> OTData['status'] = 'Offline';      
    }
}

public function Status ()
{
    return $this -> OTData['status'];
}

private function GenerateUptime ( &$Data )
{
    preg_match('/uptime="(\d+)"/', $Data, $matches);
    $h = floor($matches[1] / 3600);
    $m = floor(($matches[1] - $h*3600) / 60);
    
    return array ( 'hours' => $h , 'minutes' => $m );
}

public function GetOwnerName()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> owner -> attributes() -> name;
}

public function GetOwnerEmail()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> owner -> attributes() -> email;
}

public function GetServerName()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> serverinfo -> attributes() -> servername;
}

public function GetServerLocation()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> serverinfo -> attributes() -> location;
}

public function GetServerVersion()
{
    if ( is_object ( $this -> XML) )
        //return (string) $this -> XML -> serverinfo -> attributes() -> version;
        return (string) $this -> XML -> serverinfo -> attributes() -> version;
}

public function GetNowUptime()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> serverinfo -> attributes() -> uptime;
}

public function GetCountOfPlayersOnline()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> players -> attributes() -> online;
}

public function GetMaxPlayersCount()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> players -> attributes() -> max;
}

public function GetMaxPlayersRecord()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> players -> attributes() -> peak;
}

public function GetAllMonsters()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> monsters -> attributes() -> total;
}

public function GetMotd()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> motd;
}
}
?> 

【问题讨论】:

标签: php mysql timeout


【解决方案1】:

“网关超时”表示代理服务器或负载均衡器超时(例如nginx)。

增加脚本的超时限制是不够的。您还需要相应地配置导致脚本执行的其他组件的超时时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多