Predis is giving 'Error while reading line from server'

Viewed 34606

I am using predis, it's subscribed to a channel and listening. It throws the following error (below) and dies after 60 secs exactly. It's surely not my web servers error or its timeout.

There is a similar issue being discussed here. Could not get much of it.

I tried setting connection_timeout in predis conf file to 0, but doesn't helps much.

Also if i keep using (send data to it and it processes) the worker it doesn't give any error. So its likely a timeout somewhere, and that too in connection.

Here is my code snippet, which is likely producing error, because if data is given to worker it runs this code and go forward, which produces no error after that.

$pubsub = $redis->pubSub();
$pubsub->subscribe($channel1);

foreach ($pubsub as $message) { //doing stuff here and unsubscribing from channel
}

Trace

PHP Fatal error:  Uncaught exception 'Predis\Network\ConnectionException' with message 'Error while reading line from the server' in Predis/Network/ConnectionBase.php:159 Stack trace:
#0 library/vendor/predis/lib/Predis/Network/StreamConnection.php(195): Predis\Network\ConnectionBase->onConnectionError('Error while rea...')
#1 library/vendor/predis/lib/Predis/PubSub/PubSubContext.php(259): Predis\Network\StreamConnection->read()
#2 library/vendor/predis/lib/Predis/PubSub/PubSubContext.php(206): Predis\PubSub\PubSubContext->getValue()
#3 pdf/file.php(16): Predis\PubSub\PubSubContext->current()
#4 {main}   thrown in Predis/Network/ConnectionBase.php on line 159

Checked the redis.conf timeout too, its also disabled.

5 Answers

I got the resolution to the problem. So, there is a limit to ports that a application server can connect to a particular application on another machine. These ports were getting exhausted. We increased the limit and the problem got resolved.

How we got to know about this problem ? In php, we were getting "Cannot assign requested address" error while creating a socket (error code 99).

At /etc/redis/redis.conf , set

timeout = 0 

I'm using Heroku and solved this problem with switching from Redis Heroku to Redis Enterprise addon and then:

use Predis\Client as PredisClient;

To solve collision with GuzzleHttp\Client. You can leave the

as PredisClient

line if you are not usng GuzzleHttp.

And then connection:

$redisClient = new PredisClient(array(
     'host' => parse_url(env('REDIS_URL'), PHP_URL_HOST),
     'port' => parse_url(env('REDIS_URL'), PHP_URL_PORT),
     'password' => parse_url(env('REDIS_URL'), PHP_URL_PASS)
)

);

(You can find your 'REDIS_URL' automatically prefilled in Heroku config vars).

Related