Module: Unicorn::WorkerKiller::Oom

Defined in:
lib/unicorn/worker-killer-2/oom.rb

Defined Under Namespace

Modules: MonkeyPatch

Class Method Summary collapse

Class Method Details

.monkey_patch(opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/unicorn/worker-killer-2/oom.rb', line 29

def self.monkey_patch(opts = {})
  min = opts[:memory_limit_min] || (1024**3)
  max = opts[:memory_limit_max] || 2 * (1024**3)
  check_cycle = opts[:check_cycle] || 16
  verbose = opts[:verbose] || false

  ObjectSpace.each_object(HttpServer) do |s|
    s.extend(MonkeyPatch)

    s.instance_variable_set(:@_worker_process_start, WorkerKiller.now)

    s.instance_variable_set(:@_worker_memory_limit_min, min)
    s.instance_variable_set(:@_worker_memory_limit_max, max)
    s.instance_variable_set(:@_worker_check_cycle, check_cycle)
    s.instance_variable_set(:@_verbose, verbose)

    r = WorkerKiller.randomize(max - min + 1)
    s.instance_variable_set(:@_worker_memory_limit, min + r)
  end
end