Skip to content

Commit 08e361d

Browse files
authored
Merge pull request #71 from utopia-php/feat/redis-publisher-timeouts
Add connect and read timeout support to Redis connections
2 parents aa80ffe + 47114c1 commit 08e361d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Queue/Connection/Redis.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ class Redis implements Connection
1010
protected int $port;
1111
protected ?string $user;
1212
protected ?string $password;
13+
protected float $connectTimeout;
14+
protected float $readTimeout;
1315
protected ?\Redis $redis = null;
1416

15-
public function __construct(string $host, int $port = 6379, ?string $user = null, ?string $password = null)
17+
public function __construct(string $host, int $port = 6379, ?string $user = null, ?string $password = null, float $connectTimeout = -1, float $readTimeout = -1)
1618
{
1719
$this->host = $host;
1820
$this->port = $port;
1921
$this->user = $user;
2022
$this->password = $password;
23+
$this->connectTimeout = $connectTimeout;
24+
$this->readTimeout = $readTimeout;
2125
}
2226

2327
public function rightPopLeftPushArray(string $queue, string $destination, int $timeout): array|false
@@ -186,7 +190,12 @@ protected function getRedis(): \Redis
186190

187191
$this->redis = new \Redis();
188192

189-
$this->redis->connect($this->host, $this->port);
193+
$connectTimeout = $this->connectTimeout < 0 ? 0 : $this->connectTimeout;
194+
$this->redis->connect($this->host, $this->port, $connectTimeout);
195+
196+
if ($this->readTimeout >= 0) {
197+
$this->redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->readTimeout);
198+
}
190199

191200
return $this->redis;
192201
}

src/Queue/Connection/RedisCluster.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
class RedisCluster implements Connection
88
{
99
protected array $seeds;
10+
protected float $connectTimeout;
11+
protected float $readTimeout;
1012
protected ?\RedisCluster $redis = null;
1113

12-
public function __construct(array $seeds)
14+
public function __construct(array $seeds, float $connectTimeout = -1, float $readTimeout = -1)
1315
{
1416
$this->seeds = $seeds;
17+
$this->connectTimeout = $connectTimeout;
18+
$this->readTimeout = $readTimeout;
1519
}
1620

1721
public function rightPopLeftPushArray(string $queue, string $destination, int $timeout): array|false
@@ -181,7 +185,9 @@ protected function getRedis(): \RedisCluster
181185
return $this->redis;
182186
}
183187

184-
$this->redis = new \RedisCluster(null, $this->seeds);
188+
$connectTimeout = $this->connectTimeout < 0 ? 0 : $this->connectTimeout;
189+
$readTimeout = $this->readTimeout < 0 ? 0 : $this->readTimeout;
190+
$this->redis = new \RedisCluster(null, $this->seeds, $connectTimeout, $readTimeout);
185191
return $this->redis;
186192
}
187193
}

0 commit comments

Comments
 (0)