-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
NatureFreshMilk
committed
Sep 16, 2019
0 parents
commit 87ee67c
Showing
7 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |