Skip to content

Commit 930c324

Browse files
committed
1316: support resilient redis mode for cache utils
Tests passing: ``` $ make busted-util /usr/local/openresty/luajit/bin/rover exec bin/busted "spec/threescale_utils_spec.lua" ●●● 3 successes / 0 failures / 0 errors / 0 pending : 0.02537 seconds ```
1 parent f018994 commit 930c324

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

gateway/src/apicast/threescale_utils.lua

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function _M.connect_redis(options)
124124
local opts = {}
125125

126126
local url = options and options.url or env.get('REDIS_URL')
127-
127+
local resilient = options.resilient
128128

129129
if url then
130130
url = resty_url.split(url, 'redis')
@@ -152,22 +152,34 @@ function _M.connect_redis(options)
152152

153153
local ok, err = red:connect(_M.resolve(host, port))
154154
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
156160
end
157161

158162
if opts.password then
159163
ok = red:auth(opts.password)
160164

161165
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
163171
end
164172
end
165173

166174
if opts.db then
167175
ok = red:select(opts.db)
168176

169177
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
171183
end
172184
end
173185

@@ -187,17 +199,25 @@ function _M.match_xml_element(xml, element, value)
187199
return string.find(xml, pattern, xml_header_len, xml_header_len, true)
188200
end
189201

190-
-- error and exit
191-
function _M.error(...)
202+
-- error without exit
203+
function _M.error_silently(...)
192204
if ngx.get_phase() == 'timer' then
193205
return table.concat(table.pack(...))
194206
else
195207
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
196208
ngx.say(...)
197-
ngx.exit(ngx.status)
198209
end
199210
end
200211

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+
201221
function _M.missing_args(text)
202222
ngx.say(text)
203223
ngx.exit(ngx.HTTP_OK)

spec/threescale_utils_spec.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,27 @@ describe('3scale utils', function()
1010

1111
assert.equal('one two three', error)
1212
end)
13+
14+
it('.error fails the nginx chain', function()
15+
stub(ngx, 'get_phase', function() return 'init' end)
16+
stub(ngx, 'say', function(...) return nil end)
17+
local exit = spy.on(ngx, 'exit', function(s) return 'exited!' end)
18+
19+
local error = _M.error('cache issue ' .. 'host:' .. 6379)
20+
21+
assert.spy(ngx.exit).was_called(1)
22+
assert.spy(ngx.say).was.called_with('cache issue ' .. 'host:' .. 6379)
23+
end)
24+
25+
it('.error_silently logs the error without exiting chain', function()
26+
stub(ngx, 'get_phase', function() return 'init' end)
27+
stub(ngx, 'say', function(...) return nil end)
28+
29+
local exit = spy.on(ngx, 'exit', function(s) return 'exited!' end)
30+
local error = _M.error_silently('redis is not reachable')
31+
32+
assert.spy(ngx.exit).was_not_called()
33+
assert.spy(ngx.say).was.called_with('redis is not reachable')
34+
end)
1335
end)
1436
end)

0 commit comments

Comments
 (0)