Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2017-07-04 17:15:24 +0300
committerRémy Coutable <remy@rymai.me>2017-07-06 12:18:26 +0300
commit00ac76cc4ce87954d770abae411c54eb8bf23360 (patch)
treee9727ab4149ecd6ece8e3ebe692998e53821b3c8 /lib/gitlab/performance_bar.rb
parent75bd0c3a444cb58f1a7ed5f0c540dddbde6f09ed (diff)
Cache the allowed user IDs for the performance bar, in Redis for 10 minutes
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'lib/gitlab/performance_bar.rb')
-rw-r--r--lib/gitlab/performance_bar.rb52
1 files changed, 35 insertions, 17 deletions
diff --git a/lib/gitlab/performance_bar.rb b/lib/gitlab/performance_bar.rb
index 60c8ba5063e..85f4371ec23 100644
--- a/lib/gitlab/performance_bar.rb
+++ b/lib/gitlab/performance_bar.rb
@@ -1,35 +1,53 @@
module Gitlab
module PerformanceBar
+ ALLOWED_USER_IDS_KEY = 'performance_bar_allowed_user_ids'.freeze
+ # The time (in seconds) after which a set of allowed user IDs is expired
+ # automatically.
+ ALLOWED_USER_IDS_TIME_TO_LIVE = 10.minutes.to_i
+
def self.enabled?(current_user = nil)
Feature.enabled?(:gitlab_performance_bar, current_user)
end
def self.allowed_user?(user)
- return false unless allowed_group
+ return false unless allowed_group_name
- if RequestStore.active?
- RequestStore.fetch('performance_bar:user_member_of_allowed_group') do
- user_member_of_allowed_group?(user)
- end
- else
- user_member_of_allowed_group?(user)
- end
+ allowed_user_ids.include?(user.id)
end
- def self.allowed_group
- return nil unless Gitlab.config.performance_bar.allowed_group
+ def self.allowed_group_name
+ Gitlab.config.performance_bar.allowed_group
+ end
+
+ def self.allowed_user_ids
+ Gitlab::Redis.with do |redis|
+ if redis.exists(cache_key)
+ redis.smembers(cache_key).map(&:to_i)
+ else
+ group = Group.find_by_full_path(allowed_group_name)
+ # Redis#sadd doesn't accept an empty array, but we still want to use
+ # Redis to let us know that no users are allowed, so we set the
+ # array to [-1] in this case.
+ user_ids =
+ if group
+ GroupMembersFinder.new(group).execute
+ .pluck(:user_id).presence || [-1]
+ else
+ [-1]
+ end
+
+ redis.multi do
+ redis.sadd(cache_key, user_ids)
+ redis.expire(cache_key, ALLOWED_USER_IDS_TIME_TO_LIVE)
+ end
- if RequestStore.active?
- RequestStore.fetch('performance_bar:allowed_group') do
- Group.find_by_full_path(Gitlab.config.performance_bar.allowed_group)
+ user_ids
end
- else
- Group.find_by_full_path(Gitlab.config.performance_bar.allowed_group)
end
end
- def self.user_member_of_allowed_group?(user)
- GroupMembersFinder.new(allowed_group).execute.exists?(user_id: user.id)
+ def self.cache_key
+ "#{ALLOWED_USER_IDS_KEY}:#{allowed_group_name}"
end
end
end