Skip to content

Commit

Permalink
1316: support resilient redis mode for cache util
Browse files Browse the repository at this point in the history
Tests run:
$ bash-4.4$ make busted-util
EXTRA_CFLAGS="-DHAVE_EVP_KDF_CTX=1" /usr/local/openresty/luajit/bin/rover install --roverfile=gateway/Roverfile > /dev/null
/usr/local/openresty/luajit/bin/rover exec bin/busted "spec/threescale_utils_spec.lua"
●●
2 successes / 0 failures / 0 errors / 0 pending : 0.029941 seconds
  • Loading branch information
abarrak committed Feb 10, 2022
1 parent f018994 commit 7788167
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
14 changes: 8 additions & 6 deletions gateway/src/apicast/threescale_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function _M.connect_redis(options)
local opts = {}

local url = options and options.url or env.get('REDIS_URL')

local resilient = options.resilient or false

if url then
url = resty_url.split(url, 'redis')
Expand Down Expand Up @@ -152,22 +152,22 @@ function _M.connect_redis(options)

local ok, err = red:connect(_M.resolve(host, port))
if not ok then
return nil, _M.error("failed to connect to redis on ", host, ":", port, ": ", err)
return nil, _M.error(resilient, "failed to connect to redis on ", host, ":", port, ": ", err)
end

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

if not ok then
return nil, _M.error("failed to auth on redis ", host, ":", port)
return nil, _M.error(resilient, "failed to auth on redis ", host, ":", port)
end
end

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

if not ok then
return nil, _M.error("failed to select db ", opts.db, " on redis ", host, ":", port)
return nil, _M.error(resilient, "failed to select db ", opts.db, " on redis ", host, ":", port)
end
end

Expand All @@ -188,13 +188,15 @@ function _M.match_xml_element(xml, element, value)
end

-- error and exit
function _M.error(...)
function _M.error(resilient, ...)
if ngx.get_phase() == 'timer' then
return table.concat(table.pack(...))
else
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
ngx.say(...)
ngx.exit(ngx.status)
if not resilient then
ngx.exit(ngx.status)
end
end
end

Expand Down
11 changes: 10 additions & 1 deletion spec/threescale_utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ describe('3scale utils', function()
describe('.error', function()
it('returns concatenated error in timer phase', function()
local get_phase = spy.on(ngx, 'get_phase', function() return 'timer' end)
local error = _M.error('one', ' two', ' three')
local error = _M.error(nil, 'one', ' two', ' three')

assert.spy(get_phase).was_called(1)

assert.equal('one two three', error)
end)

it('does not terminate with nginx.exit if resilient flag is set', function()
local exit = spy.on(ngx, 'exit', function() return 'exited!' end)
local error = _M.error(true, 'redis is not reachable')

assert.spy(exit).was_not_called()

assert.equal('redis is not reachable', error)
end)
end)
end)

0 comments on commit 7788167

Please sign in to comment.