Skip to content

Commit e6be354

Browse files
lohanidamodarclaude
andcommitted
feat: add configurable connect and read timeouts to Redis connections
Queue Redis connections previously used PHP's default timeout of 0 (blocking indefinitely). When Redis becomes unresponsive due to contention (e.g. Dragonfly TxQueue saturation), worker BRPOP calls would hang until the OS TCP timeout (21-127s), causing fatal "read error on connection" crashes and cascading restarts. Adds `connectTimeout` and `readTimeout` parameters (default 5s) to both `Redis` and `RedisCluster` connection classes so callers can configure fail-fast behaviour. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent aa80ffe commit e6be354

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/Queue/Connection/Redis.php

Lines changed: 7 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 = 5, float $readTimeout = 5)
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,8 @@ protected function getRedis(): \Redis
186190

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

189-
$this->redis->connect($this->host, $this->port);
193+
$this->redis->connect($this->host, $this->port, $this->connectTimeout);
194+
$this->redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->readTimeout);
190195

191196
return $this->redis;
192197
}

src/Queue/Connection/RedisCluster.php

Lines changed: 6 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 = 5, float $readTimeout = 5)
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,7 @@ protected function getRedis(): \RedisCluster
181185
return $this->redis;
182186
}
183187

184-
$this->redis = new \RedisCluster(null, $this->seeds);
188+
$this->redis = new \RedisCluster(null, $this->seeds, $this->connectTimeout, $this->readTimeout);
185189
return $this->redis;
186190
}
187191
}

0 commit comments

Comments
 (0)