如何超时ibase_connect?
How to timeout ibase_connect?
我正在尝试在连接到 firebird 2.5 db 时实现超时。
这适用于连接到 150 多台服务器的脚本。我的目标是那个服务器失败并移动到下一个服务器,以保持脚本执行时间。
正常的脚本执行时间是 30 秒,但如果一台服务器出现故障,它会增加到 300 秒。
我在 PHP 7.
上使用 ibase 扩展
有什么建议吗?
提前致谢。
服务器端肯定存在连接超时选项,在客户端你可以尝试在firebird.conf中设置它
在连接之前测试端口是否打开怎么样?
<?php
function con_test($i, $p) {
$f = @fsockopen($i, $p, $errno, $errstr, 0.1);
if (!$f) {
return false;
}
else {
fclose($f);
return true;
}
}
$host[] = ['ip'=>'192.168.52.97','port' => '3050', 'alias' => 'test'];
$host[] = ['ip'=>'192.168.52.96','port' => '3050', 'alias' => 'test'];
$username='sysdba';
$password = 'masterkey';
foreach ($host as $k=>$v)
{
if (con_test($v['ip'],$v['port'])) {
$host = $v['ip'].'/'.$v['port'].':'.$v['alias'];
$dbh = ibase_connect($host, $username, $password);
$stmt = 'SELECT \'test\' as test FROM rdb$database';
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
echo $row->TEST, PHP_EOL;
}
ibase_free_result($sth);
ibase_close($dbh);
}
else {
echo 'Cannot connect to '.$v['ip'].':'.$v['port'].PHP_EOL;
}
}
我正在尝试在连接到 firebird 2.5 db 时实现超时。
这适用于连接到 150 多台服务器的脚本。我的目标是那个服务器失败并移动到下一个服务器,以保持脚本执行时间。
正常的脚本执行时间是 30 秒,但如果一台服务器出现故障,它会增加到 300 秒。 我在 PHP 7.
上使用 ibase 扩展有什么建议吗?
提前致谢。
服务器端肯定存在连接超时选项,在客户端你可以尝试在firebird.conf中设置它 在连接之前测试端口是否打开怎么样?
<?php
function con_test($i, $p) {
$f = @fsockopen($i, $p, $errno, $errstr, 0.1);
if (!$f) {
return false;
}
else {
fclose($f);
return true;
}
}
$host[] = ['ip'=>'192.168.52.97','port' => '3050', 'alias' => 'test'];
$host[] = ['ip'=>'192.168.52.96','port' => '3050', 'alias' => 'test'];
$username='sysdba';
$password = 'masterkey';
foreach ($host as $k=>$v)
{
if (con_test($v['ip'],$v['port'])) {
$host = $v['ip'].'/'.$v['port'].':'.$v['alias'];
$dbh = ibase_connect($host, $username, $password);
$stmt = 'SELECT \'test\' as test FROM rdb$database';
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
echo $row->TEST, PHP_EOL;
}
ibase_free_result($sth);
ibase_close($dbh);
}
else {
echo 'Cannot connect to '.$v['ip'].':'.$v['port'].PHP_EOL;
}
}