Skip to content

Commit 339753c

Browse files
committed
Fix view runtime for controllers with async queries
1 parent 7f34113 commit 339753c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

activerecord/lib/active_record/railties/controller_runtime.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ def cleanup_view_runtime
3737
db_rt_before_render = ActiveRecord::RuntimeRegistry.reset
3838
self.db_runtime = (db_runtime || 0) + db_rt_before_render
3939
runtime = super
40+
queries_rt = ActiveRecord::RuntimeRegistry.sql_runtime - ActiveRecord::RuntimeRegistry.async_sql_runtime
4041
db_rt_after_render = ActiveRecord::RuntimeRegistry.reset
4142
self.db_runtime += db_rt_after_render
42-
runtime - db_rt_after_render
43+
runtime - queries_rt
4344
else
4445
super
4546
end

activerecord/lib/active_record/runtime_registry.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,26 @@ def sql_runtime=(runtime)
1717
ActiveSupport::IsolatedExecutionState[:active_record_sql_runtime] = runtime
1818
end
1919

20+
def async_sql_runtime
21+
ActiveSupport::IsolatedExecutionState[:active_record_async_sql_runtime] ||= 0.0
22+
end
23+
24+
def async_sql_runtime=(runtime)
25+
ActiveSupport::IsolatedExecutionState[:active_record_async_sql_runtime] = runtime
26+
end
27+
2028
def reset
2129
rt, self.sql_runtime = sql_runtime, 0.0
30+
self.async_sql_runtime = 0.0
2231
rt
2332
end
2433
end
2534
end
2635

2736
ActiveSupport::Notifications.monotonic_subscribe("sql.active_record") do |name, start, finish, id, payload|
28-
ActiveRecord::RuntimeRegistry.sql_runtime += (finish - start)
37+
runtime = finish - start
38+
if payload[:async]
39+
ActiveRecord::RuntimeRegistry.async_sql_runtime += (runtime - payload[:lock_wait])
40+
end
41+
ActiveRecord::RuntimeRegistry.sql_runtime += runtime
2942
end

0 commit comments

Comments
 (0)