Class: Rack::MP::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/mp/middleware.rb

Overview

Small middleware to count Ruby objects before and after a request. Puts the values in the X-MP-ObjectCounts response header. Just pass __mp__=counts in the query string.

That’s all it does at the moment - probably only useful for identifying which type of object is causing a memory leak. And, possibly, confirming which queries are contributing.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

New middleware instance.



15
16
17
# File 'lib/rack/mp/middleware.rb', line 15

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Run the middleware. Will count the objects, run the app, count the objects, and output how long the whole thing took, and the number of objects before and after.

NOTE - the header string will be JSON! Yuck. But it was the quickest way to do it for now.



25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/mp/middleware.rb', line 25

def call(env)
  return @app.call(env) unless active?(env)
  before, t0 = time { count_objects }
  result, t1 = time { @app.call(env.merge('mp.counting' => true)) }
  after,  t2 = time { count_objects }
  timings = { before: t0, request: t1, after: t2 }
  data = serialize(before: before, after: after, timings: timings)
  status, headers, body = *result
  [status, headers.merge('X-MP-ObjectCounts' => data), result]
end