Class: Stipa::Middleware::Timing

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

Overview

Records wall-clock time and appends X-Response-Time to the response. Uses CLOCK_MONOTONIC (not Time.now) so system clock adjustments don’t produce negative or inflated durations.

Instance Method Summary collapse

Constructor Details

#initialize(next_app) ⇒ Timing

Returns a new instance of Timing.



81
82
83
# File 'lib/stipa/middleware.rb', line 81

def initialize(next_app)
  @next_app = next_app
end

Instance Method Details

#call(req, res) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/stipa/middleware.rb', line 85

def call(req, res)
  t0     = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = @next_app.call(req, res)
  ms     = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0) * 1000).round(2)
  result.set_header('X-Response-Time', "#{ms}ms")
  result
end