Class: RackGcProfiler::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_gc_profiler.rb

Constant Summary collapse

GC_TIME_FORMAT =
"%0.6f".freeze
GC_TIME_HEADER =
"GC-Time".freeze
GC_RUNS_HEADER =
"GC-Runs".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, profiler = GC::Profiler) ⇒ Middleware

Returns a new instance of Middleware.



12
13
14
15
# File 'lib/rack_gc_profiler.rb', line 12

def initialize(app, profiler = GC::Profiler)
  @app = app
  @profiler = profiler
end

Instance Attribute Details

#profilerObject (readonly)

Returns the value of attribute profiler.



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

def profiler
  @profiler
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack_gc_profiler.rb', line 17

def call(env)
  @profiler.enable

  status, headers, body = @app.call(env)

  headers[GC_TIME_HEADER] = GC_TIME_FORMAT % @profiler.total_time
  headers[GC_RUNS_HEADER] = @profiler.raw_data.size.to_s

  [status, headers, body]

ensure
  @profiler.disable
  @profiler.clear

end