Commit 921324ad authored by Nick Thomas's avatar Nick Thomas

Merge branch 'sh-memoize-project-license-name' into 'master'

Only load project license if needed

Closes #223740

See merge request gitlab-org/gitlab!35068
parents fe2699c5 d68fc202
......@@ -384,9 +384,12 @@ module ProjectsHelper
end
def project_license_name(project)
project.repository.license&.name
key = "project:#{project.id}:license_name"
Gitlab::SafeRequestStore.fetch(key) { project.repository.license&.name }
rescue GRPC::Unavailable, GRPC::DeadlineExceeded, Gitlab::Git::CommandError => e
Gitlab::ErrorTracking.track_exception(e)
Gitlab::SafeRequestStore[key] = nil
nil
end
......
......@@ -16,7 +16,6 @@
- css_controls_class = compact_mode ? [] : ["flex-lg-row", "justify-content-lg-between"]
- css_controls_class << "with-pipeline-status" if show_pipeline_status_icon
- avatar_container_class = project.creator && use_creator_avatar ? '' : 'rect-avatar'
- license_name = project_license_name(project)
%li.project-row.d-flex{ class: css_class }
= cache(cache_key) do
......@@ -43,10 +42,10 @@
%span.metadata-info.visibility-icon.append-right-10.gl-mt-3.text-secondary.has-tooltip{ data: { container: 'body', placement: 'top' }, title: visibility_icon_description(project) }
= visibility_level_icon(project.visibility_level, fw: true)
- if explore_projects_tab? && license_name
- if explore_projects_tab? && project_license_name(project)
%span.metadata-info.d-inline-flex.align-items-center.append-right-10.gl-mt-3
= sprite_icon('scale', size: 14, css_class: 'gl-mr-2')
= license_name
= project_license_name(project)
- if !explore_projects_tab? && access&.nonzero?
-# haml-lint:disable UnnecessaryStringOutput
......
---
title: Only load project license if needed
merge_request: 35068
author:
type: performance
......@@ -977,19 +977,32 @@ RSpec.describe ProjectsHelper do
end
end
describe '#project_license_name(project)' do
describe '#project_license_name(project)', :request_store do
let_it_be(:project) { create(:project) }
let_it_be(:repository) { project.repository }
subject { project_license_name(project) }
def license_name
project_license_name(project)
end
context 'gitaly is working appropriately' do
it 'returns the license name' do
license = Licensee::License.new('mit')
allow(repository).to receive(:license).and_return(license)
let(:license) { Licensee::License.new('mit') }
before do
expect(repository).to receive(:license).and_return(license)
end
it 'returns the license name' do
expect(subject).to eq(license.name)
end
it 'memoizes the value' do
expect do
2.times { expect(license_name).to eq(license.name) }
end.to change { Gitlab::GitalyClient.get_request_count }.by_at_most(1)
end
end
context 'gitaly is unreachable' do
......@@ -1003,10 +1016,16 @@ RSpec.describe ProjectsHelper do
subject
end
it 'memoizes the nil value' do
expect do
2.times { expect(license_name).to be_nil }
end.to change { Gitlab::GitalyClient.get_request_count }.by_at_most(1)
end
end
before do
allow(repository).to receive(:license).and_raise(exception)
expect(repository).to receive(:license).and_raise(exception)
end
context "Gitlab::Git::CommandError" do
......
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