You are here

protected function PhpRedis::askForMaster in Redis 8

Connect to sentinels to get Redis master instance.

Just asking one sentinels after another until given the master location. More info about this mode at https://redis.io/topics/sentinel.

Parameters

\Redis $client: The PhpRedis client.

array $sentinels: An array of the sentinels' ip:port.

string $password: An optional Sentinels' password.

Return value

mixed An array with ip & port of the Master instance or NULL.

1 call to PhpRedis::askForMaster()
PhpRedis::getClient in src/Client/PhpRedis.php
Get the connected client instance.

File

src/Client/PhpRedis.php, line 74

Class

PhpRedis
PhpRedis client specific implementation.

Namespace

Drupal\redis\Client

Code

protected function askForMaster(\Redis $client, array $sentinels = [], $password = NULL) {
  $ip_port = NULL;
  $settings = Settings::get('redis.connection', []);
  $settings += [
    'instance' => NULL,
  ];
  if ($settings['instance']) {
    foreach ($sentinels as $sentinel) {
      list($host, $port) = explode(':', $sentinel);

      // Prevent fatal PHP errors when one of the sentinels is down.
      set_error_handler(function () {
        return TRUE;
      });

      // 0.5s timeout.
      $success = $client
        ->connect($host, $port, 0.5);
      restore_error_handler();
      if (!$success) {
        continue;
      }
      if (isset($password)) {
        $client
          ->auth($password);
      }
      if ($client
        ->isConnected()) {
        $ip_port = $client
          ->rawcommand('SENTINEL', 'get-master-addr-by-name', $settings['instance']);
        if ($ip_port) {
          break;
        }
      }
      $client
        ->close();
    }
  }
  return $ip_port;
}