Example #1
0
 /**
  * Returns the next free slave. This method is async, so be aware of async calls between this call.
  *
  * @return integer
  */
 protected function getNextSlave($cb)
 {
     $that = $this;
     $checkSlave = function () use($cb, $that, &$checkSlave) {
         $minConnections = null;
         $minPort = null;
         foreach ($this->slaves as $slave) {
             if (!$slave['ready']) {
                 continue;
             }
             if (!$this->concurrentRequestsPerWorker && $slave['busy']) {
                 //we skip workers that are busy, means worker that are currently handle a connection
                 //this makes it more robust since most applications are not made to handle
                 //several request at the same time - even when one request is streaming. Would lead
                 //to strange effects&crashes in high traffic sites if not considered.
                 //maybe in the future this can be set application specific.
                 //Rule of thumb: The application may not operate on globals, statics or same file paths to get this working.
                 continue;
             }
             // we pick a slave that currently handles the fewest connections
             if (null === $minConnections || $slave['connections'] < $minConnections) {
                 $minConnections = $slave['connections'];
                 $minPort = $slave['port'];
             }
         }
         if (null !== $minPort) {
             $cb($minPort);
             return;
         }
         $this->loop->futureTick($checkSlave);
     };
     $checkSlave();
 }