FIX: view tracking needs to release data earlier

retaining data during queuing was causing huge memory spikes
This commit is contained in:
Sam 2015-02-10 17:03:33 +11:00
parent 39e828dee4
commit acda6ebd60
2 changed files with 44 additions and 21 deletions

View File

@ -6,29 +6,25 @@ class Middleware::RequestTracker
@app = app
end
def self.log_request_on_site(result, env, helper=nil)
def self.log_request_on_site(data)
host = RailsMultisite::ConnectionManagement.host(env)
RailsMultisite::ConnectionManagement.with_hostname(host) do
log_request(result,env,helper)
log_request(data)
end
end
PATH_PARAMS = "action_dispatch.request.path_parameters".freeze
TRACK_VIEW = "HTTP_DISCOURSE_TRACK_VIEW".freeze
def self.log_request(result,env,helper=nil)
def self.log_request(data)
helper ||= Middleware::AnonymousCache::Helper.new(env)
request = Rack::Request.new(env)
status,headers = result
status = status.to_i
status = data[:status]
track_view = data[:track_view]
if (env[TRACK_VIEW] || (request.get? && !request.xhr? && headers["Content-Type"] =~ /text\/html/)) && status == 200
if helper.is_crawler?
if track_view
if data[:is_crawler]
ApplicationRequest.increment!(:page_view_crawler)
elsif helper.has_auth_cookie?
elsif data[:has_auth_cookie]
ApplicationRequest.increment!(:page_view_logged_in)
else
ApplicationRequest.increment!(:page_view_anon)
@ -44,24 +40,46 @@ class Middleware::RequestTracker
elsif status >= 300
ApplicationRequest.increment!(:http_3xx)
else
if request.path =~ /^\/message-bus\// || request.path == /\/topics\/timings/
if data[:is_background]
ApplicationRequest.increment!(:http_background)
elsif status >= 200 && status < 300
ApplicationRequest.increment!(:http_2xx)
end
end
# rescue => ex
# Discourse.handle_exception(ex, {message: "Failed to log request"})
end
TRACK_VIEW = "HTTP_DISCOURSE_TRACK_VIEW".freeze
CONTENT_TYPE = "Content-Type".freeze
def self.get_data(env,result)
status,headers = result
status = status.to_i
helper = Middleware::AnonymousCache::Helper.new(env)
request = Rack::Request.new(env)
{
status: status,
is_crawler: helper.is_crawler?,
has_auth_cookie: helper.has_auth_cookie?,
is_background: request.path =~ /^\/message-bus\// || request.path == /\/topics\/timings/,
track_view: (env[TRACK_VIEW] || (request.get? && !request.xhr? && headers[CONTENT_TYPE] =~ /text\/html/)) && status == 200
}
end
def call(env)
result = @app.call(env)
ensure
Scheduler::Defer.later("Track view", _db=nil) do
self.class.log_request_on_site(result,env)
# we got to skip this on error ... its just logging
data = self.class.get_data(env,result) rescue nil
if data
Scheduler::Defer.later("Track view", _db=nil) do
self.class.log_request_on_site(data)
end
end
end
end

View File

@ -18,12 +18,17 @@ describe Middleware::RequestTracker do
ApplicationRequest.clear_cache!
Middleware::RequestTracker.log_request(["200",{"Content-Type" => 'text/html'}], env(
data = Middleware::RequestTracker.get_data(env(
"HTTP_USER_AGENT" => "AdsBot-Google (+http://www.google.com/adsbot.html)"
))
Middleware::RequestTracker.log_request(["200",{}], env(
), ["200",{"Content-Type" => 'text/html'}])
Middleware::RequestTracker.log_request(data)
data = Middleware::RequestTracker.get_data(env(
"HTTP_DISCOURSE_TRACK_VIEW" => "1"
))
), ["200",{}])
Middleware::RequestTracker.log_request(data)
ApplicationRequest.write_cache!