Commit f26d81f3 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'sh-strong-memoize-validate-path-locks' into 'master'

Properly memoize ChangeAccess#validate_path_locks? to avoid excessive queries

Closes #4488

See merge request gitlab-org/gitlab-ee!3945
parents 1b16ffac 49fcbe5e
---
title: Properly memoize ChangeAccess#validate_path_locks? to avoid excessive queries
merge_request:
author:
type: performance
......@@ -2,6 +2,7 @@ module Gitlab
module Checks
class ChangeAccess
include PathLocksHelper
include Gitlab::Utils::StrongMemoize
ERROR_MESSAGES = {
push_code: 'You are not allowed to push code to this project.',
......@@ -300,9 +301,11 @@ module Gitlab
end
def validate_path_locks?
@validate_path_locks ||= @project.feature_available?(:file_locks) &&
project.path_locks.any? && @newrev && @oldrev &&
project.default_branch == @branch_name # locks protect default branch only
strong_memoize(:validate_path_locks) do
@project.feature_available?(:file_locks) &&
project.path_locks.any? && @newrev && @oldrev &&
project.default_branch == @branch_name # locks protect default branch only
end
end
def path_locks_validation
......
......@@ -319,6 +319,12 @@ describe Gitlab::Checks::ChangeAccess do
it 'allows the default branch even if it does not match push rule' do
expect { subject.exec }.not_to raise_error
end
it 'memoizes the validate_path_locks? call' do
expect(project.path_locks).to receive(:any?).once.and_call_original
2.times { subject.exec }
end
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