2018-05-18 21:36:43 +00:00
|
|
|
local balancer_resty = require("balancer.resty")
|
|
|
|
local resty_chash = require("resty.chash")
|
|
|
|
local util = require("util")
|
2020-05-19 03:51:30 +00:00
|
|
|
local ngx_log = ngx.log
|
|
|
|
local ngx_ERR = ngx.ERR
|
2020-06-06 15:07:06 +00:00
|
|
|
local setmetatable = setmetatable
|
2018-05-18 21:36:43 +00:00
|
|
|
|
|
|
|
local _M = balancer_resty:new({ factory = resty_chash, name = "chash" })
|
|
|
|
|
|
|
|
function _M.new(self, backend)
|
|
|
|
local nodes = util.get_nodes(backend.endpoints)
|
2020-06-06 15:07:06 +00:00
|
|
|
local complex_val, err =
|
|
|
|
util.parse_complex_value(backend["upstreamHashByConfig"]["upstream-hash-by"])
|
2020-05-19 03:51:30 +00:00
|
|
|
if err ~= nil then
|
|
|
|
ngx_log(ngx_ERR, "could not parse the value of the upstream-hash-by: ", err)
|
|
|
|
end
|
|
|
|
|
2018-09-18 18:05:32 +00:00
|
|
|
local o = {
|
|
|
|
instance = self.factory:new(nodes),
|
2020-05-19 03:51:30 +00:00
|
|
|
hash_by = complex_val,
|
2018-11-13 11:44:57 +00:00
|
|
|
traffic_shaping_policy = backend.trafficShapingPolicy,
|
|
|
|
alternative_backends = backend.alternativeBackends,
|
2018-09-18 18:05:32 +00:00
|
|
|
}
|
2018-05-18 21:36:43 +00:00
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
function _M.balance(self)
|
2020-05-19 03:51:30 +00:00
|
|
|
local key = util.generate_var_value(self.hash_by)
|
2018-05-30 21:14:28 +00:00
|
|
|
return self.instance:find(key)
|
2018-05-18 21:36:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return _M
|