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
21
rootfs/etc/nginx/lua/balancer/chash.lua
Normal file
21
rootfs/etc/nginx/lua/balancer/chash.lua
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
local balancer_resty = require("balancer.resty")
|
||||
local resty_chash = require("resty.chash")
|
||||
local util = require("util")
|
||||
|
||||
local _M = balancer_resty:new({ factory = resty_chash, name = "chash" })
|
||||
|
||||
function _M.new(self, backend)
|
||||
local nodes = util.get_nodes(backend.endpoints)
|
||||
local o = { instance = self.factory:new(nodes), hash_by = backend["upstream-hash-by"] }
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
function _M.balance(self)
|
||||
local key = util.lua_ngx_var(self.hash_by)
|
||||
local endpoint_string = self.instance:find(key)
|
||||
return util.split_pair(endpoint_string, ":")
|
||||
end
|
||||
|
||||
return _M
|
||||
Loading…
Add table
Add a link
Reference in a new issue