Fixed review findings.

This commit is contained in:
Alexander Maret-Huskinson 2019-09-24 10:46:02 +02:00
parent 880b3dc5f1
commit f1839ddb42
4 changed files with 13 additions and 59 deletions

View file

@ -1,5 +1,4 @@
local balancer_resty = require("balancer.resty")
local util = require("util")
local ck = require("resty.cookie")
local ngx_balancer = require("ngx.balancer")
local split = require("util.split")
@ -87,7 +86,6 @@ local function get_failed_upstreams()
end
local function should_set_cookie(self)
if self.cookie_session_affinity.locations and ngx.var.host then
local locs = self.cookie_session_affinity.locations[ngx.var.host]
if locs == nil then
@ -115,7 +113,7 @@ end
function _M.balance(self)
local upstream_from_cookie
local key = self:get_routing_key()
local key = self:get_cookie()
if key then
upstream_from_cookie = self.instance:find(key)
end
@ -134,7 +132,7 @@ function _M.balance(self)
if not new_upstream then
ngx.log(ngx.WARN, string.format("failed to get new upstream; using upstream %s", new_upstream))
elseif should_set_cookie(self) then
self:set_routing_key(key)
self:set_cookie(key)
end
return new_upstream
@ -144,18 +142,6 @@ function _M.sync(self, backend)
-- reload balancer nodes
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.alternative_backends = backend.alternativeBackends
self.cookie_session_affinity = backend.sessionAffinityConfig.cookieSessionAffinity

View file

@ -5,9 +5,9 @@
-- pods.
--
local balancer_sticky = require("balancer.sticky")
local math = require("math")
local math_random = require("math").random
local resty_chash = require("resty.chash")
local util = require("util")
local util_get_nodes = require("util").get_nodes
local _M = balancer_sticky:new()
@ -18,7 +18,7 @@ local _M = balancer_sticky:new()
local MAX_UPSTREAM_CHECKS_COUNT = 20
function _M.new(self, backend)
local nodes = util.get_nodes(backend.endpoints)
local nodes = util_get_nodes(backend.endpoints)
local o = {
name = "sticky_balanced",
@ -33,17 +33,9 @@ function _M.new(self, backend)
return o
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)
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)
if not failed_upstreams[new_upstream] then

View file

@ -4,13 +4,13 @@
-- be rebalanced.
--
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 _M = balancer_sticky:new()
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 o = {
@ -26,29 +26,6 @@ function _M.new(self, backend)
return o
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)
return self.instance:random_except(failed_upstreams)
end