Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix time measurement for async #1

Merged
merged 2 commits into from

1 participant

Sam Stokes
Sam Stokes
Owner

@ept I've tested the fix with a trivial app, but would appreciate your eyes on it because I'm clearly a stupidhead. Then I'll push the new gem.

Sam Stokes
Owner

My trivial test app:

require 'eventmachine'
require 'rack/informant'
require 'sinatra'
require 'thin'

use(Rack::Informant) {|request| p request[:runtime] }

get '/hi' do
  EM::add_timer(1) { env['async.callback'].call [200, {}, 'hey'] }
  throw :async
end
samstokes added some commits
Sam Stokes samstokes Timing was broken for async endpoints, fix it...
There's only one Rack::Informant instance per app (not per request!), so
for async endpoints, @start would get overwritten by each subsequent
request.  An outstanding request would therefore appear to have started
whenever the most recent request came in, rather than when it actually
started, and thus be reported as quicker than it was.

Fix by using a local variable in #call (which *is* per-request) rather
than an instance variable.

Thanks @ept for pointing this out.
961d242
Sam Stokes samstokes bump version to 1.1.0 864f595
Sam Stokes samstokes merged commit 6b18538 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 15, 2012
  1. Sam Stokes

    Timing was broken for async endpoints, fix it...

    samstokes authored
    There's only one Rack::Informant instance per app (not per request!), so
    for async endpoints, @start would get overwritten by each subsequent
    request.  An outstanding request would therefore appear to have started
    whenever the most recent request came in, rather than when it actually
    started, and thus be reported as quicker than it was.
    
    Fix by using a local variable in #call (which *is* per-request) rather
    than an instance variable.
    
    Thanks @ept for pointing this out.
  2. Sam Stokes

    bump version to 1.1.0

    samstokes authored
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 6 deletions.
  1. +5 −5 lib/rack/informant.rb
  2. +1 −1  rack-informant.gemspec
10 lib/rack/informant.rb
View
@@ -30,13 +30,13 @@ def initialize(app, &block)
end
def call(env)
- @start = Time.now
+ start = Time.now
if original_callback = env['async.callback']
env['async.callback'] = proc do |response|
status, headers, body = response
- inform!(env, status, headers, body)
+ inform!(env, start, status, headers, body)
original_callback.call(response)
end
@@ -49,7 +49,7 @@ def call(env)
return [status, headers, body] if status == -1 # alternative async API
# if we got *this* far, @app.call is definitely synchronous
- inform!(env, status, headers, body)
+ inform!(env, start, status, headers, body)
return [status, headers, body]
end
@@ -59,8 +59,8 @@ def call(env)
end
private
- def inform!(env, status, headers, body)
- runtime = Time.now - @start
+ def inform!(env, start, status, headers, body)
+ runtime = Time.now - start
event = {
method: env['REQUEST_METHOD'],
2  rack-informant.gemspec
View
@@ -2,7 +2,7 @@ Gem::Specification.new do |s|
s.name = 'rack-informant'
s.authors = ['Rapportive Inc']
s.email = 'supportive@rapportive.com'
- s.version = '1.0.0'
+ s.version = '1.1.0'
s.summary = %q{Reporting middleware.}
s.description = "Middleware that reports all requests to an interested party (e.g. for analytics)."
s.homepage = "https://github.com/rapportive-oss/rack-informant"
Something went wrong with that request. Please try again.