openresty/lua-resty-redis — pull request #45 by h4lflife (2014-05-22 19:32:26+00:00)
Title: Redis cluster client

Redis cluster client.

This is implementated as a wrapper over the existing resty-redis client adding cluster functionality. Basic functionality except pipelining is working. Working on pipelining support.

- Each cluster is identified by a "cluster id".
- Cluster representation identified by "cluster id" is shared across all requests within a worker process.
- Supports multiple redis clusters.
- Connection pooling as present in the existing resty-redis client is available.
- Performance nearly same as original resty-redis under normal conditions.

ref: https://github.com/antirez/redis-rb-cluster

Example:
```lua
local redis_cluster = require("redis_cluster")

local cluster_id = "test_cluster"

-- Subset of nodes within the cluster
local startup_nodes = {
{"127.0.0.1", 7004},
{"127.0.0.1", 7000},
{"127.0.0.1", 7001}
}

local opt = {
timeout = 100,
keepalive_size = 100,
keepalive_duration = 60000
}

local rc = redis_cluster:new(cluster_id, startup_nodes, opt)

rc:initialize()

local ok, err = rc:set("key1", "val1")
if not ok then
ngx.say("Unable to set key1: ", err)
else
ngx.say("key1 set result: ", ok)
end

local res, err = rc:get("key1")
if not res then
ngx.say("Failed to get key1: ", err)
else
ngx.say("key1:", res)
end

-- (same as above, slightly faster)
res, err = rc:send_cluster_command("get", "key1")
if not res then
ngx.say("Failed to get key1 with send_cluster_command: ", err)
else
ngx.say("key1 using send_cluster_command:", res)
end
```

Open Issues:
- At the time of initialization(once per worker) and cluster reconfiguration, all requests during the time window of the data refresh(should be quite small, few ms) will try to update the cluster representation. It doesn't seem to have an effect on functionality but the condition needs to be removed as it would cause a small spike in latency during cluster reconfiguration. Would like to have opinion on this one.

- Pipeline "asking" request during cluster reconfiguration.

Please review and let me know of any comments.