fix sticky session implementation

This commit is contained in:
Elvin Efendi 2018-10-30 16:23:08 +04:00
parent 91a12ffd52
commit 9e639f9788
2 changed files with 6 additions and 10 deletions

View file

@ -50,22 +50,17 @@ local function set_cookie(self, value)
end
end
local function pick_random(instance)
local index = math.random(instance.npoints)
return instance:next(index)
end
function _M.balance(self)
local cookie, err = ck:new()
if not cookie then
ngx.log(ngx.ERR, err)
return pick_random(self.instance)
ngx.log(ngx.ERR, "error while initializing cookie: " .. tostring(err))
return
end
local key = cookie:get(self.cookie_name)
if not key then
local tmp_endpoint_string = pick_random(self.instance)
key = encrypted_endpoint_string(self, tmp_endpoint_string)
local random_str = string.format("%s.%s", ngx.now(), ngx.worker.pid())
key = encrypted_endpoint_string(self, random_str)
set_cookie(self, key)
end

View file

@ -116,7 +116,8 @@ describe("Sticky", function()
local cookie_instance = {
set = function(self, payload)
assert.equal(payload.key, test_backend.sessionAffinityConfig.cookieSessionAffinity.name)
assert.equal(payload.value, util[test_backend_hash_fn .. "_digest"](test_backend_endpoint))
local expected_len = #util[test_backend_hash_fn .. "_digest"]("anything")
assert.equal(#payload.value, expected_len)
assert.equal(payload.path, ngx.var.location_path)
assert.equal(payload.domain, nil)
assert.equal(payload.httponly, true)