Skip to content

Commit 063da5e

Browse files
committed
Add support for unix sockets
1 parent 6eb9268 commit 063da5e

5 files changed

Lines changed: 42 additions & 18 deletions

File tree

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,18 @@ services:
8080
Redis:
8181
8282
- `PCA_REDIS_0_NAME` The server name for an info panel. Optional, default: Localhost.
83-
- `PCA_REDIS_0_HOST` Redis server host.
84-
- `PCA_REDIS_0_PORT` Redis server port. Optional, default: 6379.
83+
- `PCA_REDIS_0_HOST` Redis host. Optional, when a path is specified.
84+
- `PCA_REDIS_0_PORT` Redis port. Optional, default: 6379.
8585
- `PCA_REDIS_0_DATABASE` Redis database. Optional, default: 0.
8686
- `PCA_REDIS_0_PASSWORD` Redis password. Optional, default: empty.
87+
- `PCA_REDIS_0_PATH` Redis unix path. Optional, default: /var/run/redis/redis-server.sock.
8788

8889
Memcached:
8990

9091
- `PCA_MEMCACHED_0_NAME` The server name for an info panel. Optional, default: Localhost.
91-
- `PCA_MEMCACHED_0_HOST` Memcached server host.
92-
- `PCA_MEMCACHED_0_PORT` Memcached server port. Optional, default: 11211.
92+
- `PCA_MEMCACHED_0_HOST` Memcached host. Optional, when a path is specified.
93+
- `PCA_MEMCACHED_0_PORT` Memcached port. Optional, default: 11211.
94+
- `PCA_MEMCACHED_0_PATH` Memcached unix path. Optional, default: /var/run/memcached/memcached.sock.
9395

9496
To add another server, add the same environment variables, but change 0 to 1 (2 for third server and so on).
9597

config.dist.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
'redis' => [
2424
[
2525
'name' => 'Localhost', // Optional
26-
'host' => '127.0.0.1',
26+
'host' => '127.0.0.1', // Optional, when a path is specified
2727
'port' => 6379, // Optional
2828
//'database' => 0, // Optional
2929
//'password' => '', // Optional
30+
//'path' => '/var/run/redis/redis-server.sock', // Optional
3031
],
3132
],
3233
'memcached' => [
3334
[
3435
'name' => 'Localhost', // Optional
35-
'host' => '127.0.0.1',
36+
'host' => '127.0.0.1', // Optional, when a path is specified
3637
'port' => 11211, // Optional
38+
//'path' => '/var/run/memcached/memcached.sock', // Optional
3739
],
3840
],
3941
'timeformat' => 'd. m. Y H:i:s',

src/Dashboards/Memcached/MemcacheCompatibility/RunCommandTrait.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ trait RunCommandTrait {
2525
public function runCommand(string $command): ?array {
2626
$data = [];
2727

28-
$this->server['port'] ??= 11211;
28+
if (isset($this->server['path'])) {
29+
$fp = stream_socket_client('unix://'.$this->server['path']);
30+
} else {
31+
$this->server['port'] ??= 11211;
2932

30-
$fp = @fsockopen($this->server['host'], (int) $this->server['port'], $error_code, $error_message, 3);
33+
$fp = fsockopen($this->server['host'], (int) $this->server['port'], $error_code, $error_message, 3);
34+
}
3135

3236
if ($fp === false) {
3337
return null;

src/Dashboards/Memcached/MemcachedDashboard.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,20 @@ private function connect(array $server) {
7272
throw new DashboardException('Memcache(d) extension is not installed.');
7373
}
7474

75-
$server['port'] ??= 11211;
75+
if (isset($server['path'])) {
76+
$memcached_server = $server['path'];
7677

77-
$memcache->addServer($server['host'], (int) $server['port']);
78+
$memcache->addServer($server['path'], 0);
79+
} else {
80+
$server['port'] ??= 11211;
81+
82+
$memcached_server = $server['host'].':'.$server['port'];
83+
84+
$memcache->addServer($server['host'], (int) $server['port']);
85+
}
7886

7987
if (!$memcache->isConnected()) {
80-
throw new DashboardException(
81-
sprintf('Failed to connect to Memcache(d) server (%s:%s).', $server['host'], $server['port'])
82-
);
88+
throw new DashboardException(sprintf('Failed to connect to Memcache(d) server (%s).', $memcached_server));
8389
}
8490

8591
return $memcache;

src/Dashboards/Redis/RedisDashboard.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,23 @@ private function connect(array $server): Redis {
7575
throw new DashboardException('Redis extension is not installed.');
7676
}
7777

78-
$server['port'] ??= 6379;
78+
if (isset($server['path'])) {
79+
$redis_server = $server['path'];
80+
} else {
81+
$server['port'] ??= 6379;
82+
83+
$redis_server = $server['host'].':'.$server['port'];
84+
}
7985

8086
try {
81-
$redis->connect($server['host'], (int) $server['port'], 3);
87+
if (isset($server['path'])) {
88+
$redis->connect($server['path']);
89+
} else {
90+
$redis->connect($server['host'], (int) $server['port'], 3);
91+
}
8292
} catch (Exception $e) {
8393
throw new DashboardException(
84-
sprintf('Failed to connect to Redis server (%s:%s). Error: %s', $server['host'], $server['port'], $e->getMessage())
94+
sprintf('Failed to connect to Redis server (%s). Error: %s', $redis_server, $e->getMessage())
8595
);
8696
}
8797

@@ -91,15 +101,15 @@ private function connect(array $server): Redis {
91101
}
92102
} catch (Exception $e) {
93103
throw new DashboardException(
94-
sprintf('Could not authenticate with Redis server (%s:%s). Error: %s', $server['host'], $server['port'], $e->getMessage())
104+
sprintf('Could not authenticate with Redis server (%s). Error: %s', $redis_server, $e->getMessage())
95105
);
96106
}
97107

98108
try {
99109
$redis->select(Http::get('db', 'int', $server['database'] ?? 0));
100110
} catch (Exception $e) {
101111
throw new DashboardException(
102-
sprintf('Could not select Redis database (%s:%s). Error: %s', $server['host'], $server['port'], $e->getMessage())
112+
sprintf('Could not select Redis database (%s). Error: %s', $redis_server, $e->getMessage())
103113
);
104114
}
105115

0 commit comments

Comments
 (0)