Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoize balancer for a request #4365

Merged
merged 2 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion rootfs/etc/nginx/lua/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ local function route_to_alternative_balancer(balancer)
end

local function get_balancer()
if ngx.ctx.balancer then
return ngx.ctx.balancer
end

local backend_name = ngx.var.proxy_upstream_name

local balancer = balancers[backend_name]
Expand All @@ -201,9 +205,11 @@ local function get_balancer()
local alternative_backend_name = balancer.alternative_backends[1]
ngx.var.proxy_alternative_upstream_name = alternative_backend_name

return balancers[alternative_backend_name]
balancer = balancers[alternative_backend_name]
end

ngx.ctx.balancer = balancer

return balancer
end

Expand Down Expand Up @@ -260,6 +266,7 @@ if _TEST then
_M.get_implementation = get_implementation
_M.sync_backend = sync_backend
_M.route_to_alternative_balancer = route_to_alternative_balancer
_M.get_balancer = get_balancer
end

return _M
38 changes: 38 additions & 0 deletions rootfs/etc/nginx/lua/test/balancer_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,44 @@ describe("Balancer", function()
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()
local backend, _balancer

Expand Down