From 0aa763d898e623c6a20d4f47d0e3c68deed0aec0 Mon Sep 17 00:00:00 2001 From: Elvin Efendi Date: Fri, 25 May 2018 21:25:41 -0400 Subject: [PATCH] make sure balancer gets deleted when ther is no backend --- rootfs/etc/nginx/lua/balancer.lua | 46 +++++++++++++++++++--------- rootfs/etc/nginx/template/nginx.tmpl | 9 ++++-- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/rootfs/etc/nginx/lua/balancer.lua b/rootfs/etc/nginx/lua/balancer.lua index 727384d98..4487dad16 100644 --- a/rootfs/etc/nginx/lua/balancer.lua +++ b/rootfs/etc/nginx/lua/balancer.lua @@ -62,6 +62,7 @@ end local function sync_backends() local backends_data = configuration.get_backends_data() if not backends_data then + balancers = {} return end @@ -71,9 +72,22 @@ local function sync_backends() return 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) + balancers_to_keep[new_backend.name] = balancers[new_backend.name] 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 function _M.init_worker() @@ -84,29 +98,24 @@ function _M.init_worker() end end -function _M.call() - local phase = ngx.get_phase() - 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] +function _M.rewrite() + local balancer = get_balancer() if not balancer then ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE return ngx.exit(ngx.status) end +end - if phase == "log" then - balancer:after_balance() +function _M.balance() + local balancer = get_balancer() + if not balancer then return end local host, port = balancer:balance() if not host then - ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE - return ngx.exit(ngx.status) + ngx.log(ngx.WARN, "no host returned, balancer: " .. balancer.name) + return end ngx_balancer.set_more_tries(1) @@ -117,6 +126,15 @@ function _M.call() end end +function _M.log() + local balancer = get_balancer() + if not balancer then + return + end + + balancer:after_balance() +end + if _TEST then _M.get_implementation = get_implementation _M.sync_backend = sync_backend diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index dd6e1fb0f..fa1fa49f3 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -389,7 +389,7 @@ http { server 0.0.0.1; # placeholder balancer_by_lua_block { - balancer.call() + balancer.balance() } {{ if (gt $cfg.UpstreamKeepaliveConnections 0) }} @@ -833,6 +833,11 @@ stream { location {{ $path }} { {{ if not $all.DisableLua }} + rewrite_by_lua_block { + {{ if $all.DynamicConfigurationEnabled}} + balancer.rewrite() + {{ end }} + } {{ if shouldConfigureLuaRestyWAF $all.Cfg.DisableLuaRestyWAF $location.LuaRestyWAF.Mode }} access_by_lua_block { local lua_resty_waf = require("resty.waf") @@ -880,7 +885,7 @@ stream { waf:exec() {{ end }} {{ if $all.DynamicConfigurationEnabled}} - balancer.call() + balancer.log() {{ end }} } {{ end }}