paulbsd-salt/states/haproxy/scripts/weight_by_latency.lua

44 lines
985 B
Lua
Raw Normal View History

2022-10-23 16:37:53 +02:00
local 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
local function arrange_backends()
local results = {}
while true do
for _, backend in pairs(core.backends) do
results = {}
for n,server in pairs(backend.servers) do
if server:get_stats()["check_status"] == "L4OK" then
local svname = server:get_stats()["svname"]
local latency = server:get_stats()["check_duration"]
results[svname] = latency
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
core.msleep(1000)
end
end
core.register_task(arrange_backends)