diff --git a/src/Queue/Connection/Redis.php b/src/Queue/Connection/Redis.php index a0729b3..8900277 100644 --- a/src/Queue/Connection/Redis.php +++ b/src/Queue/Connection/Redis.php @@ -10,14 +10,18 @@ class Redis implements Connection protected int $port; protected ?string $user; protected ?string $password; + protected float $connectTimeout; + protected float $readTimeout; protected ?\Redis $redis = null; - public function __construct(string $host, int $port = 6379, ?string $user = null, ?string $password = null) + public function __construct(string $host, int $port = 6379, ?string $user = null, ?string $password = null, float $connectTimeout = -1, float $readTimeout = -1) { $this->host = $host; $this->port = $port; $this->user = $user; $this->password = $password; + $this->connectTimeout = $connectTimeout; + $this->readTimeout = $readTimeout; } public function rightPopLeftPushArray(string $queue, string $destination, int $timeout): array|false @@ -186,7 +190,12 @@ protected function getRedis(): \Redis $this->redis = new \Redis(); - $this->redis->connect($this->host, $this->port); + $connectTimeout = $this->connectTimeout < 0 ? 0 : $this->connectTimeout; + $this->redis->connect($this->host, $this->port, $connectTimeout); + + if ($this->readTimeout >= 0) { + $this->redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->readTimeout); + } return $this->redis; } diff --git a/src/Queue/Connection/RedisCluster.php b/src/Queue/Connection/RedisCluster.php index ec9ec68..1c8c0a9 100644 --- a/src/Queue/Connection/RedisCluster.php +++ b/src/Queue/Connection/RedisCluster.php @@ -7,11 +7,15 @@ class RedisCluster implements Connection { protected array $seeds; + protected float $connectTimeout; + protected float $readTimeout; protected ?\RedisCluster $redis = null; - public function __construct(array $seeds) + public function __construct(array $seeds, float $connectTimeout = -1, float $readTimeout = -1) { $this->seeds = $seeds; + $this->connectTimeout = $connectTimeout; + $this->readTimeout = $readTimeout; } public function rightPopLeftPushArray(string $queue, string $destination, int $timeout): array|false @@ -181,7 +185,9 @@ protected function getRedis(): \RedisCluster return $this->redis; } - $this->redis = new \RedisCluster(null, $this->seeds); + $connectTimeout = $this->connectTimeout < 0 ? 0 : $this->connectTimeout; + $readTimeout = $this->readTimeout < 0 ? 0 : $this->readTimeout; + $this->redis = new \RedisCluster(null, $this->seeds, $connectTimeout, $readTimeout); return $this->redis; } }