Class: RightSupport::Rack::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/right_support/rack/runtime.rb

Overview

Sets an “X-Runtime” response header, indicating the response time of the request, in milliseconds

You can put it right before the application to see the processing time, or before all the other middlewares to include time for them, too.

Instance Method Summary collapse

Constructor Details

#initialize(app, name = nil) ⇒ Runtime

Returns a new instance of Runtime.



34
35
36
37
38
# File 'lib/right_support/rack/runtime.rb', line 34

def initialize(app, name = nil)
  @app = app
  @header_name = "X-Runtime"
  @header_name << "-#{name}" if name
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/right_support/rack/runtime.rb', line 40

def call(env)
  start_time = Time.now
  status, headers, body = @app.call(env)
  request_time = Time.now - start_time

  if !headers.has_key?(@header_name)
    headers[@header_name] = (request_time * 1000).truncate.to_s
  end

  [status, headers, body]
end