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 ...@@ -2,6 +2,7 @@ module Gitlab
module Checks module Checks
class ChangeAccess class ChangeAccess
include PathLocksHelper include PathLocksHelper
include Gitlab::Utils::StrongMemoize
ERROR_MESSAGES = { ERROR_MESSAGES = {
push_code: 'You are not allowed to push code to this project.', push_code: 'You are not allowed to push code to this project.',
...@@ -300,9 +301,11 @@ module Gitlab ...@@ -300,9 +301,11 @@ module Gitlab
end end
def validate_path_locks? def validate_path_locks?
@validate_path_locks ||= @project.feature_available?(:file_locks) && strong_memoize(:validate_path_locks) do
project.path_locks.any? && @newrev && @oldrev && @project.feature_available?(:file_locks) &&
project.default_branch == @branch_name # locks protect default branch only project.path_locks.any? && @newrev && @oldrev &&
project.default_branch == @branch_name # locks protect default branch only
end
end end
def path_locks_validation def path_locks_validation
......
...@@ -319,6 +319,12 @@ describe Gitlab::Checks::ChangeAccess do ...@@ -319,6 +319,12 @@ describe Gitlab::Checks::ChangeAccess do
it 'allows the default branch even if it does not match push rule' do it 'allows the default branch even if it does not match push rule' do
expect { subject.exec }.not_to raise_error expect { subject.exec }.not_to raise_error
end 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
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