Fixed review findings.
This commit is contained in:
parent
880b3dc5f1
commit
f1839ddb42
4 changed files with 13 additions and 59 deletions
|
@ -1,5 +1,4 @@
|
||||||
local balancer_resty = require("balancer.resty")
|
local balancer_resty = require("balancer.resty")
|
||||||
local util = require("util")
|
|
||||||
local ck = require("resty.cookie")
|
local ck = require("resty.cookie")
|
||||||
local ngx_balancer = require("ngx.balancer")
|
local ngx_balancer = require("ngx.balancer")
|
||||||
local split = require("util.split")
|
local split = require("util.split")
|
||||||
|
@ -87,7 +86,6 @@ local function get_failed_upstreams()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function should_set_cookie(self)
|
local function should_set_cookie(self)
|
||||||
|
|
||||||
if self.cookie_session_affinity.locations and ngx.var.host then
|
if self.cookie_session_affinity.locations and ngx.var.host then
|
||||||
local locs = self.cookie_session_affinity.locations[ngx.var.host]
|
local locs = self.cookie_session_affinity.locations[ngx.var.host]
|
||||||
if locs == nil then
|
if locs == nil then
|
||||||
|
@ -115,7 +113,7 @@ end
|
||||||
function _M.balance(self)
|
function _M.balance(self)
|
||||||
local upstream_from_cookie
|
local upstream_from_cookie
|
||||||
|
|
||||||
local key = self:get_routing_key()
|
local key = self:get_cookie()
|
||||||
if key then
|
if key then
|
||||||
upstream_from_cookie = self.instance:find(key)
|
upstream_from_cookie = self.instance:find(key)
|
||||||
end
|
end
|
||||||
|
@ -134,7 +132,7 @@ function _M.balance(self)
|
||||||
if not new_upstream then
|
if not new_upstream then
|
||||||
ngx.log(ngx.WARN, string.format("failed to get new upstream; using upstream %s", new_upstream))
|
ngx.log(ngx.WARN, string.format("failed to get new upstream; using upstream %s", new_upstream))
|
||||||
elseif should_set_cookie(self) then
|
elseif should_set_cookie(self) then
|
||||||
self:set_routing_key(key)
|
self:set_cookie(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
return new_upstream
|
return new_upstream
|
||||||
|
@ -144,18 +142,6 @@ function _M.sync(self, backend)
|
||||||
-- reload balancer nodes
|
-- reload balancer nodes
|
||||||
balancer_resty.sync(self, backend)
|
balancer_resty.sync(self, backend)
|
||||||
|
|
||||||
-- Reload the balancer if any of the annotations have changed.
|
|
||||||
local changed = not util.deep_compare(
|
|
||||||
self.cookie_session_affinity,
|
|
||||||
backend.sessionAffinityConfig.cookieSessionAffinity
|
|
||||||
)
|
|
||||||
|
|
||||||
if not changed then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
ngx_log(INFO, string_format("[%s] nodes have changed for backend %s", self.name, backend.name))
|
|
||||||
|
|
||||||
self.traffic_shaping_policy = backend.trafficShapingPolicy
|
self.traffic_shaping_policy = backend.trafficShapingPolicy
|
||||||
self.alternative_backends = backend.alternativeBackends
|
self.alternative_backends = backend.alternativeBackends
|
||||||
self.cookie_session_affinity = backend.sessionAffinityConfig.cookieSessionAffinity
|
self.cookie_session_affinity = backend.sessionAffinityConfig.cookieSessionAffinity
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
-- pods.
|
-- pods.
|
||||||
--
|
--
|
||||||
local balancer_sticky = require("balancer.sticky")
|
local balancer_sticky = require("balancer.sticky")
|
||||||
local math = require("math")
|
local math_random = require("math").random
|
||||||
local resty_chash = require("resty.chash")
|
local resty_chash = require("resty.chash")
|
||||||
local util = require("util")
|
local util_get_nodes = require("util").get_nodes
|
||||||
|
|
||||||
local _M = balancer_sticky:new()
|
local _M = balancer_sticky:new()
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ local _M = balancer_sticky:new()
|
||||||
local MAX_UPSTREAM_CHECKS_COUNT = 20
|
local MAX_UPSTREAM_CHECKS_COUNT = 20
|
||||||
|
|
||||||
function _M.new(self, backend)
|
function _M.new(self, backend)
|
||||||
local nodes = util.get_nodes(backend.endpoints)
|
local nodes = util_get_nodes(backend.endpoints)
|
||||||
|
|
||||||
local o = {
|
local o = {
|
||||||
name = "sticky_balanced",
|
name = "sticky_balanced",
|
||||||
|
@ -33,17 +33,9 @@ function _M.new(self, backend)
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.get_routing_key(self)
|
|
||||||
return self:get_cookie(), nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function _M.set_routing_key(self, key)
|
|
||||||
self:set_cookie(key)
|
|
||||||
end
|
|
||||||
|
|
||||||
function _M.pick_new_upstream(self, failed_upstreams)
|
function _M.pick_new_upstream(self, failed_upstreams)
|
||||||
for i = 1, MAX_UPSTREAM_CHECKS_COUNT do
|
for i = 1, MAX_UPSTREAM_CHECKS_COUNT do
|
||||||
local key = string.format("%s.%s.%s", ngx.now() + i, ngx.worker.pid(), math.random(999999))
|
local key = string.format("%s.%s.%s", ngx.now() + i, ngx.worker.pid(), math_random(999999))
|
||||||
local new_upstream = self.instance:find(key)
|
local new_upstream = self.instance:find(key)
|
||||||
|
|
||||||
if not failed_upstreams[new_upstream] then
|
if not failed_upstreams[new_upstream] then
|
||||||
|
|
|
@ -4,13 +4,13 @@
|
||||||
-- be rebalanced.
|
-- be rebalanced.
|
||||||
--
|
--
|
||||||
local balancer_sticky = require("balancer.sticky")
|
local balancer_sticky = require("balancer.sticky")
|
||||||
local util = require("util")
|
local util_get_nodes = require("util").get_nodes
|
||||||
local util_nodemap = require("util.nodemap")
|
local util_nodemap = require("util.nodemap")
|
||||||
|
|
||||||
local _M = balancer_sticky:new()
|
local _M = balancer_sticky:new()
|
||||||
|
|
||||||
function _M.new(self, backend)
|
function _M.new(self, backend)
|
||||||
local nodes = util.get_nodes(backend.endpoints)
|
local nodes = util_get_nodes(backend.endpoints)
|
||||||
local hash_salt = backend["name"]
|
local hash_salt = backend["name"]
|
||||||
|
|
||||||
local o = {
|
local o = {
|
||||||
|
@ -26,29 +26,6 @@ function _M.new(self, backend)
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.get_routing_key(self)
|
|
||||||
local cookie_value = self:get_cookie()
|
|
||||||
|
|
||||||
if cookie_value then
|
|
||||||
-- format <timestamp>.<workder-pid>.<routing-key>
|
|
||||||
local routing_key = string.match(cookie_value, '[^\\.]+$')
|
|
||||||
|
|
||||||
if routing_key == nil then
|
|
||||||
local err = string.format("Failed to extract routing key from cookie '%s'!", cookie_value)
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
|
|
||||||
return routing_key, nil
|
|
||||||
end
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function _M.set_routing_key(self, key)
|
|
||||||
local value = string.format("%s.%s.%s", ngx.now(), ngx.worker.pid(), key)
|
|
||||||
self:set_cookie(value);
|
|
||||||
end
|
|
||||||
|
|
||||||
function _M.pick_new_upstream(self, failed_upstreams)
|
function _M.pick_new_upstream(self, failed_upstreams)
|
||||||
return self.instance:random_except(failed_upstreams)
|
return self.instance:random_except(failed_upstreams)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
local math = require("math")
|
local math_random = require("math").random
|
||||||
local util = require("util")
|
local util_tablelength = require("util").tablelength
|
||||||
|
|
||||||
local _M = {}
|
local _M = {}
|
||||||
|
|
||||||
|
@ -24,13 +24,13 @@ end
|
||||||
-- @tparam {[string], ...} map A key to node hash table.
|
-- @tparam {[string], ...} map A key to node hash table.
|
||||||
-- @treturn string,string The node and its key
|
-- @treturn string,string The node and its key
|
||||||
local function get_random_node(map)
|
local function get_random_node(map)
|
||||||
local size = util.tablelength(map)
|
local size = util_tablelength(map)
|
||||||
|
|
||||||
if size < 1 then
|
if size < 1 then
|
||||||
return nil, nil
|
return nil, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local index = math.random(1, size)
|
local index = math_random(1, size)
|
||||||
local count = 1
|
local count = 1
|
||||||
|
|
||||||
for key, endpoint in pairs(map) do
|
for key, endpoint in pairs(map) do
|
||||||
|
@ -58,7 +58,6 @@ end
|
||||||
-- @tparam {[string]=number} endpoints A table with the node endpoint as a key and its weight as a value.
|
-- @tparam {[string]=number} endpoints A table with the node endpoint as a key and its weight as a value.
|
||||||
-- @tparam[opt] string hash_salt A optional hash salt that will be used to obfuscate the hash key.
|
-- @tparam[opt] string hash_salt A optional hash salt that will be used to obfuscate the hash key.
|
||||||
function _M.new(self, endpoints, hash_salt)
|
function _M.new(self, endpoints, hash_salt)
|
||||||
|
|
||||||
if hash_salt == nil then
|
if hash_salt == nil then
|
||||||
hash_salt = ''
|
hash_salt = ''
|
||||||
end
|
end
|
||||||
|
@ -103,7 +102,7 @@ function _M.random_except(self, ignore_nodes)
|
||||||
local valid_nodes = {}
|
local valid_nodes = {}
|
||||||
|
|
||||||
-- avoid generating the map if no ignores where provided
|
-- avoid generating the map if no ignores where provided
|
||||||
if ignore_nodes == nil or util.tablelength(ignore_nodes) == 0 then
|
if ignore_nodes == nil or util_tablelength(ignore_nodes) == 0 then
|
||||||
return get_random_node(self.map)
|
return get_random_node(self.map)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue