Class: Rack::OOMKiller

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/oom_killer.rb,
lib/rack/oom_killer/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ OOMKiller

Returns a new instance of OOMKiller.



3
4
5
6
7
8
# File 'lib/rack/oom_killer.rb', line 3

def initialize(app, options={})
  default_options = {
    :max_mem => 350,
  }
  @app, @options = app, default_options.merge(options)
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rack/oom_killer.rb', line 10

def call(env)
  ret = @app.call(env)
  rss_mem = `ps -o rss= -p #{$$}`.to_i
  if rss_mem > @options[:max_mem] * 1024
    puts "Memory usage excedes #{@options[:max_mem]}mb limit, exiting"
    Thread.new do
      `kill -INT #{$$}`
      sleep 10
      `kill -KILL #{$$}`
    end
  end
  ret
end