Skip to content

Commit 0d0bafe

Browse files
committed
Merge branch 'release/0.04'
2 parents 2b93187 + 8ccd3ae commit 0d0bafe

File tree

11 files changed

+873
-398
lines changed

11 files changed

+873
-398
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
t/servroot/
22
t/error.log
3+
luacov.*

.luacov

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
modules = {
2+
["resty.redis.connector"] = "lib/resty/redis/connector.lua",
3+
["resty.redis.sentinel"] = "lib/resty/redis/sentinel.lua",
4+
}

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
SHELL := /bin/bash # Cheat by using bash :)
22

3-
OPENRESTY_PREFIX = /usr/local/openresty-debug
3+
OPENRESTY_PREFIX = /usr/local/openresty
44

55
TEST_FILE ?= t
66
SENTINEL_TEST_FILE ?= $(TEST_FILE)/sentinel
@@ -141,8 +141,11 @@ check_ports:
141141
@$(foreach port,$(REDIS_PORTS),! lsof -i :$(port) &&) true 2>&1 > /dev/null
142142

143143
test_redis: flush_db
144-
$(TEST_REDIS_VARS) $(PROVE) $(TEST_FILE)
145144
util/lua-releng
145+
@rm -f luacov.stats.out
146+
$(TEST_REDIS_VARS) $(PROVE) $(TEST_FILE)
147+
@luacov
148+
@tail -7 luacov.report.out
146149

147150
test_leak: flush_db
148151
$(TEST_REDIS_VARS) TEST_NGINX_CHECK_LEAK=1 $(PROVE) $(TEST_FILE)

README.md

Lines changed: 84 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,88 @@
11
# lua-resty-redis-connector
22

