swoole / library

📚 Swoole Library
https://wiki.swoole.com/#/library
Apache License 2.0
233 stars 59 forks source link

feat(redis-pool): add option for persistent connections #110

Closed TorstenDittmann closed 3 years ago

TorstenDittmann commented 3 years ago
twose commented 3 years ago

IMO, persistent things make no sense for Swoole-like projects as the Swoole server is already persistent, it only benefits FPM projects. And connection pool can not work on FPM mode beacuse pool is not persistent...

TorstenDittmann commented 3 years ago

We are using a Redis Connection from the ConnectionPool inside a WebSocket Worker and are losing connection to a subscribe every 60 seconds using this configuration:

ini_set('default_socket_timeout', -1);

// ...

$pool = new RedisPool((new RedisConfig)
    ->withHost($redisHost)
    ->withPort($redisPort)
    ->withAuth($redisAuth)
    ->withDbIndex(0)
);

We have previously used a persistent connection and didn't run into this issue before - thats why this was my first guess to implement.

TorstenDittmann commented 3 years ago

Just realized something - using the withReadTimeout will fix the disconnects. Meaning if I set it to 120 - it will disconnect after 120 seconds.

Looking at the official phpredis documentation, having a read timeout of 0 should mean unlimited, which would solve my problem.

So as it looks like this is not a Swoole problem on the first glance 🤔

TorstenDittmann commented 3 years ago

Fixed it with setting the option after getting a redis connection from the pool:

$redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);

Looks like this is not possible on the connect constructor of Redis.

twose commented 3 years ago

You mean the read_timeout paramter in connect() did not work, why?

TorstenDittmann commented 3 years ago

You mean the read_timeout paramter in connect() did not work, why?

Yes, it works with everything that is not 0 - just the unlimited timeout with passing 0 doesn't work.

twose commented 3 years ago

After took a quick look at source code, I think doc of phpredis is wrong, when read_timeout is zero, the connection will set read_timeout to default_socket_timeout. I noticed that you have already set default_socket_timeout to -1, I still do not know why it do not work for you....

TorstenDittmann commented 3 years ago

After took a quick look at source code, I think doc of phpredis is wrong, when read_timeout is zero, the connection will set read_timeout to default_socket_timeout. I noticed that you have already set default_socket_timeout to -1, I still do not know why it do not work for you....

Confused as well. But it's not a problem with Swoole - so I think we are good here 🙂