refactor balancer into more testable and extensible interface
This commit is contained in:
parent
1b5db4b3b0
commit
e9dc275b81
20 changed files with 368 additions and 467 deletions
|
|
@ -14,7 +14,7 @@ local PICK_SET_SIZE = 2
|
|||
|
||||
local ewma_lock = resty_lock:new("locks", {timeout = 0, exptime = 0.1})
|
||||
|
||||
local _M = {}
|
||||
local _M = { name = "ewma" }
|
||||
|
||||
local function lock(upstream)
|
||||
local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
|
||||
|
|
@ -117,17 +117,18 @@ local function pick_and_score(peers, k)
|
|||
return peers[lowest_score_index]
|
||||
end
|
||||
|
||||
function _M.balance(backend)
|
||||
local peers = backend.endpoints
|
||||
function _M.balance(self)
|
||||
local peers = self.peers
|
||||
if #peers == 1 then
|
||||
return peers[1]
|
||||
end
|
||||
local k = (#peers < PICK_SET_SIZE) and #peers or PICK_SET_SIZE
|
||||
local peer_copy = util.deepcopy(peers)
|
||||
return pick_and_score(peer_copy, k)
|
||||
local endpoint = pick_and_score(peer_copy, k)
|
||||
return endpoint.address, endpoint.port
|
||||
end
|
||||
|
||||
function _M.after_balance()
|
||||
function _M.after_balance(_)
|
||||
local response_time = tonumber(util.get_first_value(ngx.var.upstream_response_time)) or 0
|
||||
local connect_time = tonumber(util.get_first_value(ngx.var.upstream_connect_time)) or 0
|
||||
local rtt = connect_time + response_time
|
||||
|
|
@ -139,10 +140,21 @@ function _M.after_balance()
|
|||
get_or_update_ewma(upstream, rtt, true)
|
||||
end
|
||||
|
||||
function _M.sync(_)
|
||||
function _M.sync(self, backend)
|
||||
local changed = not util.deep_compare(self.peers, backend.endpoints)
|
||||
if not changed then
|
||||
return
|
||||
end
|
||||
-- TODO: Reset state of EWMA per backend
|
||||
ngx.shared.balancer_ewma:flush_all()
|
||||
ngx.shared.balancer_ewma_last_touched_at:flush_all()
|
||||
end
|
||||
|
||||
function _M.new(self, backend)
|
||||
local o = { peers = backend.endpoints }
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue