|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Prometheus\Storage; |
| 4 | + |
| 5 | +use Prometheus\Exception\StorageException; |
| 6 | + |
| 7 | +class RedisSentinel |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @var \RedisSentinel |
| 11 | + */ |
| 12 | + private $sentinel; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var mixed[] |
| 16 | + */ |
| 17 | + private $options = []; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var mixed[] |
| 21 | + */ |
| 22 | + private static $defaultOptions = [ |
| 23 | + 'enable' => false, // if enabled uses sentinel to get the master before connecting to redis |
| 24 | + 'host' => '127.0.0.1', // phpredis sentinel address of the redis |
| 25 | + 'port' => 26379, // phpredis sentinel port of the primary redis server, default 26379 if empty. |
| 26 | + 'service' => 'myprimary', //, phpredis sentinel primary name, default myprimary |
| 27 | + 'timeout' => 0, // phpredis sentinel connection timeout |
| 28 | + 'persistent' => null, // phpredis sentinel persistence parameter |
| 29 | + 'retry_interval' => 0, // phpredis sentinel retry interval |
| 30 | + 'read_timeout' => 0, // phpredis sentinel read timeout |
| 31 | + 'reconnect' => 0, // retries after losing connection to redis asking for a new primary, if -1 will retry indefinetely |
| 32 | + 'username' => '', // phpredis sentinel auth username |
| 33 | + 'password' => '', // phpredis sentinel auth password |
| 34 | + 'ssl' => null, |
| 35 | + ]; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param \RedisSentinel $redisSentinel |
| 39 | + * @return self |
| 40 | + * @throws StorageException |
| 41 | + */ |
| 42 | + public static function fromExistingConnection(\RedisSentinel $redisSentinel) : self { |
| 43 | + $sentinel = new self(); |
| 44 | + $sentinel->sentinel = $redisSentinel; |
| 45 | + $sentinel->getMaster(); |
| 46 | + return $sentinel; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Redis constructor. |
| 51 | + * @param mixed[] $options |
| 52 | + */ |
| 53 | + public function __construct(array $options = []) |
| 54 | + { |
| 55 | + $this->options = [...self::$defaultOptions, ...$options]; |
| 56 | + $this->sentinel = $this->connectToSentinel($this->options); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + * @param mixed[] $config |
| 62 | + * @return mixed[]|bool |
| 63 | + * @throws StorageException|\RedisException |
| 64 | + */ |
| 65 | + public function getMaster(): array|bool |
| 66 | + { |
| 67 | + $service = $this->options['service']; |
| 68 | + |
| 69 | + try { |
| 70 | + if(!$this->sentinel) { |
| 71 | + $this->sentinel = $this->connectToSentinel($this->options); |
| 72 | + } |
| 73 | + |
| 74 | + $master = $this->sentinel->master($service); |
| 75 | + } catch (\RedisException $e){ |
| 76 | + throw new StorageException( |
| 77 | + sprintf("Can't connect to RedisSentinel server. %s", $e->getMessage()), |
| 78 | + $e->getCode(), |
| 79 | + $e |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + if (! $this->isValidMaster($master)) { |
| 84 | + throw new StorageException(sprintf("No master found for service '%s'.", $service)); |
| 85 | + } |
| 86 | + |
| 87 | + return $master; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Check whether master is valid or not. |
| 92 | + * @param mixed[]|bool $master |
| 93 | + * @return bool |
| 94 | + */ |
| 95 | + protected function isValidMaster(array|bool $master): bool |
| 96 | + { |
| 97 | + return is_array($master) && isset($master['ip']) && isset($master['port']); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Connect to the configured Redis Sentinel instance. |
| 102 | + * @return \RedisSentinel |
| 103 | + * @throws StorageException |
| 104 | + */ |
| 105 | + private function connectToSentinel(): \RedisSentinel |
| 106 | + { |
| 107 | + $host = $this->options['host'] ?? ''; |
| 108 | + $port = $this->options['port'] ?? 26379; |
| 109 | + $timeout = $this->options['timeout'] ?? 0.2; |
| 110 | + $persistent = $this->options['persistent'] ?? null; |
| 111 | + $retryInterval = $this->options['retry_interval'] ?? 0; |
| 112 | + $readTimeout = $this->options['read_timeout'] ?? 0; |
| 113 | + $username = $this->options['username'] ?? ''; |
| 114 | + $password = $this->options['password'] ?? ''; |
| 115 | + $ssl = $this->options['ssl'] ?? null; |
| 116 | + |
| 117 | + if (strlen(trim($host)) === 0) { |
| 118 | + throw new StorageException('No host has been specified for the Redis Sentinel connection.'); |
| 119 | + } |
| 120 | + |
| 121 | + $auth = null; |
| 122 | + if (strlen(trim($username)) !== 0 && strlen(trim($password)) !== 0) { |
| 123 | + $auth = [$username, $password]; |
| 124 | + } elseif (strlen(trim($password)) !== 0) { |
| 125 | + $auth = $password; |
| 126 | + } |
| 127 | + |
| 128 | + if (version_compare((string)phpversion('redis'), '6.0', '>=')) { |
| 129 | + $options = [ |
| 130 | + 'host' => $host, |
| 131 | + 'port' => $port, |
| 132 | + 'connectTimeout' => $timeout, |
| 133 | + 'persistent' => $persistent, |
| 134 | + 'retryInterval' => $retryInterval, |
| 135 | + 'readTimeout' => $readTimeout, |
| 136 | + ]; |
| 137 | + |
| 138 | + if ($auth !== null) { |
| 139 | + $options['auth'] = $auth; |
| 140 | + } |
| 141 | + |
| 142 | + if (version_compare((string)phpversion('redis'), '6.1', '>=') && $ssl !== null) { |
| 143 | + $options['ssl'] = $ssl; |
| 144 | + } |
| 145 | + // @phpstan-ignore arguments.count, argument.type |
| 146 | + return new \RedisSentinel($options); |
| 147 | + } |
| 148 | + |
| 149 | + if ($auth !== null) { |
| 150 | + /** |
| 151 | + * @phpstan-ignore arguments.count |
| 152 | + **/ |
| 153 | + return new \RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout, $auth); |
| 154 | + } |
| 155 | + return new \RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout); |
| 156 | + } |
| 157 | + |
| 158 | + public function getSentinel() : \RedisSentinel { |
| 159 | + return $this->sentinel; |
| 160 | + } |
| 161 | +} |
0 commit comments