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

46 lines
1.0 KiB
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()
while true do
for _, backend in pairs(core.backends) do
2022-11-08 11:04:39 +01:00
local results = {}
2022-10-23 16:37:53 +02:00
for n,server in pairs(backend.servers) do
2022-11-08 11:04:39 +01:00
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
2022-10-23 16:37:53 +02:00
end
end
2022-11-08 11:04:39 +01:00
2022-10-23 16:37:53 +02:00
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)