Cache PerformanceBar data using RequestStore

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2017-06-28 19:18:16 +02:00
parent 186048a404
commit b531616eba
3 changed files with 33 additions and 18 deletions

View file

@ -489,7 +489,7 @@ Settings.gitaly['enabled'] = true if Settings.gitaly['enabled'].nil?
# Performance bar # Performance bar
# #
Settings['performance_bar'] ||= Settingslogic.new({}) Settings['performance_bar'] ||= Settingslogic.new({})
Settings.performance_bar['allowed_group'] = 'gitlab-org' if Settings.performance_bar['allowed_group'].nil? Settings.performance_bar['allowed_group'] ||= nil
# #
# Webpack settings # Webpack settings

View file

@ -5,19 +5,34 @@ module Gitlab
end end
def self.allowed_actor?(actor) def self.allowed_actor?(actor)
group = allowed_group return false unless actor.thing&.is_a?(User) && allowed_group
return false unless actor&.is_a?(User) && group
GroupMembersFinder.new(group) if RequestStore.active?
.execute RequestStore.fetch('performance_bar:user_member_of_allowed_group') do
.where(user_id: actor.id) user_member_of_allowed_group?(actor.thing)
.any? end
else
user_member_of_allowed_group?(actor.thing)
end
end end
def self.allowed_group def self.allowed_group
return nil unless Gitlab.config.performance_bar.allowed_group return nil unless Gitlab.config.performance_bar.allowed_group
Group.by_path(Gitlab.config.performance_bar.allowed_group) if RequestStore.active?
RequestStore.fetch('performance_bar:allowed_group') do
Group.by_path(Gitlab.config.performance_bar.allowed_group)
end
else
Group.by_path(Gitlab.config.performance_bar.allowed_group)
end
end
def self.user_member_of_allowed_group?(user)
GroupMembersFinder.new(allowed_group)
.execute
.where(user_id: user.id)
.any?
end end
end end
end end

View file

@ -2,38 +2,38 @@ require 'spec_helper'
describe Gitlab::PerformanceBar do describe Gitlab::PerformanceBar do
describe '.enabled?' do describe '.enabled?' do
it 'returns false when given user is nil' do it 'returns false when given actor is nil' do
expect(described_class.enabled?(nil)).to be_falsy expect(described_class.enabled?(nil)).to be_falsy
end end
it 'returns false when feature is disabled' do it 'returns false when feature is disabled' do
user = double('user') actor = double('actor')
expect(Feature).to receive(:enabled?) expect(Feature).to receive(:enabled?)
.with(:gitlab_performance_bar, user).and_return(false) .with(:gitlab_performance_bar, actor).and_return(false)
expect(described_class.enabled?(user)).to be_falsy expect(described_class.enabled?(actor)).to be_falsy
end end
it 'returns true when feature is enabled' do it 'returns true when feature is enabled' do
user = double('user') actor = double('actor')
expect(Feature).to receive(:enabled?) expect(Feature).to receive(:enabled?)
.with(:gitlab_performance_bar, user).and_return(true) .with(:gitlab_performance_bar, actor).and_return(true)
expect(described_class.enabled?(user)).to be_truthy expect(described_class.enabled?(actor)).to be_truthy
end end
end end
describe '.allowed_actor?' do describe '.allowed_actor?' do
it 'returns false when given actor is not a User' do it 'returns false when given actor is not a User' do
actor = double actor = double('actor', thing: double)
expect(described_class.allowed_actor?(actor)).to be_falsy expect(described_class.allowed_actor?(actor)).to be_falsy
end end
context 'when given actor is a User' do context 'when given actor is a User' do
let(:actor) { create(:user) } let(:actor) { double('actor', thing: create(:user)) }
before do before do
stub_performance_bar_setting(allowed_group: 'my-group') stub_performance_bar_setting(allowed_group: 'my-group')
@ -56,7 +56,7 @@ describe Gitlab::PerformanceBar do
context 'when user is a member of the allowed group' do context 'when user is a member of the allowed group' do
before do before do
my_group.add_developer(actor) my_group.add_developer(actor.thing)
end end
it 'returns true' do it 'returns true' do