paulbsd-salt/states/haproxy/scripts/weight_by_latency.lua
2022-11-08 11:04:39 +01:00

46 lines
1.0 KiB
Lua

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()
while true do
for _, backend in pairs(core.backends) do
local results = {}
for n,server in pairs(backend.servers) do
if server:get_stats()["check_status"] ~= nil 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
core.msleep(1000)
end
end
core.register_task(arrange_backends)