Skip to content

Commit 4f42c31

Browse files
committed
code cleaning
1 parent 1f4c1fe commit 4f42c31

File tree

3 files changed

+113
-106
lines changed

3 files changed

+113
-106
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"github-actions.workflows.pinned.workflows": []
3+
}

src/Prometheus/Storage/Redis.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,18 @@ public function __construct(array $options = [])
9494

9595
/**
9696
* Sentinels descoverMaster
97-
* @param array $options
97+
* @param mixed[] $options
98+
* @return mixed[]
9899
*/
99-
public function getSentinelPrimary(array $options = [])
100+
public function getSentinelPrimary(array $options = []) : array
100101
{
101102
$sentinel = new RedisSentinelConnector();
102103
$options['sentinel']['host'] = $options['sentinel']['host'] ?? $options['host'];
103104
$master = $sentinel->getMaster($options['sentinel']);
104-
$options['host'] = $master['ip'];
105-
$options['port'] = $master['port'];
105+
if (is_array($master)) {
106+
$options['host'] = $master['ip'];
107+
$options['port'] = $master['port'];
108+
}
106109
return $options;
107110
}
108111

@@ -235,7 +238,7 @@ function (array $metric): MetricFamilySamples {
235238
* Inspects the given exception and reconnects the client if the reported error indicates that the server
236239
* went away or is in readonly mode, which may happen in case of a Redis Sentinel failover.
237240
*/
238-
private function reconnectIfRedisIsUnavailableOrReadonly(RedisException $exception): bool
241+
private function reconnectIfRedisIsUnavailableOrReadonly(\RedisException $exception): bool
239242
{
240243
// We convert the exception message to lower-case in order to perform case-insensitive comparison.
241244
$exceptionMessage = strtolower($exception->getMessage());
@@ -261,10 +264,10 @@ private function ensureOpenConnection(): void
261264
return;
262265
}
263266

264-
if($this->options['sentinel'] && $this->options['sentinel']['enable']){
267+
if (isset($this->options['sentinel']) && boolval($this->options['sentinel']['enable'])) {
265268
$reconnect = $this->options['sentinel']['reconnect'];
266269
$retries = 0;
267-
while ($retries<=$reconnect) {
270+
while ($retries <= $reconnect) {
268271
try {
269272
$this->options = $this->getSentinelPrimary($this->options);
270273
$this->connectToServer();
@@ -279,13 +282,11 @@ private function ensureOpenConnection(): void
279282
);
280283
}
281284
}
282-
$retries++;
285+
$retries++;
283286
}
284287
} else {
285288
$this->connectToServer();
286289
}
287-
288-
289290

290291
$authParams = [];
291292

@@ -328,7 +329,7 @@ private function connectToServer(): void
328329
if (!$connection_successful) {
329330
throw new StorageException(
330331
sprintf("Can't connect to Redis server. %s", $this->redis->getLastError()),
331-
null
332+
0
332333
);
333334
}
334335
}
Lines changed: 98 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,98 @@
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

Comments
 (0)