Merge pull request #2511 from Shopify/bug-fix
force backend sync when worker starts
This commit is contained in:
commit
7983a2b9d3
3 changed files with 9 additions and 8 deletions
File diff suppressed because one or more lines are too long
|
@ -28,6 +28,7 @@ local function get_current_backend()
|
||||||
local backend = backends:get(backend_name)
|
local backend = backends:get(backend_name)
|
||||||
|
|
||||||
if not backend then
|
if not backend then
|
||||||
|
-- TODO(elvinefendi) maybe force backend sync here?
|
||||||
ngx.log(ngx.WARN, "no backend configuration found for " .. tostring(backend_name))
|
ngx.log(ngx.WARN, "no backend configuration found for " .. tostring(backend_name))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -139,6 +140,7 @@ local function after_balance()
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.init_worker()
|
function _M.init_worker()
|
||||||
|
sync_backends() -- when worker starts, sync backends without delay
|
||||||
_, err = ngx.timer.every(BACKENDS_SYNC_INTERVAL, sync_backends)
|
_, err = ngx.timer.every(BACKENDS_SYNC_INTERVAL, sync_backends)
|
||||||
if err then
|
if err then
|
||||||
ngx.log(ngx.ERR, string.format("error when setting up timer.every for sync_backends: %s", tostring(err)))
|
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)
|
return error("must be called in balancer or log, but was called in: " .. phase)
|
||||||
end
|
end
|
||||||
|
|
||||||
ngx_balancer.set_more_tries(1)
|
|
||||||
|
|
||||||
local host, port = balance()
|
local host, port = balance()
|
||||||
if not host then
|
if not host then
|
||||||
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
|
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
|
||||||
return
|
return ngx.exit(ngx.status)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ngx_balancer.set_more_tries(1)
|
||||||
|
|
||||||
local ok
|
local ok
|
||||||
ok, err = ngx_balancer.set_current_peer(host, port)
|
ok, err = ngx_balancer.set_current_peer(host, port)
|
||||||
if ok then
|
if ok then
|
||||||
|
|
|
@ -71,7 +71,8 @@ local function init()
|
||||||
WARN = "warn",
|
WARN = "warn",
|
||||||
INFO = "info",
|
INFO = "info",
|
||||||
ERR = "err",
|
ERR = "err",
|
||||||
HTTP_SERVICE_UNAVAILABLE = 503
|
HTTP_SERVICE_UNAVAILABLE = 503,
|
||||||
|
exit = function(status) return end,
|
||||||
}
|
}
|
||||||
package.loaded["ngx.balancer"] = mock_ngx_balancer
|
package.loaded["ngx.balancer"] = mock_ngx_balancer
|
||||||
package.loaded["resty.lrucache"] = mock_lrucache
|
package.loaded["resty.lrucache"] = mock_lrucache
|
||||||
|
@ -218,13 +219,11 @@ describe("[balancer_test]", function()
|
||||||
mock_backends._vals = {}
|
mock_backends._vals = {}
|
||||||
|
|
||||||
local backend_get_spy = spy.on(mock_backends, "get")
|
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")
|
local set_current_peer_spy = spy.on(mock_ngx_balancer, "set_current_peer")
|
||||||
|
|
||||||
assert.has_no_errors(balancer.call)
|
assert.has_no_errors(balancer.call)
|
||||||
assert.are_equal(ngx.status, 503)
|
assert.are_equal(ngx.status, 503)
|
||||||
assert.spy(backend_get_spy).was_called_with(match.is_table(), "mock_rr_backend")
|
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()
|
assert.spy(set_current_peer_spy).was_not_called()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
Loading…
Reference in a new issue