Support the combination of nginx variables and text value for annotation upstream-hash-by.

This commit is contained in:
agile6v 2020-05-19 11:51:30 +08:00
parent b18ea267a8
commit c035a144f8
6 changed files with 95 additions and 22 deletions

View file

@ -3,6 +3,8 @@
local resty_chash = require("resty.chash")
local util = require("util")
local ngx_log = ngx.log
local ngx_ERR = ngx.ERR
local _M = { name = "chashsubset" }
@ -44,10 +46,14 @@ end
function _M.new(self, backend)
local subset_map, subsets = build_subset_map(backend)
local complex_val, err = util.parse_complex_value(backend["upstreamHashByConfig"]["upstream-hash-by"])
if err ~= nil then
ngx_log(ngx_ERR, "could not parse the value of the upstream-hash-by: ", err)
end
local o = {
instance = resty_chash:new(subset_map),
hash_by = backend["upstreamHashByConfig"]["upstream-hash-by"],
hash_by = complex_val,
subsets = subsets,
current_endpoints = backend.endpoints
}
@ -57,7 +63,7 @@ function _M.new(self, backend)
end
function _M.balance(self)
local key = util.lua_ngx_var(self.hash_by)
local key = util.generate_var_value(self.hash_by)
local subset_id = self.instance:find(key)
local endpoints = self.subsets[subset_id]
local endpoint = endpoints[math.random(#endpoints)]