Class: Rack::Leakin

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

Constant Summary collapse

VERSION =
'0.0.1'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Leakin

Returns a new instance of Leakin.



31
32
33
# File 'lib/rack/leakin.rb', line 31

def initialize(app)
  @app = app
end

Class Attribute Details

.handlerObject



14
15
16
17
18
# File 'lib/rack/leakin.rb', line 14

def handler
  @handler ||= lambda do |env, beginning, ending|
    logger.warn "*** [Memory leak detected] #{env['REQUEST_METHOD']} #{env['REQUEST_URI']}, #{beginning}KB --> #{ending}KB"
  end
end

.loggerObject



20
21
22
23
24
25
26
27
28
# File 'lib/rack/leakin.rb', line 20

def logger
  @logger ||= begin
    if defined?(Rails)
      ::Rails.logger
    else
      ::Logger.new('rack-leaker.log')
    end
  end
end

.thresholdObject



10
11
12
# File 'lib/rack/leakin.rb', line 10

def threshold
  @threshold ||= 131072 # 128 Mb by default
end

Class Method Details

.versionObject



5
6
7
# File 'lib/rack/leakin/version.rb', line 5

def self.version
  VERSION
end

Instance Method Details

#call(env) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rack/leakin.rb', line 35

def call(env)
  beginning = Process.memory

  @app.call(env).tap do
    ending = Process.memory

    if ending - beginning > self.class.threshold
      self.class.handler.call(env, beginning, ending)
    end
  end
end