diff --git a/rootfs/etc/nginx/lua/balancer/sticky.lua b/rootfs/etc/nginx/lua/balancer/sticky.lua index 4d4684146..5eb550ba5 100644 --- a/rootfs/etc/nginx/lua/balancer/sticky.lua +++ b/rootfs/etc/nginx/lua/balancer/sticky.lua @@ -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 diff --git a/rootfs/etc/nginx/lua/test/balancer/sticky_test.lua b/rootfs/etc/nginx/lua/test/balancer/sticky_test.lua index d5c402236..9c022d9c1 100644 --- a/rootfs/etc/nginx/lua/test/balancer/sticky_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer/sticky_test.lua @@ -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)