Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

random_controller function

Mike Smith
2011-05-23
2013-05-20
  • Mike Smith
    Mike Smith
    2011-05-23

    Hey all,

    So, I ran into an issue over our maintenance weekend. The windows team had to shut down one of the AD Controllers for about 2 hours. During this time, it was hit or miss if you could log in or not. Basically I have 2 controllers in the array. Since the function calls array_rand, you had a 50/50 shot of getting the controller that was up.

    I have since modified the code a bit to account for this. Not sure if this is the best way to handle, but it seems to work for me.

    --- snip ---

    /**
        * Select a random domain controller from your domain controller array
        *
        * @return string
        */
        protected function random_controller(){
            mt_srand(doubleval(microtime()) * 100000000); // For older PHP versions
            if (sizeof($this->_domain_controllers) > 1) {
                $ad_controller = $this->_domain_controllers;
                $ping = $this->test_controller($ad_controller);
                if ($ping > 0) {
                    return $this->random_controller();
                }
                else {
                    return ($ad_controller);
                }
            }
            else {
                return ($this->_domain_controllers);
            }
        }

        /**
        * Test basic connectivity to controller
        *
        * @return int
        */
        protected function test_controller($host){
            $port = '389';
            if ($this->_use_ssl) { $port = '636'; }
            fSockOpen($host, $port, $errno, $errstr, '10');
            if ($errno > 0 ) { return 1; }
            return 0;
        }

     
  • Oliver Geisen
    Oliver Geisen
    2012-02-23

    I also came around this but in PHP-Docs i found that hint in ldap_connect() function

    The host name parameter can be a space separated list of host names. This means that the LDAP code will talk to a backup server if the main server is not operational. There will be a delay while the code times out trying to talk to the main server but things will still work. This is particularly useful with a typical Microsoft Active Directory setup of primary and backup domain controllers.
    <?php
    $ldaphost = "192.168.0.100 192.168.0.101";
    $ldapconn = ldap_connect($ldaphost);
    ?>

    So ldap_connect() accepts a string with space separated DCs and will automatically do load-balance and failover. So i think there may be a simpler approach to this problem?