nrk / redis-lua

A Lua client library for the redis key value storage system.
MIT License
733 stars 239 forks source link

Lack of auto-reconnect #9

Open buendia opened 13 years ago

buendia commented 13 years ago

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:

redis_client = Redis.connect('127.0.0.1', 6379)
function redis()
  local ok, err = pcall(redis_client:ping())
  if not ok then redis_client = Redis.connect('127.0.0.1', 6379); end

  return redis_client;
end

There are two problems with this approach:

nrk commented 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.

linuxmaniac commented 10 years ago

I think that, at least, redis:ping() should return false without error.

Now there is no easy way to check the connection.

bevinhex commented 3 months ago

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