refactor lua balancer and fix ipv6 issue
This commit is contained in:
parent
082325b6f5
commit
4b07e73e5d
7 changed files with 16 additions and 29 deletions
|
@ -135,18 +135,17 @@ function _M.balance()
|
|||
return
|
||||
end
|
||||
|
||||
local host, port = balancer:balance()
|
||||
if not (host and port) then
|
||||
ngx.log(ngx.WARN,
|
||||
string.format("host or port is missing, balancer: %s, host: %s, port: %s", balancer.name, host, port))
|
||||
local peer = balancer:balance()
|
||||
if not peer then
|
||||
ngx.log(ngx.WARN, "no peer was returned, balancer: " .. balancer.name)
|
||||
return
|
||||
end
|
||||
|
||||
ngx_balancer.set_more_tries(1)
|
||||
|
||||
local ok, err = ngx_balancer.set_current_peer(host, port)
|
||||
local ok, err = ngx_balancer.set_current_peer(peer)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "error while setting current upstream peer to " .. tostring(err))
|
||||
ngx.log(ngx.ERR, "error while setting current upstream peer: " .. tostring(err))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
local balancer_resty = require("balancer.resty")
|
||||
local resty_chash = require("resty.chash")
|
||||
local util = require("util")
|
||||
local split = require("util.split")
|
||||
|
||||
local _M = balancer_resty:new({ factory = resty_chash, name = "chash" })
|
||||
|
||||
|
@ -15,8 +14,7 @@ end
|
|||
|
||||
function _M.balance(self)
|
||||
local key = util.lua_ngx_var(self.hash_by)
|
||||
local endpoint_string = self.instance:find(key)
|
||||
return split.split_pair(endpoint_string, ":")
|
||||
return self.instance:find(key)
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
@ -128,7 +128,8 @@ function _M.balance(self)
|
|||
endpoint = pick_and_score(peer_copy, k)
|
||||
end
|
||||
|
||||
return endpoint.address, endpoint.port
|
||||
-- TODO(elvinefendi) move this processing to _M.sync
|
||||
return endpoint.address .. ":" .. endpoint.port
|
||||
end
|
||||
|
||||
function _M.after_balance(_)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
local balancer_resty = require("balancer.resty")
|
||||
local resty_roundrobin = require("resty.roundrobin")
|
||||
local util = require("util")
|
||||
local split = require("util.split")
|
||||
|
||||
local _M = balancer_resty:new({ factory = resty_roundrobin, name = "round_robin" })
|
||||
|
||||
|
@ -14,8 +13,7 @@ function _M.new(self, backend)
|
|||
end
|
||||
|
||||
function _M.balance(self)
|
||||
local endpoint_string = self.instance:find()
|
||||
return split.split_pair(endpoint_string, ":")
|
||||
return self.instance:find()
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
local balancer_resty = require("balancer.resty")
|
||||
local resty_chash = require("resty.chash")
|
||||
local util = require("util")
|
||||
local split = require("util.split")
|
||||
local ck = require("resty.cookie")
|
||||
|
||||
local _M = balancer_resty:new({ factory = resty_chash, name = "sticky" })
|
||||
|
@ -56,7 +55,7 @@ local function pick_random(instance)
|
|||
return instance:next(index)
|
||||
end
|
||||
|
||||
local function sticky_endpoint_string(self)
|
||||
function _M.balance(self)
|
||||
local cookie, err = ck:new()
|
||||
if not cookie then
|
||||
ngx.log(ngx.ERR, err)
|
||||
|
@ -73,9 +72,4 @@ local function sticky_endpoint_string(self)
|
|||
return self.instance:find(key)
|
||||
end
|
||||
|
||||
function _M.balance(self)
|
||||
local endpoint_string = sticky_endpoint_string(self)
|
||||
return split.split_pair(endpoint_string, ":")
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
@ -21,9 +21,8 @@ describe("Balancer chash", function()
|
|||
}
|
||||
local instance = balancer_chash:new(backend)
|
||||
|
||||
local host, port = instance:balance()
|
||||
assert.equal("10.184.7.40", host)
|
||||
assert.equal("8080", port)
|
||||
local peer = instance:balance()
|
||||
assert.equal("10.184.7.40:8080", peer)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
|
|
@ -11,9 +11,8 @@ describe("Balancer ewma", function()
|
|||
}
|
||||
local instance = balancer_ewma:new(backend)
|
||||
|
||||
local host, port = instance:balance()
|
||||
assert.equal("10.184.7.40", host)
|
||||
assert.equal("8080", port)
|
||||
local peer = instance:balance()
|
||||
assert.equal("10.184.7.40:8080", peer)
|
||||
end)
|
||||
|
||||
it("picks the endpoint with lowest score when there two of them", function()
|
||||
|
@ -32,9 +31,8 @@ describe("Balancer ewma", function()
|
|||
ngx.shared.balancer_ewma_last_touched_at.get = function(self, key) return t end
|
||||
|
||||
|
||||
local host, port = instance:balance()
|
||||
assert.equal("10.184.97.100", host)
|
||||
assert.equal("8080", port)
|
||||
local peer = instance:balance()
|
||||
assert.equal("10.184.97.100:8080", peer)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
|
Loading…
Reference in a new issue