From b4e6513fc8fbb6f10b10c9001f61c1fcad2329d2 Mon Sep 17 00:00:00 2001 From: Elvin Efendi Date: Mon, 28 May 2018 16:08:53 -0400 Subject: [PATCH] make sure `after_balance` is actually otional add inline comment to make LB algorithm change detection logic clearer also require port in addition to host --- rootfs/etc/nginx/lua/balancer.lua | 12 ++++++++++-- rootfs/etc/nginx/lua/balancer/resty.lua | 3 --- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/rootfs/etc/nginx/lua/balancer.lua b/rootfs/etc/nginx/lua/balancer.lua index 4487dad16..beabb418a 100644 --- a/rootfs/etc/nginx/lua/balancer.lua +++ b/rootfs/etc/nginx/lua/balancer.lua @@ -49,6 +49,9 @@ local function sync_backend(backend) return end + -- every implementation is the metatable of its instances (see .new(...) functions) + -- here we check if `balancer` is the instance of `implementation` + -- if it is not then we deduce LB algorithm has changed for the backend if getmetatable(balancer) ~= implementation then ngx.log(ngx.INFO, string.format("LB algorithm changed from %s to %s, resetting the instance", balancer.name, implementation.name)) @@ -113,8 +116,9 @@ function _M.balance() end local host, port = balancer:balance() - if not host then - ngx.log(ngx.WARN, "no host returned, balancer: " .. balancer.name) + if not (host and port) then + ngx.log(ngx.WARN, + string.format("host or port is missing, balancer: %s, host: %s, port: %s", balancer.name, host, port)) return end @@ -132,6 +136,10 @@ function _M.log() return end + if not balancer.after_balance then + return + end + balancer:after_balance() end diff --git a/rootfs/etc/nginx/lua/balancer/resty.lua b/rootfs/etc/nginx/lua/balancer/resty.lua index 356c19a66..457300ed5 100644 --- a/rootfs/etc/nginx/lua/balancer/resty.lua +++ b/rootfs/etc/nginx/lua/balancer/resty.lua @@ -19,7 +19,4 @@ function _M.sync(self, backend) self.instance:reinit(nodes) end -function _M.after_balance(_) -end - return _M