mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 08:42:44 +08:00
FIX: view tracking needs to release data earlier
retaining data during queuing was causing huge memory spikes
This commit is contained in:
parent
39e828dee4
commit
acda6ebd60
|
@ -6,29 +6,25 @@ class Middleware::RequestTracker
|
||||||
@app = app
|
@app = app
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.log_request_on_site(result, env, helper=nil)
|
def self.log_request_on_site(data)
|
||||||
host = RailsMultisite::ConnectionManagement.host(env)
|
host = RailsMultisite::ConnectionManagement.host(env)
|
||||||
RailsMultisite::ConnectionManagement.with_hostname(host) do
|
RailsMultisite::ConnectionManagement.with_hostname(host) do
|
||||||
log_request(result,env,helper)
|
log_request(data)
|
||||||
end
|
end
|
||||||
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 = data[:status]
|
||||||
status = status.to_i
|
track_view = data[:track_view]
|
||||||
|
|
||||||
if (env[TRACK_VIEW] || (request.get? && !request.xhr? && headers["Content-Type"] =~ /text\/html/)) && status == 200
|
if track_view
|
||||||
if helper.is_crawler?
|
if data[:is_crawler]
|
||||||
ApplicationRequest.increment!(:page_view_crawler)
|
ApplicationRequest.increment!(:page_view_crawler)
|
||||||
elsif helper.has_auth_cookie?
|
elsif data[:has_auth_cookie]
|
||||||
ApplicationRequest.increment!(:page_view_logged_in)
|
ApplicationRequest.increment!(:page_view_logged_in)
|
||||||
else
|
else
|
||||||
ApplicationRequest.increment!(:page_view_anon)
|
ApplicationRequest.increment!(:page_view_anon)
|
||||||
|
@ -44,24 +40,46 @@ class Middleware::RequestTracker
|
||||||
elsif status >= 300
|
elsif status >= 300
|
||||||
ApplicationRequest.increment!(:http_3xx)
|
ApplicationRequest.increment!(:http_3xx)
|
||||||
else
|
else
|
||||||
if request.path =~ /^\/message-bus\// || request.path == /\/topics\/timings/
|
if data[:is_background]
|
||||||
ApplicationRequest.increment!(:http_background)
|
ApplicationRequest.increment!(:http_background)
|
||||||
elsif status >= 200 && status < 300
|
elsif status >= 200 && status < 300
|
||||||
ApplicationRequest.increment!(:http_2xx)
|
ApplicationRequest.increment!(:http_2xx)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# rescue => ex
|
|
||||||
# Discourse.handle_exception(ex, {message: "Failed to log request"})
|
|
||||||
end
|
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)
|
def call(env)
|
||||||
result = @app.call(env)
|
result = @app.call(env)
|
||||||
ensure
|
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
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,12 +18,17 @@ describe Middleware::RequestTracker do
|
||||||
|
|
||||||
ApplicationRequest.clear_cache!
|
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)"
|
"HTTP_USER_AGENT" => "AdsBot-Google (+http://www.google.com/adsbot.html)"
|
||||||
))
|
), ["200",{"Content-Type" => 'text/html'}])
|
||||||
Middleware::RequestTracker.log_request(["200",{}], env(
|
|
||||||
|
Middleware::RequestTracker.log_request(data)
|
||||||
|
|
||||||
|
data = Middleware::RequestTracker.get_data(env(
|
||||||
"HTTP_DISCOURSE_TRACK_VIEW" => "1"
|
"HTTP_DISCOURSE_TRACK_VIEW" => "1"
|
||||||
))
|
), ["200",{}])
|
||||||
|
|
||||||
|
Middleware::RequestTracker.log_request(data)
|
||||||
|
|
||||||
ApplicationRequest.write_cache!
|
ApplicationRequest.write_cache!
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user