1
1
# lua-resty-redis-connector
2
2
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 ) .
6
4
7
5
8
6
## Synopsis
9
7
8
+ Quick and simple authenticated connection on localhost to DB 2:
9
+
10
10
``` 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
+ ```
13
15
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:
15
17
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
+ })
17
25
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
+
19
43
host = " 127.0.0.1" ,
20
44
port = 6379 ,
21
45
db = 2 ,
22
- password = " PASSWORD" ,
23
- }
46
+ password = " mypass" ,
47
+ })
48
+
49
+ local redis , err = rc :connect ()
24
50
25
- if not redis then
26
- ngx . log ( ngx . ERR , err )
27
- end
51
+ -- ...
52
+
53
+ local ok , err = rc : set_keepalive ( redis )
28
54
```
29
55
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
+
30
72
## DSN format
31
73
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
34
77
35
78
The format for connecting directly to Redis is:
36
79
37
80
` redis://PASSWORD@HOST:PORT/DB `
38
81
39
82
The ` PASSWORD ` and ` DB ` fields are optional, all other components are required.
40
83
84
+ ### Connections via Redis Sentinel
85
+
41
86
When connecting via Redis Sentinel, the format is as follows:
42
87
43
88
` 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`,
48
93
* ` s ` : slave
49
94
* ` a ` : any (first tries the master, but will failover to a slave if required)
50
95
96
+ A table of ` sentinels ` must also be supplied. e.g.
51
97
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
+ ```
53
106
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.
56
107
57
- The defaults are as follows:
108
+ ## Default Parameters
58
109
59
110
60
111
``` lua
61
112
{
113
+ connect_timeout = 100 ,
114
+ read_timeout = 1000 ,
115
+ connection_options = {}, -- pool, etc
116
+ keepalive_timeout = 60000 ,
117
+ keepalive_poolsize = 30 ,
118
+
62
119
host = " 127.0.0.1" ,
63
120
port = " 6379" ,
64
- path = nil , -- unix socket path, e.g. /tmp/redis.sock
121
+ path = " " , -- unix socket path, e.g. /tmp/redis.sock
65
122
password = " " ,
66
123
db = 0 ,
124
+
67
125
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 = {},
82
128
}
83
129
```
84
130
85
131
86
132
## API
87
133
88
134
* [ new] ( #new )
89
- * [ set_connect_timeout] ( #set_connect_timeout )
90
- * [ set_read_timeout] ( #set_read_timeout )
91
- * [ set_connection_options] ( #set_connection_options )
92
135
* [ connect] ( #connect )
93
136
* [ Utilities] ( #utilities )
94
137
* [ connect_via_sentinel] ( #connect_via_sentinel )
95
138
* [ try_hosts] ( #try_hosts )
96
139
* [ connect_to_host] ( #connect_to_host )
97
- * [ Sentinel Utilities] ( #sentinel-utilities )
98
140
* [ sentinel.get_master] ( #sentinelget_master )
99
141
* [ sentinel.get_slaves] ( #sentinelget_slaves )
100
142
@@ -106,38 +148,18 @@ local redis, err = rc:connect{
106
148
Creates the Redis Connector object. In case of failures, returns ` nil ` and a string describing the error.
107
149
108
150
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
-
132
151
### connect
133
152
134
153
` syntax: redis, err = rc:connect(params) `
135
154
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.
137
156
138
157
139
158
## Utilities
140
159
160
+ The following methods are not typically needed, but may be useful if a custom interface is required.
161
+
162
+
141
163
### connect_via_sentinel
142
164
143
165
` syntax: redis, err = rc:connect_via_sentinel(params) `
@@ -160,8 +182,6 @@ Tries the hosts supplied in order and returns the first successful connection.
160
182
Attempts to connect to the supplied ` host ` .
161
183
162
184
163
- ## Sentinel Utilities
164
-
165
185
### sentinel.get_master
166
186
167
187
` 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
176
196
Given a connected Sentinel instance and a master name, will return a list of registered slave Redis instances.
177
197
178
198
179
- ## TODO
180
-
181
- * Redis Cluster support.
182
-
183
-
184
199
# Author
185
200
186
201
James Hurst < james@pintsized.co.uk >
0 commit comments