amphp / redis

Efficient asynchronous communication with Redis servers, enabling scalable and responsive data storage and retrieval.

Home Page:https://amphp.org/redis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Client Timeout Bug

opened this issue · comments

redis-server config

# Close the connection after a client is idle for N seconds (0 to disable)
timeout 10
Amp\run(function () 
{
    try
    {
        $client = new \Amp\Redis\Client("tcp://localhost:6379");
        yield $client->set("foo", "21");
        $res = yield $client->get("foo"); 

        print($res . "\n"); //21
    }
    catch (Throwable $e)
    {
        print($e->getMessage());
    }
});

Problem: If you run this code, the loop will be stopped without any error or exception after 10 sec,
Is there any method to catch this ? After some investigation I find where is the problem:

Amp\Redis\ConnectException: Connection went away (read) in amphp/redis/lib/Connection.php:224

This can be a problem for thous who ex: Create a Tcp Server and use 1 redis connection for multiple tcp clients, as I do.

Where's the bug? The connection is closed after 10 seconds of inactivity as you configured it. It will be automatically reopened on the next command.

If you want to keep the loop alive, you can just register a dummy watcher, that just does nothing.

Amp\run(function () {
    Amp\repeat(function () {}, 60000);
});

Sorry about few details, (i will build an example)
In my case this was critical, I have a TcpServer who open a redis connection, and after 10 sec of idle, connection was closed and not reopen on tcp client, but the most strange, there where not any errors or exceptions the new tcp client just hang.

@umbri The loop is probably stopped because you don't have write / read watchers on your TcpServer?