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

71 lines
1.6 KiB
Lua
Raw Normal View History

2023-06-06 09:23:53 +02:00
local sleep_secs=15
local args = table.pack(...)
if args[1] ~= nil then sleep_secs = args[1] end
function getmax(t)
2022-10-23 16:37:53 +02:00
local tmpvalue = 100000
local svname
local value
for k,v in pairs(t) do
if v < tmpvalue then
svname = k
value = v
end
2023-06-06 09:23:53 +02:00
2022-10-23 16:37:53 +02:00
tmpvalue = v
end
2023-06-06 09:23:53 +02:00
2022-10-23 16:37:53 +02:00
return svname, value
end
2023-06-06 09:23:53 +02:00
function arrange_backends()
local try = 0
local lastconns = 0
2022-10-23 16:37:53 +02:00
while true do
2023-06-06 09:23:53 +02:00
if lastconns == core.get_info()["CumConns"] then
try = try+1
end
2022-10-23 16:37:53 +02:00
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
2023-06-06 09:23:53 +02:00
if server:get_stats()["check_status"] ~= nil
and server:get_stats()["status"] == "UP"
and server:get_stats()["bck"] == 0 then
2022-11-08 11:04:39 +01:00
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
2023-06-06 09:23:53 +02:00
lastconns = core.get_info()["CumConns"]
if try == 10 then
2024-05-25 13:54:44 +02:00
local msg = string.format("Worker with pid %s exited for arrange_backends task", core.get_info()["Pid"])
2023-06-06 09:23:53 +02:00
print(msg)
core.done(msg)
end
core.sleep(sleep_secs)
2022-10-23 16:37:53 +02:00
end
end
core.register_task(arrange_backends)