Merge pull request #4365 from ElvinEfendi/fix-balancer-bug
memoize balancer for a request
This commit is contained in:
commit
daf4a150ae
2 changed files with 46 additions and 1 deletions
|
@ -190,6 +190,10 @@ local function route_to_alternative_balancer(balancer)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_balancer()
|
local function get_balancer()
|
||||||
|
if ngx.ctx.balancer then
|
||||||
|
return ngx.ctx.balancer
|
||||||
|
end
|
||||||
|
|
||||||
local backend_name = ngx.var.proxy_upstream_name
|
local backend_name = ngx.var.proxy_upstream_name
|
||||||
|
|
||||||
local balancer = balancers[backend_name]
|
local balancer = balancers[backend_name]
|
||||||
|
@ -201,9 +205,11 @@ local function get_balancer()
|
||||||
local alternative_backend_name = balancer.alternative_backends[1]
|
local alternative_backend_name = balancer.alternative_backends[1]
|
||||||
ngx.var.proxy_alternative_upstream_name = alternative_backend_name
|
ngx.var.proxy_alternative_upstream_name = alternative_backend_name
|
||||||
|
|
||||||
return balancers[alternative_backend_name]
|
balancer = balancers[alternative_backend_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ngx.ctx.balancer = balancer
|
||||||
|
|
||||||
return balancer
|
return balancer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -260,6 +266,7 @@ if _TEST then
|
||||||
_M.get_implementation = get_implementation
|
_M.get_implementation = get_implementation
|
||||||
_M.sync_backend = sync_backend
|
_M.sync_backend = sync_backend
|
||||||
_M.route_to_alternative_balancer = route_to_alternative_balancer
|
_M.route_to_alternative_balancer = route_to_alternative_balancer
|
||||||
|
_M.get_balancer = get_balancer
|
||||||
end
|
end
|
||||||
|
|
||||||
return _M
|
return _M
|
||||||
|
|
|
@ -86,6 +86,44 @@ describe("Balancer", function()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("get_balancer()", function()
|
||||||
|
it("always returns the same balancer for given request context", function()
|
||||||
|
local backend = {
|
||||||
|
name = "my-dummy-app-6", ["load-balance"] = "ewma",
|
||||||
|
alternativeBackends = { "my-dummy-canary-app-6" },
|
||||||
|
endpoints = { { address = "10.184.7.40", port = "8080", maxFails = 0, failTimeout = 0 } },
|
||||||
|
trafficShapingPolicy = {
|
||||||
|
weight = 0,
|
||||||
|
header = "",
|
||||||
|
headerValue = "",
|
||||||
|
cookie = ""
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local canary_backend = {
|
||||||
|
name = "my-dummy-canary-app-6", ["load-balance"] = "ewma",
|
||||||
|
alternativeBackends = { "my-dummy-canary-app-6" },
|
||||||
|
endpoints = { { address = "11.184.7.40", port = "8080", maxFails = 0, failTimeout = 0 } },
|
||||||
|
trafficShapingPolicy = {
|
||||||
|
weight = 5,
|
||||||
|
header = "",
|
||||||
|
headerValue = "",
|
||||||
|
cookie = ""
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
balancer.sync_backend(backend)
|
||||||
|
balancer.sync_backend(canary_backend)
|
||||||
|
|
||||||
|
mock_ngx({ var = { proxy_upstream_name = backend.name } })
|
||||||
|
|
||||||
|
local expected = balancer.get_balancer()
|
||||||
|
|
||||||
|
for i = 1,50,1 do
|
||||||
|
assert.are.same(expected, balancer.get_balancer())
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe("route_to_alternative_balancer()", function()
|
describe("route_to_alternative_balancer()", function()
|
||||||
local backend, _balancer
|
local backend, _balancer
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue