Recover from dropped connection in redis pub/sub
Asked Answered
D

2

9

I am running client that is connecting to a redis db. The client is on a WiFi connection and will drop the connection at times. Unfortunately, when this happens, the program just keeps running without throwing any type of warning.

r = redis.StrictRedis(host=XX, password=YY...)
ps = r.pubsub()
ps.subscribe("12345")
for items in ps.listen():
    if items['type'] == 'message':
       data = items['data']

Ideally, what I am looking for is a catch an event when the connection is lost, try and reestablish the connection, do some error correcting, then get things back up and running. Should this be done in the python program? Should I have an external watchdog?

Deist answered 30/11, 2012 at 22:42 Comment(0)
J
2

Unfortunately, one have to 'ping' Redis to check if it is available. If You try to put a value to Redis storage, it will raise an ConnectionError exception if connection is lost. But the listen() generator will not close automatically when connection is lost.

I think that hacking Redis' connection pool could help, give it a try.

P.S. In is very insecure to connect to redis in an untrusted network environment.

Jazzy answered 4/10, 2013 at 11:4 Comment(1)
If using pubsub with a connection pool and testWhileIdle enabled, I assume a ping will be transmitted and detect the failed connection?Ungenerous
B
1

This is an old, old question but I linked one of my own questions to it and happened to run across it again. It turned out there was a bug in the redis library that caused the client to enter an infinite loop attempting to reconnect if it lost connection to the redis server. I debugged the issue and PR'd the change. it was merged a long time ago now. Once surfaced the maintainer also knew of a second location that had the same issue.

This problem shouldn't occur anymore.

To fully answer the question, I can't remember which error it is given the time since I fixed this but there is now a specific error returned you can catch and reconnect on.

Bolinger answered 3/9, 2019 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.