Now chash key only support fetch from nginx variable. We need to be more 
flexible approach??
eg: cookie, custom header and so on.
Here is an example to achieve:
upstream json schema??
hash_on = {
            type = "string",
            default = "vars",
            enum = {
              "vars",
              "header",
              "cookie",
              "consumer"
            },
        },
        key = {
            description = "the key of chash for 
dynamic load balancing",
            type = "string"
        },


eg:
local function create_chash_hash_key(ctx, upstream)
    local key = upstream.key 
    local hash_on = upstream.hash_on
    local chash_key
    -- from nginx variable
    if hash_on == "vars" then 
        chash_key = ctx.var[key]
    elseif hash_on == "header" then 
        chash_key = ngx.req.get_headers()[key]
    elseif hash_on == "cookie" then 
        chash_key = ctx.var["cookie_" .. key]
        -- TODO chash_key doesn't exist, set-cookie
    end
    if not chash_key then 
        chash_key = ctx.var["remote_addr"]
    end
    return chash_key
end


hash_on type desc:
1 vars: fetchs nginx variable
2 header: custom headers or standard http headers
3 cookie: fetch chash key from cookie. It can be used to sticky session
4 consumer: hash by consumer_id
If the specified chash key is not present, use default key: remote_addr(it can 
also provide default settings).


everybody have a better idea?

github issue:
https://github.com/apache/incubator-apisix/issues/992

Reply via email to