企業のDCの1つに障害が発生した場合に、PHPプログラムをあきらめる前にXXX秒待機させたくない場合、
およびldap_connect()
ユーザーが指定した時間にタイムアウトするメカニズムがないため、
これは私の回避策であり、優れた実用的な結果を示しています。
function serviceping($host, $port=389, $timeout=1)
{
$op = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$op) return 0; //DC is N/A
else {
fclose($op); //explicitly close open socket connection
return 1; //DC is up & running, we can safely connect with ldap_connect
}
}
// ##### STATIC DC LIST, if your DNS round robin is not setup
//$dclist = array('10.111.222.111', '10.111.222.100', '10.111.222.200');
// ##### DYNAMIC DC LIST, reverse DNS lookup sorted by round-robin result
$dclist = gethostbynamel('domain.name');
foreach ($dclist as $k => $dc) if (serviceping($dc) == true) break; else $dc = 0;
//after this loop, either there will be at least one DC which is available at present, or $dc would return bool false while the next line stops program from further execution
if (!$dc) exit("NO DOMAIN CONTROLLERS AVAILABLE AT PRESENT, PLEASE TRY AGAIN LATER!"); //user being notified
//now, ldap_connect would certainly connect succesfully to DC tested previously and no timeout will occur
$ldapconn = ldap_connect($dc) or die("DC N/A, PLEASE TRY AGAIN LATER.");
また、このアプローチを使用すると、非常に優れたフェイルオーバー機能を利用できます。
例として、数十のDC-aが離れた場所に分散している会社を考えてみましょう。
このようにして、現在少なくとも1つのDCがアクティブになっている場合、PHPプログラムの可用性は常に高くなります。