force backend sync when worker starts

This commit is contained in:
Elvin Efendi 2018-05-14 17:02:09 -04:00
parent 6aeb7ceb39
commit 44ddd8abba
3 changed files with 9 additions and 8 deletions

File diff suppressed because one or more lines are too long

View file

@ -28,6 +28,7 @@ local function get_current_backend()
local backend = backends:get(backend_name)
if not backend then
-- TODO(elvinefendi) maybe force backend sync here?
ngx.log(ngx.WARN, "no backend configuration found for " .. tostring(backend_name))
end
@ -139,6 +140,7 @@ local function after_balance()
end
function _M.init_worker()
sync_backends() -- when worker starts, sync backends without delay
_, err = ngx.timer.every(BACKENDS_SYNC_INTERVAL, sync_backends)
if err then
ngx.log(ngx.ERR, string.format("error when setting up timer.every for sync_backends: %s", tostring(err)))
@ -155,14 +157,14 @@ function _M.call()
return error("must be called in balancer or log, but was called in: " .. phase)
end
ngx_balancer.set_more_tries(1)
local host, port = balance()
if not host then
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
return
return ngx.exit(ngx.status)
end
ngx_balancer.set_more_tries(1)
local ok
ok, err = ngx_balancer.set_current_peer(host, port)
if ok then

View file

@ -71,7 +71,8 @@ local function init()
WARN = "warn",
INFO = "info",
ERR = "err",
HTTP_SERVICE_UNAVAILABLE = 503
HTTP_SERVICE_UNAVAILABLE = 503,
exit = function(status) return end,
}
package.loaded["ngx.balancer"] = mock_ngx_balancer
package.loaded["resty.lrucache"] = mock_lrucache
@ -218,13 +219,11 @@ describe("[balancer_test]", function()
mock_backends._vals = {}
local backend_get_spy = spy.on(mock_backends, "get")
local set_more_tries_spy = spy.on(mock_ngx_balancer, "set_more_tries")
local set_current_peer_spy = spy.on(mock_ngx_balancer, "set_current_peer")
assert.has_no_errors(balancer.call)
assert.are_equal(ngx.status, 503)
assert.spy(backend_get_spy).was_called_with(match.is_table(), "mock_rr_backend")
assert.spy(set_more_tries_spy).was_called_with(1)
assert.spy(set_current_peer_spy).was_not_called()
end)
end)