/**
  * {@inheritdoc}
  */
 public function add(NodeConnectionInterface $connection)
 {
     $parameters = $connection->getParameters();
     if (isset($parameters->alias)) {
         $this->pool[$parameters->alias] = $connection;
     } else {
         $this->pool[] = $connection;
     }
     $weight = isset($parameters->weight) ? $parameters->weight : null;
     $this->distributor->add($connection, $weight);
 }
 /**
  * {@inheritdoc}
  */
 public function remove(NodeConnectionInterface $connection)
 {
     if ($connection->getParameters()->alias === 'master') {
         $this->master = null;
         $this->reset();
         return true;
     } else {
         if (($id = array_search($connection, $this->slaves, true)) !== false) {
             unset($this->slaves[$id]);
             $this->reset();
             return true;
         }
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 protected function executeSingleNode(NodeConnectionInterface $connection, \SplQueue $commands)
 {
     $responses = array();
     $sizeOfPipe = count($commands);
     foreach ($commands as $command) {
         try {
             $connection->writeRequest($command);
         } catch (CommunicationException $exception) {
             return array_fill(0, $sizeOfPipe, $exception);
         }
     }
     for ($i = 0; $i < $sizeOfPipe; ++$i) {
         $command = $commands->dequeue();
         try {
             $responses[$i] = $connection->readResponse($command);
         } catch (CommunicationException $exception) {
             $add = count($commands) - count($responses);
             $responses = array_merge($responses, array_fill(0, $add, $exception));
             break;
         }
     }
     return $responses;
 }
Example #4
0
 /**
  * Prepares a connection instance after its initialization.
  *
  * @param NodeConnectionInterface $connection Connection instance.
  */
 protected function prepareConnection(NodeConnectionInterface $connection)
 {
     $parameters = $connection->getParameters();
     if (isset($parameters->password)) {
         $connection->addConnectCommand(new RawCommand(array('AUTH', $parameters->password)));
     }
     if (isset($parameters->database)) {
         $connection->addConnectCommand(new RawCommand(array('SELECT', $parameters->database)));
     }
 }