Commit f0b0358b authored by Mario Celi's avatar Mario Celi

Preload project user authorizations on REST API

Improves performance of authenticated request on the /projects
endpoint

Changelog: performance
parent 7cff2f77
...@@ -157,6 +157,8 @@ module API ...@@ -157,6 +157,8 @@ module API
[options[:with].prepare_relation(projects, options), options] [options[:with].prepare_relation(projects, options), options]
end end
Preloaders::UserMaxAccessLevelInProjectsPreloader.new(records, current_user).execute if current_user
present records, options present records, options
end end
......
...@@ -164,24 +164,21 @@ RSpec.describe API::Projects do ...@@ -164,24 +164,21 @@ RSpec.describe API::Projects do
end end
end end
shared_examples_for 'projects response without N + 1 queries' do shared_examples_for 'projects response without N + 1 queries' do |threshold|
let(:additional_project) { create(:project, :public) }
it 'avoids N + 1 queries' do it 'avoids N + 1 queries' do
get api('/projects', current_user)
control = ActiveRecord::QueryRecorder.new do control = ActiveRecord::QueryRecorder.new do
get api('/projects', current_user) get api('/projects', current_user)
end end
if defined?(additional_project) additional_project
additional_project
else
create(:project, :public)
end
# TODO: We're currently querying to detect if a project is a fork
# in 2 ways. Lower this back to 8 when `ForkedProjectLink` relation is
# removed
expect do expect do
get api('/projects', current_user) get api('/projects', current_user)
end.not_to exceed_query_limit(control).with_threshold(9) end.not_to exceed_query_limit(control).with_threshold(threshold)
end end
end end
...@@ -194,7 +191,7 @@ RSpec.describe API::Projects do ...@@ -194,7 +191,7 @@ RSpec.describe API::Projects do
let(:projects) { [project] } let(:projects) { [project] }
end end
it_behaves_like 'projects response without N + 1 queries' do it_behaves_like 'projects response without N + 1 queries', 1 do
let(:current_user) { nil } let(:current_user) { nil }
end end
end end
...@@ -206,7 +203,7 @@ RSpec.describe API::Projects do ...@@ -206,7 +203,7 @@ RSpec.describe API::Projects do
let(:projects) { user_projects } let(:projects) { user_projects }
end end
it_behaves_like 'projects response without N + 1 queries' do it_behaves_like 'projects response without N + 1 queries', 0 do
let(:current_user) { user } let(:current_user) { user }
end end
...@@ -215,7 +212,7 @@ RSpec.describe API::Projects do ...@@ -215,7 +212,7 @@ RSpec.describe API::Projects do
create(:project, :public, group: create(:group)) create(:project, :public, group: create(:group))
end end
it_behaves_like 'projects response without N + 1 queries' do it_behaves_like 'projects response without N + 1 queries', 0 do
let(:current_user) { user } let(:current_user) { user }
let(:additional_project) { create(:project, :public, group: create(:group)) } let(:additional_project) { create(:project, :public, group: create(:group)) }
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment