Open buendia opened 13 years ago
There are two reasons why automatic reconnection is not implemented in redis-lua:
1) it's not something trivial to implement in a generic way. Aside from the need to keep track of the current database after using SELECT, you cannot simply reconnect while using MULTI / EXEC, SUBSCRIBE or when pipelining commands. 2) the current design of redis-lua poses some issues in that regard, but it's something that will get addressed with the next major version.
Currently redis-lua 2.0.2 allows developers to define a custom error handler for the Redis module (see Redis.error
), you might want to look into that and eventually implement your own automatic reconnection strategy that fits your use case. Admittedly Redis.error is not that good for this specific case but you can catch every error being raised inside the library and act accordingly. It's better than forcing a sub-par or half-baked feature on users for now.
I think that, at least, redis:ping() should return false without error.
Now there is no easy way to check the connection.
proper code should be
-- check connection to redis-server and reconnect if connection lost
local ok, err = pcall(function() return client_redis:ping() end)
if ok then
ok = err
end
if not ok then
-- reconnect to redis-server
client_redis = redis.connect('127.0.0.1', '6379')
end
Lack of auto-reconnect causes major unreliability in production.
Why is that there is no re-connect like redis-rb?
One trivial solution is every time the client is called, to check for a live connection:
There are two problems with this approach:
pcall(redis_client:ping())
is not efficient.