Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lack of auto-reconnect #9

Open
buendia opened this issue Jul 12, 2011 · 3 comments
Open

Lack of auto-reconnect #9

buendia opened this issue Jul 12, 2011 · 3 comments

Comments

@buendia
Copy link

buendia commented Jul 12, 2011

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:

  • Keep calling pcall(redis_client:ping()) is not efficient.
  • This does not work for the pubsub subscriber case.
@nrk
Copy link
Owner

nrk commented Jul 17, 2011

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
Copy link

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

Now there is no easy way to check the connection.

@bevinhex
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants