paulbsd-salt/states/haproxy/scripts/weight.lua
Paul e9b2c207f9
All checks were successful
continuous-integration/drone/push Build is passing
updated haproxy state
2024-05-25 13:54:44 +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 = string.format("Worker with pid %s exited for arrange_backends task", core.get_info()["Pid"])
print(msg)
core.done(msg)
end
core.sleep(sleep_secs)
end
end
core.register_task(arrange_backends)