diff options
author | Thomas Rudin <thomas@rudin.io> | 2019-10-09 21:24:00 +0200 |
---|---|---|
committer | Thomas Rudin <thomas@rudin.io> | 2019-10-09 21:24:00 +0200 |
commit | 3582abf84cf9dd3a1e4a227cb48db2e6bdbb811c (patch) | |
tree | 03fd0aa35cb3742c7bf607ed7fffacf38b1efee0 | |
parent | f1ffd844c0ae52823bef0dbbbc51cce35217c898 (diff) | |
download | mesecons_debug-3582abf84cf9dd3a1e4a227cb48db2e6bdbb811c.tar.gz mesecons_debug-3582abf84cf9dd3a1e4a227cb48db2e6bdbb811c.zip |
global circuit breaker concept
Diffstat (limited to '')
-rw-r--r-- | overrides.lua | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/overrides.lua b/overrides.lua index 4bf241e..3af4d22 100644 --- a/overrides.lua +++ b/overrides.lua @@ -2,16 +2,66 @@ -- enable/disable mesecons entirely local enabled = true +local reenable_seconds = 0 +local cumulative_microseconds = 0 +local hit_count = 0 -- execute() local old_execute = mesecon.queue.execute mesecon.queue.execute = function(...) if enabled then - old_execute(...) + local t0 = minetest.get_us_time() + old_execute(...) + local t1 = minetest.get_us_time() + local micros = t1 - t0 + cumulative_microseconds = cumulative_microseconds + micros end end + +local timer = 0 +minetest.register_globalstep(function(dtime) + timer = timer + dtime + if timer < 1 then return end + timer=0 + + local max_micros = tonumber(minetest.settings:get("mesecons_debug.max_micros")) or 100000 + local max_hit_count = tonumber(minetest.settings:get("mesecons_debug.max_hit_count")) or 5 + + if cumulative_microseconds > max_micros then + -- heat up + hit_count = hit_count + 1 + else + -- cooldown + max_hit_count = max_hit_count - 0.1 + end + + cumulative_microseconds = 0 + + + if hit_count > max_hit_count then + hit_count = 0 + enabled = false + + minetest.chat_send_all("[circuit-breaker] mesecons are disabled for a minute due to abuse, " .. + "please fix/optimize your circuits!") + + minetest.log("warning", "[mesecons_debug] circuit-breaker triggered -> disabled for 60 seconds") + + reenable_seconds = 60 + end + + if reenable_seconds > 0 then + reenable_seconds = reenable_seconds - 1 + if reenable_seconds < 1 then + -- re-enable again + enabled = true + end + end + +end) + -- add_action() local old_add_action = mesecon.queue.add_action mesecon.queue.add_action = function(...) |