Skip to content

Commit

Permalink
1316: support resilient redis mode for cache utils
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
abarrak committed Feb 11, 2022
1 parent f018994 commit 930c324
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
34 changes: 27 additions & 7 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

if url then
url = resty_url.split(url, 'redis')
Expand Down Expand Up @@ -152,22 +152,34 @@ 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)
if not resilient then
return nil, _M.error("failed to connect to redis on ", host, ":", port, ": ", err)
else
return nil, _M.error_silently("failed to connect to redis on ", host, ":", port, ": ", err)
end
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)
if not resilient then
return nil, _M.error("failed to auth on redis ", host, ":", port)
else
return nil, _M.error_silently("failed to auth on redis ", host, ":", port)
end
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)
if not resilient then
return nil, _M.error("failed to select db ", opts.db, " on redis ", host, ":", port)
else
return nil, _M.error_silently("failed to select db ", opts.db, " on redis ", host, ":", port)
end
end
end

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

-- error and exit
function _M.error(...)
-- error without exit
function _M.error_silently(...)
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)
end
end

-- error and exit
function _M.error(...)
local err = _M.error_silently(...)
if err then
return err
end
ngx.exit(ngx.status)
end

function _M.missing_args(text)
ngx.say(text)
ngx.exit(ngx.HTTP_OK)
Expand Down
22 changes: 22 additions & 0 deletions spec/threescale_utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,27 @@ describe('3scale utils', function()

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

it('.error fails the nginx chain', function()
stub(ngx, 'get_phase', function() return 'init' end)
stub(ngx, 'say', function(...) return nil end)
local exit = spy.on(ngx, 'exit', function(s) return 'exited!' end)

local error = _M.error('cache issue ' .. 'host:' .. 6379)

assert.spy(ngx.exit).was_called(1)
assert.spy(ngx.say).was.called_with('cache issue ' .. 'host:' .. 6379)
end)

it('.error_silently logs the error without exiting chain', function()
stub(ngx, 'get_phase', function() return 'init' end)
stub(ngx, 'say', function(...) return nil end)

local exit = spy.on(ngx, 'exit', function(s) return 'exited!' end)
local error = _M.error_silently('redis is not reachable')

assert.spy(ngx.exit).was_not_called()
assert.spy(ngx.say).was.called_with('redis is not reachable')
end)
end)
end)

0 comments on commit 930c324

Please sign in to comment.