@@ -124,7 +124,7 @@ function _M.connect_redis(options)
124
124
local opts = {}
125
125
126
126
local url = options and options .url or env .get (' REDIS_URL' )
127
-
127
+ local resilient = options . resilient
128
128
129
129
if url then
130
130
url = resty_url .split (url , ' redis' )
@@ -152,22 +152,34 @@ function _M.connect_redis(options)
152
152
153
153
local ok , err = red :connect (_M .resolve (host , port ))
154
154
if not ok then
155
- return nil , _M .error (" failed to connect to redis on " , host , " :" , port , " : " , err )
155
+ if not resilient then
156
+ return nil , _M .error (" failed to connect to redis on " , host , " :" , port , " : " , err )
157
+ else
158
+ return nil , _M .error_silently (" failed to connect to redis on " , host , " :" , port , " : " , err )
159
+ end
156
160
end
157
161
158
162
if opts .password then
159
163
ok = red :auth (opts .password )
160
164
161
165
if not ok then
162
- return nil , _M .error (" failed to auth on redis " , host , " :" , port )
166
+ if not resilient then
167
+ return nil , _M .error (" failed to auth on redis " , host , " :" , port )
168
+ else
169
+ return nil , _M .error_silently (" failed to auth on redis " , host , " :" , port )
170
+ end
163
171
end
164
172
end
165
173
166
174
if opts .db then
167
175
ok = red :select (opts .db )
168
176
169
177
if not ok then
170
- return nil , _M .error (" failed to select db " , opts .db , " on redis " , host , " :" , port )
178
+ if not resilient then
179
+ return nil , _M .error (" failed to select db " , opts .db , " on redis " , host , " :" , port )
180
+ else
181
+ return nil , _M .error_silently (" failed to select db " , opts .db , " on redis " , host , " :" , port )
182
+ end
171
183
end
172
184
end
173
185
@@ -187,17 +199,25 @@ function _M.match_xml_element(xml, element, value)
187
199
return string.find (xml , pattern , xml_header_len , xml_header_len , true )
188
200
end
189
201
190
- -- error and exit
191
- function _M .error (...)
202
+ -- error without exit
203
+ function _M .error_silently (...)
192
204
if ngx .get_phase () == ' timer' then
193
205
return table.concat (table.pack (... ))
194
206
else
195
207
ngx .status = ngx .HTTP_INTERNAL_SERVER_ERROR
196
208
ngx .say (... )
197
- ngx .exit (ngx .status )
198
209
end
199
210
end
200
211
212
+ -- error and exit
213
+ function _M .error (...)
214
+ local err = _M .error_silently (... )
215
+ if err then
216
+ return err
217
+ end
218
+ ngx .exit (ngx .status )
219
+ end
220
+
201
221
function _M .missing_args (text )
202
222
ngx .say (text )
203
223
ngx .exit (ngx .HTTP_OK )
0 commit comments