2018-05-18 17:36:43 -04:00
|
|
|
local balancer_resty = require("balancer.resty")
|
|
|
|
|
local resty_chash = require("resty.chash")
|
|
|
|
|
local util = require("util")
|
|
|
|
|
|
|
|
|
|
local _M = balancer_resty:new({ factory = resty_chash, name = "chash" })
|
|
|
|
|
|
|
|
|
|
function _M.new(self, backend)
|
|
|
|
|
local nodes = util.get_nodes(backend.endpoints)
|
2018-09-18 14:05:32 -04:00
|
|
|
local o = {
|
|
|
|
|
instance = self.factory:new(nodes),
|
2018-11-10 20:30:06 -03:00
|
|
|
hash_by = backend["upstreamHashByConfig"]["upstream-hash-by"],
|
2018-11-13 15:44:57 +04:00
|
|
|
traffic_shaping_policy = backend.trafficShapingPolicy,
|
|
|
|
|
alternative_backends = backend.alternativeBackends,
|
2018-09-18 14:05:32 -04:00
|
|
|
}
|
2018-05-18 17:36:43 -04:00
|
|
|
setmetatable(o, self)
|
|
|
|
|
self.__index = self
|
|
|
|
|
return o
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function _M.balance(self)
|
|
|
|
|
local key = util.lua_ngx_var(self.hash_by)
|
2018-05-30 17:14:28 -04:00
|
|
|
return self.instance:find(key)
|
2018-05-18 17:36:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return _M
|