Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1316: support resilient redis mode for cache util #1324

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 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
abarrak marked this conversation as resolved.
Show resolved Hide resolved

if url then
url = resty_url.split(url, 'redis')
Expand All @@ -151,23 +151,36 @@ function _M.connect_redis(options)
red:set_timeout(opts.timeout)

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
abarrak marked this conversation as resolved.
Show resolved Hide resolved
return nil, _M.error("failed to connect to redis on ", host, ":", port, ": ", err)
else
return nil, _M.error_gracefully("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_gracefully("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_gracefully("failed to select db ", opts.db, " on redis ", host, ":", port)
end
end
end

Expand All @@ -187,17 +200,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_gracefully(...)
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_gracefully(...)
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_gracefully 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_gracefully('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)