paulbsd-salt/states/haproxy/scripts/weight.lua
2023-06-06 09:23:53 +02:00

71 lines
1.6 KiB
Lua

local sleep_secs=15
local args = table.pack(...)
if args[1] ~= nil then sleep_secs = args[1] end
function getmax(t)
local tmpvalue = 100000
local svname
local value
for k,v in pairs(t) do
if v < tmpvalue then
svname = k
value = v
end
tmpvalue = v
end
return svname, value
end
function arrange_backends()
local try = 0
local lastconns = 0
while true do
if lastconns == core.get_info()["CumConns"] then
try = try+1
end
for _, backend in pairs(core.backends) do
local results = {}
for n,server in pairs(backend.servers) do
if server:get_stats()["check_status"] ~= nil
and server:get_stats()["status"] == "UP"
and server:get_stats()["bck"] == 0 then
if string.find(server:get_stats()["check_status"],"OK") ~= nil then
local svname = server:get_stats()["svname"]
local latency = server:get_stats()["check_duration"]
results[svname] = latency
end
end
end
local b,c = getmax(results)
if b ~= nil then
for n,server in pairs(backend.servers) do
if b == server.name then
server:set_weight("10")
else
server:set_weight("1")
end
end
end
end
lastconns = core.get_info()["CumConns"]
if try == 10 then
local msg = "Worker with pid ".. core.get_info()["Pid"] .. " exited for arrange_backends task"
print(msg)
core.done(msg)
end
core.sleep(sleep_secs)
end
end
core.register_task(arrange_backends)