Closed oledahle closed 1 year ago
The consume method catches "Connection is broken" exceptions from redis-plus-plus, but nothing more happens.
Yes, so far, if the connection is broken, you can no longer use the Subscriber
any longer. Instead, you should create a new Subscriber
. Check the doc for detail.
Do I need to trigger a PING command from consume() to make redis-plus-plus detect the failover, and then recreate the subscription to make this work?
You do not need to trigger PING, instead, if consume
throws an exception other than TimeoutError
or ReplyError
, recreate a new Subscriber
.
Regards
Thanks for to the tip, I'll try it out.
connOpts.keep_alive = true;
########################### if windows , use new hisredis.
/ Enable connection KeepAlive. / int redisEnableKeepAlive(redisContext *c) { if (redisKeepAlive(c, REDIS_KEEPALIVE_INTERVAL) != REDIS_OK) return REDIS_ERR; return REDIS_OK; }
##################### but it Check every 30 seconds by default
redis++ 设置这个keep_alive = true; 建议加个时间 参数。
@wb2712 Sorry, but hiredis does not expose such an API, i.e. redisKeepAlive
, as public. So you'd better create an issue or PR with hiredis.
Regards
For the record: Calling m_redis_master->subscriber() made the "parent" sentinel object switch over to the new master as soon as the election was complete and the new master had assumed its role.
sentinel_opts.keep_alive = true;
while (1) { try { sub->consume(); } catch (const TimeoutError& te) { continue; }catch (const Error& err) { // if set keep alive, will catch err, But the cycle is long break; } } // Reconnect other toReboot();
################################## It also depends on how the server exits, whether it exits normally or crashes. Similar to power-off or manual power-off, kill - 9 redis-server or kill - 15 redis-server. If kill - 9 exits, the service will be unavailable, which is not what the client can solve.
Which way is the master node offline constructed? Unplug the server network cable or disable the network card?
For this testing, I stopped the Redis master by hitting CTRL+C in an interactive docker session, so the redis server process shut down in a controlled manner, and then the IP address disappeared as well. This produced the generic sw::redis::Error exception with the text "Connection is broken". I'm sure other shutdown / offline scenarios will give slightly different errors.
I have a Redis master and replica, monitored by three sentinels, all as docker instances. When I stop the master, the sentinels quickly elect a new master. But my clients using redis-plus-plus Sentinel to get a Redis object and then subscribe to a pub/sub topic do not reconnect to the new master. The consume method catches "Connection is broken" exceptions from redis-plus-plus, but nothing more happens.
Client connection code very close to Readme example:
The consume method is something like this:
Expected behavior Redis object automatically connects to the new master after detecting failover.
Question Do I need to trigger a PING command from consume() to make redis-plus-plus detect the failover, and then recreate the subscription to make this work?
Environment: OS: Fedora 36 Compiler: GCC 12.2.1 Redis: 6.2.7 hiredis version: 1.0.2-2 redis-plus-plus version: 1.3.6