3-
Connection utilities for [lua-resty-redis](https://github.com/openresty/lua-resty-redis), making
4-
it easy and reliable to connect to Redis hosts, either directly or via
5-
[Redis Sentinel](http://redis.io/topics/sentinel).
3+
Connection utilities for [lua-resty-redis](https://github.com/openresty/lua-resty-redis), making it easy and reliable to connect to Redis hosts, either directly or via [Redis Sentinel](http://redis.io/topics/sentinel).
64

75

86
## Synopsis
97

8+
Quick and simple authenticated connection on localhost to DB 2:
9+
1010
```lua
11-
local redis_connector = require "resty.redis.connector"
12-
local rc = redis_connector.new()
11+
local redis, err = require("resty.redis.connector").new({
12+
url = "redis://PASSWORD@127.0.0.1:6379/2",
13+
}):connect()
14+
```
1315

14-
local redis, err = rc:connect{ url = "redis://PASSWORD@127.0.0.1:6379/2" }
16+
More verbose configuration, with timeouts and a default password:
1517

16-
-- or...
18+
```lua
19+
local rc = require("resty.redis.connector").new({
20+
connect_timeout = 50,
21+
read_timeout = 5000,
22+
keepalive_timeout = 30000,
23+
password = "mypass",
24+
})
1725

18-
local redis, err = rc:connect{
26+
local redis, err = rc:connect({
27+
url = "redis://127.0.0.1:6379/2",
28+
})
29+
30+
-- ...
31+
32+
local ok, err = rc:set_keepalive(redis) -- uses keepalive params
33+
```
34+
35+
Keep all config in a table, to easily create / close connections as needed:
36+
37+
```lua
38+
local rc = require("resty.redis.connector").new({
39+
connect_timeout = 50,
40+
read_timeout = 5000,
41+
keepalive_timeout = 30000,
42+
1943
host = "127.0.0.1",
2044
port = 6379,
2145
db = 2,
22-
password = "PASSWORD",
23-
}
46+
password = "mypass",
47+
})
48+
49+
local redis, err = rc:connect()
2450

25-
if not redis then
26-
ngx.log(ngx.ERR, err)
27-
end
51+
-- ...
52+
53+
local ok, err = rc:set_keepalive(redis)
2854
```
2955

56+
`connect` can be used to override defaults given in `new`
57+
58+
59+
```lua
60+
local rc = require("resty.redis.connector").new({
61+
host = "127.0.0.1",
62+
port = 6379,
63+
db = 2,
64+
})
65+
66+
local redis, err = rc:connect({
67+
db = 5,
68+
})
69+
```
70+
71+
3072
## DSN format
3173

32-
The [connect](#connect) method accepts a single table of named arguments. If the `url` field is
33-
present then it will be parsed, overriding values supplied in the parameters table.
74+
If the `params.url` field is present then it will be parsed, overriding values supplied in the parameters table.
75+
76+
### Direct Redis connections
3477

3578
The format for connecting directly to Redis is:
3679

3780
`redis://PASSWORD@HOST:PORT/DB`
3881

3982
The `PASSWORD` and `DB` fields are optional, all other components are required.
4083

84+
### Connections via Redis Sentinel
85+
4186
When connecting via Redis Sentinel, the format is as follows:
4287

4388
`sentinel://PASSWORD@MASTER_NAME:ROLE/DB`
@@ -48,53 +93,50 @@ Again, `PASSWORD` and `DB` are optional. `ROLE` must be any of `m`, `s` or `a`,
4893
* `s`: slave
4994
* `a`: any (first tries the master, but will failover to a slave if required)
5095

96+
A table of `sentinels` must also be supplied. e.g.
5197

52-
## Parameters
98+
```lua
99+
local redis, err = rc:connect{
100+
url = "sentinel://mymaster:a/2",
101+
sentinels = {
102+
{ host = "127.0.0.1", port = 26379" },
103+
}
104+
}
105+
```
53106

54-
The [connect](#connect) method expects the following field values, either by falling back to
55-
defaults, populating the fields by parsing the DSN, or being specified directly.
56107

57-
The defaults are as follows:
108+
## Default Parameters
58109

59110

60111
```lua
61112
{
113+
connect_timeout = 100,
114+
read_timeout = 1000,
115+
connection_options = {}, -- pool, etc
116+
keepalive_timeout = 60000,
117+
keepalive_poolsize = 30,
118+
62119
host = "127.0.0.1",
63120
port = "6379",
64-
path = nil, -- unix socket path, e.g. /tmp/redis.sock
121+
path = "", -- unix socket path, e.g. /tmp/redis.sock
65122
password = "",
66123
db = 0,
124+
67125
master_name = "mymaster",
68-
role = "master", -- master | slave | any
69-
sentinels = nil,
70-
}
71-
```
72-
73-
Note that if `sentinel://` is supplied as the `url` parameter, a table of `sentinels` must also
74-
be supplied. e.g.
75-
76-
```lua
77-
local redis, err = rc:connect{
78-
url = "sentinel://mymaster:a/2",
79-
sentinels = {
80-
{ host = "127.0.0.1", port = 26379" },
81-
}
126+
role = "master", -- master | slave | any
127+
sentinels = {},
82128
}
83129
```
84130

85131

86132
## API
87133

88134
* [new](#new)
89-
* [set_connect_timeout](#set_connect_timeout)
90-
* [set_read_timeout](#set_read_timeout)
91-
* [set_connection_options](#set_connection_options)
92135
* [connect](#connect)
93136
* [Utilities](#utilities)
94137
* [connect_via_sentinel](#connect_via_sentinel)
95138
* [try_hosts](#try_hosts)
96139
* [connect_to_host](#connect_to_host)
97-
* [Sentinel Utilities](#sentinel-utilities)
98140
* [sentinel.get_master](#sentinelget_master)
99141
* [sentinel.get_slaves](#sentinelget_slaves)
100142

@@ -106,38 +148,18 @@ local redis, err = rc:connect{
106148
Creates the Redis Connector object. In case of failures, returns `nil` and a string describing the error.
107149

108150

109-
### set_connect_timeout
110-
111-
`syntax: rc:set_connect_timeout(100)`
112-
113-
Sets the cosocket connection timeout, in ms.
114-
115-
116-
117-
### set_read_timeout
118-
119-
`syntax: rc:set_read_timeout(500)`
120-
121-
Sets the cosocket read timeout, in ms.
122-
123-
124-
### set_connection_options
125-
126-
`syntax: rc:set_connection_options({ pool = params.host .. ":" .. params.port .. "/" .. params.db })`
127-
128-
Sets the connection options table, as supplied to [tcpsock:connect](https://github.com/openresty/lua-nginx-module#tcpsockconnect)
129-
method.
130-
131-
132151
### connect
133152

134153
`syntax: redis, err = rc:connect(params)`
135154

136-
Attempts to create a connection, according to the [params](#parameters) supplied.
155+
Attempts to create a connection, according to the [params](#parameters) supplied. If a connection cannot be made, returns `nil` and a string describing the reason.
137156

138157

139158
## Utilities
140159

160+
The following methods are not typically needed, but may be useful if a custom interface is required.
161+
162+
141163
### connect_via_sentinel
142164

143165
`syntax: redis, err = rc:connect_via_sentinel(params)`
@@ -160,8 +182,6 @@ Tries the hosts supplied in order and returns the first successful connection.
160182
Attempts to connect to the supplied `host`.
161183

162184

163-
## Sentinel Utilities
164-
165185
### sentinel.get_master
166186

167187
`syntax: master, err = sentinel.get_master(sentinel, master_name)`
@@ -176,11 +196,6 @@ Given a connected Sentinel instance and a master name, will return the current m
176196
Given a connected Sentinel instance and a master name, will return a list of registered slave Redis instances.
177197

178198

179-
## TODO
180-
181-
* Redis Cluster support.
182-
183-
184199
# Author
185200

186201
James Hurst <james@pintsized.co.uk>

0 commit comments

Comments
 (0)