mesecons ratelimiter
This commit is contained in:
parent
6d064607f1
commit
0d15c093a0
19
mesecons_ratelimiter/.luacheckrc
Normal file
19
mesecons_ratelimiter/.luacheckrc
Normal file
@ -0,0 +1,19 @@
|
||||
allow_defined_top = true
|
||||
|
||||
globals = {
|
||||
"mesecon",
|
||||
"minetest"
|
||||
}
|
||||
|
||||
read_globals = {
|
||||
-- Stdlib
|
||||
string = {fields = {"split"}},
|
||||
table = {fields = {"copy", "getn"}},
|
||||
|
||||
-- Minetest
|
||||
"vector", "ItemStack",
|
||||
"dump",
|
||||
|
||||
-- optional deps
|
||||
"monitoring"
|
||||
}
|
43
mesecons_ratelimiter/api.lua
Normal file
43
mesecons_ratelimiter/api.lua
Normal file
@ -0,0 +1,43 @@
|
||||
|
||||
local metric_allowed = { inc = function() end }
|
||||
local metric_blocked = { inc = function() end }
|
||||
|
||||
if minetest.get_modpath("monitoring") then
|
||||
metric_allowed = monitoring.counter(
|
||||
"mesecons_ratelimiter_allowed",
|
||||
"allowed action_on calls"
|
||||
)
|
||||
|
||||
metric_blocked = monitoring.counter(
|
||||
"mesecons_ratelimiter_blocked",
|
||||
"blocked action_on calls"
|
||||
)
|
||||
end
|
||||
|
||||
mesecons_ratelimiter.register_action_on = function(name)
|
||||
|
||||
local nodedef = minetest.registered_nodes[name]
|
||||
|
||||
local old_action_on = nodedef and
|
||||
nodedef.mesecons and
|
||||
nodedef.mesecons.effector and
|
||||
nodedef.mesecons.effector.action_on
|
||||
|
||||
if not old_action_on then
|
||||
minetest.log(
|
||||
"warning",
|
||||
"[mesecons_ratelimiter] node not found: " .. name
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
nodedef.mesecons.effector.action_on = function(pos, node, rulename)
|
||||
if mesecons_ratelimiter.can_call(pos) then
|
||||
old_action_on(pos, node, rulename)
|
||||
metric_allowed.inc()
|
||||
else
|
||||
metric_blocked.inc()
|
||||
end
|
||||
end
|
||||
|
||||
end
|
15
mesecons_ratelimiter/chatcommands.lua
Normal file
15
mesecons_ratelimiter/chatcommands.lua
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
minetest.register_chatcommand("mesecons_ratelimiter_delay", {
|
||||
description = "sets or gets the ratelimiter delay (in microseconds)",
|
||||
privs = {server=true},
|
||||
func = function(_, params)
|
||||
local setvalue = tonumber(params)
|
||||
|
||||
if setvalue then
|
||||
mesecons_ratelimiter.delay_time = setvalue
|
||||
end
|
||||
|
||||
return true, "Ratelimiter-delay: " .. mesecons_ratelimiter.delay_time ..
|
||||
" microseconds"
|
||||
end
|
||||
})
|
13
mesecons_ratelimiter/init.lua
Normal file
13
mesecons_ratelimiter/init.lua
Normal file
@ -0,0 +1,13 @@
|
||||
local MP = minetest.get_modpath("mesecons_ratelimiter")
|
||||
|
||||
mesecons_ratelimiter = {
|
||||
delay_time = 75000
|
||||
}
|
||||
|
||||
dofile(MP.."/chatcommands.lua")
|
||||
dofile(MP.."/limiter.lua")
|
||||
dofile(MP.."/api.lua")
|
||||
dofile(MP.."/register.lua")
|
||||
|
||||
|
||||
print("[OK] mesecons_ratelimiter loaded")
|
47
mesecons_ratelimiter/limiter.lua
Normal file
47
mesecons_ratelimiter/limiter.lua
Normal file
@ -0,0 +1,47 @@
|
||||
|
||||
local metric_cache_size = { set = function() end }
|
||||
local cache_size = 0
|
||||
|
||||
if minetest.get_modpath("monitoring") then
|
||||
metric_cache_size = monitoring.gauge(
|
||||
"mesecons_ratelimiter_cache_size",
|
||||
"cache size"
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
local cache = {} -- hash -> time_in_micros
|
||||
|
||||
mesecons_ratelimiter.can_call = function(pos)
|
||||
local hash = minetest.hash_node_position(pos)
|
||||
local last_call = cache[hash]
|
||||
|
||||
if not last_call then
|
||||
last_call = 0
|
||||
cache_size = cache_size + 1
|
||||
metric_cache_size.set(cache_size)
|
||||
end
|
||||
|
||||
local now = minetest.get_us_time()
|
||||
local delta = now - last_call
|
||||
|
||||
if delta > mesecons_ratelimiter.delay_time then
|
||||
cache[hash] = now
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
-- cache flush timer
|
||||
local timer = 0
|
||||
minetest.register_globalstep(function(dtime)
|
||||
timer = timer + dtime
|
||||
if timer < 60 then return end
|
||||
timer=0
|
||||
|
||||
cache = {}
|
||||
cache_size = 0
|
||||
metric_cache_size.set(cache_size)
|
||||
end)
|
3
mesecons_ratelimiter/mod.conf
Normal file
3
mesecons_ratelimiter/mod.conf
Normal file
@ -0,0 +1,3 @@
|
||||
name = mesecons_ratelimiter
|
||||
depends = mesecons
|
||||
optional_depends = pipeworks, monitoring, mesecons_lightstone, mesecons_pistons
|
28
mesecons_ratelimiter/register.lua
Normal file
28
mesecons_ratelimiter/register.lua
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
if minetest.get_modpath("pipeworks") then
|
||||
mesecons_ratelimiter.register_action_on("pipeworks:filter")
|
||||
mesecons_ratelimiter.register_action_on("pipeworks:mese_filter")
|
||||
mesecons_ratelimiter.register_action_on("pipeworks:dispenser_off")
|
||||
mesecons_ratelimiter.register_action_on("pipeworks:deployer_off")
|
||||
mesecons_ratelimiter.register_action_on("pipeworks:nodebreaker_off")
|
||||
end
|
||||
|
||||
if minetest.get_modpath("mesecons_lightstone") then
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_red_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_green_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_blue_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_gray_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_darkgray_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_yellow_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_orange_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_white_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_pink_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_magent_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_cyan_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_lightstone:lightstone_violet_off")
|
||||
end
|
||||
|
||||
if minetest.get_modpath("mesecons_pistons") then
|
||||
mesecons_ratelimiter.register_action_on("mesecons_pistons:piston_normal_off")
|
||||
mesecons_ratelimiter.register_action_on("mesecons_pistons:piston_sticky_off")
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user