|
1 |
| -<?php |
2 |
| - |
3 |
| -namespace Prometheus\Storage; |
4 |
| -use Prometheus\Exception\StorageException; |
5 |
| -use RedisSentinel; |
6 |
| - |
7 |
| -class RedisSentinelConnector |
8 |
| -{ |
9 |
| - |
10 |
| - /** |
11 |
| - * {@inheritdoc} |
12 |
| - * |
13 |
| - * @throws RedisException |
14 |
| - * @throws StorageException |
15 |
| - */ |
16 |
| - public function getMaster(array $config): array |
17 |
| - { |
18 |
| - $service = $config['service']; |
19 |
| - |
20 |
| - $sentinel = $this->connectToSentinel($config); |
21 |
| - |
22 |
| - $master = $sentinel->master($service); |
23 |
| - |
24 |
| - if (! $this->isValidMaster($master)) { |
25 |
| - throw new StorageException(sprintf("No master found for service '%s'.", $service)); |
26 |
| - } |
27 |
| - |
28 |
| - return $master; |
29 |
| - } |
30 |
| - |
31 |
| - /** |
32 |
| - * Check whether master is valid or not. |
33 |
| - */ |
34 |
| - protected function isValidMaster(mixed $master): bool |
35 |
| - { |
36 |
| - return is_array($master) && isset($master['ip']) && isset($master['port']); |
37 |
| - } |
38 |
| - |
39 |
| - /** |
40 |
| - * Connect to the configured Redis Sentinel instance. |
41 |
| - * |
42 |
| - * @throws StorageException |
43 |
| - */ |
44 |
| - private function connectToSentinel(array $config): RedisSentinel |
45 |
| - { |
46 |
| - $host = $config['host'] ?? ''; |
47 |
| - $port = $config['port'] ?? 26379; |
48 |
| - $timeout = $config['timeout'] ?? 0.2; |
49 |
| - $persistent = $config['persistent'] ?? null; |
50 |
| - $retryInterval = $config['retry_interval'] ?? 0; |
51 |
| - $readTimeout = $config['read_timeout'] ?? 0; |
52 |
| - $username = $config['username'] ?? ''; |
53 |
| - $password = $config['password'] ?? ''; |
54 |
| - $ssl = $config['ssl'] ?? null; |
55 |
| - |
56 |
| - if (strlen(trim($host)) === 0) { |
57 |
| - throw new StorageException('No host has been specified for the Redis Sentinel connection.'); |
58 |
| - } |
59 |
| - |
60 |
| - $auth = null; |
61 |
| - if (strlen(trim($username)) !== 0 && strlen(trim($password)) !== 0) { |
62 |
| - $auth = [$username, $password]; |
63 |
| - } elseif (strlen(trim($password)) !== 0) { |
64 |
| - $auth = $password; |
65 |
| - } |
66 |
| - |
67 |
| - if (version_compare(phpversion('redis'), '6.0', '>=')) { |
68 |
| - $options = [ |
69 |
| - 'host' => $host, |
70 |
| - 'port' => $port, |
71 |
| - 'connectTimeout' => $timeout, |
72 |
| - 'persistent' => $persistent, |
73 |
| - 'retryInterval' => $retryInterval, |
74 |
| - 'readTimeout' => $readTimeout, |
75 |
| - ]; |
76 |
| - |
77 |
| - if ($auth !== null) { |
78 |
| - $options['auth'] = $auth; |
79 |
| - } |
80 |
| - |
81 |
| - if (version_compare(phpversion('redis'), '6.1', '>=') && $ssl !== null) { |
82 |
| - $options['ssl'] = $ssl; |
83 |
| - } |
84 |
| - |
85 |
| - return new RedisSentinel($options); |
86 |
| - } |
87 |
| - |
88 |
| - if ($auth !== null) { |
89 |
| - /** @noinspection PhpMethodParametersCountMismatchInspection */ |
90 |
| - return new RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout, $auth); |
91 |
| - } |
92 |
| - |
93 |
| - return new RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout); |
94 |
| - } |
95 |
| -} |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Prometheus\Storage; |
| 4 | + |
| 5 | +use Prometheus\Exception\StorageException; |
| 6 | +use RedisSentinel; |
| 7 | + |
| 8 | +class RedisSentinelConnector |
| 9 | +{ |
| 10 | + /** |
| 11 | + * {@inheritdoc} |
| 12 | + * @param mixed[] $config |
| 13 | + * @return mixed[]|bool |
| 14 | + * @throws StorageException|\RedisException |
| 15 | + */ |
| 16 | + public function getMaster(array $config): array|bool |
| 17 | + { |
| 18 | + $service = $config['service']; |
| 19 | + |
| 20 | + $sentinel = $this->connectToSentinel($config); |
| 21 | + |
| 22 | + $master = $sentinel->master($service); |
| 23 | + |
| 24 | + if (! $this->isValidMaster($master)) { |
| 25 | + throw new StorageException(sprintf("No master found for service '%s'.", $service)); |
| 26 | + } |
| 27 | + |
| 28 | + return $master; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Check whether master is valid or not. |
| 33 | + * @param mixed[]|bool $master |
| 34 | + * @return bool |
| 35 | + */ |
| 36 | + protected function isValidMaster(array|bool $master): bool |
| 37 | + { |
| 38 | + return is_array($master) && isset($master['ip']) && isset($master['port']); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Connect to the configured Redis Sentinel instance. |
| 43 | + * @param mixed[] $config |
| 44 | + * @return RedisSentinel |
| 45 | + * @throws StorageException |
| 46 | + */ |
| 47 | + private function connectToSentinel(array $config): RedisSentinel |
| 48 | + { |
| 49 | + $host = $config['host'] ?? ''; |
| 50 | + $port = $config['port'] ?? 26379; |
| 51 | + $timeout = $config['timeout'] ?? 0.2; |
| 52 | + $persistent = $config['persistent'] ?? null; |
| 53 | + $retryInterval = $config['retry_interval'] ?? 0; |
| 54 | + $readTimeout = $config['read_timeout'] ?? 0; |
| 55 | + $username = $config['username'] ?? ''; |
| 56 | + $password = $config['password'] ?? ''; |
| 57 | + $ssl = $config['ssl'] ?? null; |
| 58 | + |
| 59 | + if (strlen(trim($host)) === 0) { |
| 60 | + throw new StorageException('No host has been specified for the Redis Sentinel connection.'); |
| 61 | + } |
| 62 | + |
| 63 | + $auth = null; |
| 64 | + if (strlen(trim($username)) !== 0 && strlen(trim($password)) !== 0) { |
| 65 | + $auth = [$username, $password]; |
| 66 | + } elseif (strlen(trim($password)) !== 0) { |
| 67 | + $auth = $password; |
| 68 | + } |
| 69 | + |
| 70 | + if (version_compare((string)phpversion('redis'), '6.0', '>=')) { |
| 71 | + $options = [ |
| 72 | + 'host' => $host, |
| 73 | + 'port' => $port, |
| 74 | + 'connectTimeout' => $timeout, |
| 75 | + 'persistent' => $persistent, |
| 76 | + 'retryInterval' => $retryInterval, |
| 77 | + 'readTimeout' => $readTimeout, |
| 78 | + ]; |
| 79 | + |
| 80 | + if ($auth !== null) { |
| 81 | + $options['auth'] = $auth; |
| 82 | + } |
| 83 | + |
| 84 | + if (version_compare((string)phpversion('redis'), '6.1', '>=') && $ssl !== null) { |
| 85 | + $options['ssl'] = $ssl; |
| 86 | + } |
| 87 | + |
| 88 | + return new RedisSentinel($options); |
| 89 | + } |
| 90 | + |
| 91 | + if ($auth !== null) { |
| 92 | + /** @noinspection PhpMethodParametersCountMismatchInspection */ |
| 93 | + return new RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout, $auth); |
| 94 | + } |
| 95 | + |
| 96 | + return new RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout, null); |
| 97 | + } |
| 98 | +} |
0 commit comments