Skip to content

Commit

Permalink
ratelimiter impl
Browse files Browse the repository at this point in the history
  • Loading branch information
NatureFreshMilk committed Sep 16, 2019
0 parents commit 87ee67c
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions api.lua
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions chatcommands.lua
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local MP = minetest.get_modpath("mesecons_ratelimiter")

mesecons_ratelimiter = {
delay_time = 750000
}

dofile(MP.."/chatcommands.lua")
dofile(MP.."/limiter.lua")
dofile(MP.."/api.lua")
dofile(MP.."/register.lua")


print("[OK] mesecons_ratelimiter loaded")
47 changes: 47 additions & 0 deletions limiter.lua
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions mod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = mesecons_ratelimiter
depends = mesecons
optional_depends = pipeworks, monitoring, mesecons_lightstone, mesecons_pistons
28 changes: 28 additions & 0 deletions register.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 87ee67c

Please sign in to comment.