2019-08-30 09:40:29 +00:00
|
|
|
-- An affinity mode which makes sure a session is always routed to the same endpoint.
|
|
|
|
-- The advantage of this mode is that a user will never lose his session.
|
|
|
|
-- The drawback of this mode is that when scaling up a deployment, sessions will not
|
|
|
|
-- be rebalanced.
|
|
|
|
--
|
2019-08-30 16:07:24 +00:00
|
|
|
local balancer_sticky = require("balancer.sticky")
|
2019-09-24 08:46:02 +00:00
|
|
|
local util_get_nodes = require("util").get_nodes
|
2019-08-30 09:40:29 +00:00
|
|
|
local util_nodemap = require("util.nodemap")
|
2020-06-06 15:07:06 +00:00
|
|
|
local setmetatable = setmetatable
|
2019-08-30 09:40:29 +00:00
|
|
|
|
2019-08-30 16:07:24 +00:00
|
|
|
local _M = balancer_sticky:new()
|
2019-08-30 09:40:29 +00:00
|
|
|
|
2019-08-30 16:07:24 +00:00
|
|
|
function _M.new(self, backend)
|
2019-09-24 08:46:02 +00:00
|
|
|
local nodes = util_get_nodes(backend.endpoints)
|
2019-08-30 16:07:24 +00:00
|
|
|
local hash_salt = backend["name"]
|
|
|
|
|
|
|
|
local o = {
|
|
|
|
name = "sticky_persistent",
|
|
|
|
instance = util_nodemap:new(nodes, hash_salt)
|
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
2019-08-30 17:08:03 +00:00
|
|
|
|
2019-08-30 16:07:24 +00:00
|
|
|
balancer_sticky.sync(o, backend)
|
|
|
|
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
function _M.pick_new_upstream(self, failed_upstreams)
|
2019-08-30 09:40:29 +00:00
|
|
|
return self.instance:random_except(failed_upstreams)
|
|
|
|
end
|
|
|
|
|
|
|
|
return _M
|