Commit 49fcbe5e authored by Stan Hu's avatar Stan Hu

Properly memoize ChangeAccess#validate_path_locks? to avoid excessive queries

Closes #4488
parent d9014ba8
---
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