make sure balancer gets deleted when ther is no backend
This commit is contained in:
parent
e9dc275b81
commit
da3a87646a
2 changed files with 39 additions and 16 deletions
|
@ -62,6 +62,7 @@ end
|
||||||
local function sync_backends()
|
local function sync_backends()
|
||||||
local backends_data = configuration.get_backends_data()
|
local backends_data = configuration.get_backends_data()
|
||||||
if not backends_data then
|
if not backends_data then
|
||||||
|
balancers = {}
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -71,9 +72,22 @@ local function sync_backends()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, new_backend in pairs(new_backends) do
|
local balancers_to_keep = {}
|
||||||
|
for _, new_backend in ipairs(new_backends) do
|
||||||
sync_backend(new_backend)
|
sync_backend(new_backend)
|
||||||
|
balancers_to_keep[new_backend.name] = balancers[new_backend.name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
for backend_name, _ in pairs(balancers) do
|
||||||
|
if not balancers_to_keep[backend_name] then
|
||||||
|
balancers[backend_name] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_balancer()
|
||||||
|
local backend_name = ngx.var.proxy_upstream_name
|
||||||
|
return balancers[backend_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.init_worker()
|
function _M.init_worker()
|
||||||
|
@ -84,29 +98,24 @@ function _M.init_worker()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.call()
|
function _M.rewrite()
|
||||||
local phase = ngx.get_phase()
|
local balancer = get_balancer()
|
||||||
if phase ~= "log" and phase ~= "balancer" then
|
|
||||||
ngx.log(ngx.ERR, "must be called in balancer or log, but was called in: " .. phase)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local backend_name = ngx.var.proxy_upstream_name
|
|
||||||
local balancer = balancers[backend_name]
|
|
||||||
if not balancer then
|
if not balancer then
|
||||||
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
|
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
|
||||||
return ngx.exit(ngx.status)
|
return ngx.exit(ngx.status)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if phase == "log" then
|
function _M.balance()
|
||||||
balancer:after_balance()
|
local balancer = get_balancer()
|
||||||
|
if not balancer then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local host, port = balancer:balance()
|
local host, port = balancer:balance()
|
||||||
if not host then
|
if not host then
|
||||||
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
|
ngx.log(ngx.WARN, "no host returned, balancer: " .. balancer.name)
|
||||||
return ngx.exit(ngx.status)
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
ngx_balancer.set_more_tries(1)
|
ngx_balancer.set_more_tries(1)
|
||||||
|
@ -117,6 +126,15 @@ function _M.call()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function _M.log()
|
||||||
|
local balancer = get_balancer()
|
||||||
|
if not balancer then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
balancer:after_balance()
|
||||||
|
end
|
||||||
|
|
||||||
if _TEST then
|
if _TEST then
|
||||||
_M.get_implementation = get_implementation
|
_M.get_implementation = get_implementation
|
||||||
_M.sync_backend = sync_backend
|
_M.sync_backend = sync_backend
|
||||||
|
|
|
@ -389,7 +389,7 @@ http {
|
||||||
server 0.0.0.1; # placeholder
|
server 0.0.0.1; # placeholder
|
||||||
|
|
||||||
balancer_by_lua_block {
|
balancer_by_lua_block {
|
||||||
balancer.call()
|
balancer.balance()
|
||||||
}
|
}
|
||||||
|
|
||||||
{{ if (gt $cfg.UpstreamKeepaliveConnections 0) }}
|
{{ if (gt $cfg.UpstreamKeepaliveConnections 0) }}
|
||||||
|
@ -833,6 +833,11 @@ stream {
|
||||||
|
|
||||||
location {{ $path }} {
|
location {{ $path }} {
|
||||||
{{ if not $all.DisableLua }}
|
{{ if not $all.DisableLua }}
|
||||||
|
rewrite_by_lua_block {
|
||||||
|
{{ if $all.DynamicConfigurationEnabled}}
|
||||||
|
balancer.rewrite()
|
||||||
|
{{ end }}
|
||||||
|
}
|
||||||
{{ if shouldConfigureLuaRestyWAF $all.Cfg.DisableLuaRestyWAF $location.LuaRestyWAF.Mode }}
|
{{ if shouldConfigureLuaRestyWAF $all.Cfg.DisableLuaRestyWAF $location.LuaRestyWAF.Mode }}
|
||||||
access_by_lua_block {
|
access_by_lua_block {
|
||||||
local lua_resty_waf = require("resty.waf")
|
local lua_resty_waf = require("resty.waf")
|
||||||
|
@ -880,7 +885,7 @@ stream {
|
||||||
waf:exec()
|
waf:exec()
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ if $all.DynamicConfigurationEnabled}}
|
{{ if $all.DynamicConfigurationEnabled}}
|
||||||
balancer.call()
|
balancer.log()
|
||||||
{{ end }}
|
{{ end }}
|
||||||
}
|
}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
Loading…
Reference in a new issue