aboutsummaryrefslogtreecommitdiff
path: root/overrides.lua
blob: 61fc2aa0257c7506dad07fd8edd9a0eaae2d2a47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

-- 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
    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
      mesecon.queue.actions = {}
    end
  end

end)

-- add_action()
local old_add_action = mesecon.queue.add_action
mesecon.queue.add_action = function(...)
  if enabled then
    old_add_action(...)
  end
end


minetest.register_chatcommand("mesecons_enable", {
  description = "enables the mesecons globlastep",
  privs = {mesecons_debug=true},
  func = function()
    -- flush actions, while we are on it
    mesecon.queue.actions = {}
    enabled = true
    return true, "mesecons enabled"
  end
})

minetest.register_chatcommand("mesecons_disable", {
  description = "disables the mesecons globlastep",
  privs = {mesecons_debug=true},
  func = function()
    enabled = false
    return true, "mesecons disabled"
  end
})