debian-mirror-gitlab/lib/gitlab/performance_bar.rb

55 lines
1.4 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
module PerformanceBar
2019-12-04 20:38:33 +05:30
ALLOWED_USER_IDS_KEY = 'performance_bar_allowed_user_ids:v2'
2019-09-30 21:07:59 +05:30
EXPIRY_TIME_L1_CACHE = 1.minute
EXPIRY_TIME_L2_CACHE = 5.minutes
2017-09-10 17:25:29 +05:30
2019-12-04 20:38:33 +05:30
def self.enabled_for_request?
Gitlab::SafeRequestStore[:peek_enabled]
end
def self.enabled_for_user?(user = nil)
2018-03-17 18:26:18 +05:30
return true if Rails.env.development?
2018-05-09 12:01:36 +05:30
return true if user&.admin?
2017-09-10 17:25:29 +05:30
return false unless user && allowed_group_id
allowed_user_ids.include?(user.id)
end
def self.allowed_group_id
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.performance_bar_allowed_group_id
2017-09-10 17:25:29 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def self.allowed_user_ids
2019-09-30 21:07:59 +05:30
l1_cache_backend.fetch(ALLOWED_USER_IDS_KEY, expires_in: EXPIRY_TIME_L1_CACHE) do
l2_cache_backend.fetch(ALLOWED_USER_IDS_KEY, expires_in: EXPIRY_TIME_L2_CACHE) do
group = Group.find_by_id(allowed_group_id)
2017-09-10 17:25:29 +05:30
2019-09-30 21:07:59 +05:30
if group
GroupMembersFinder.new(group).execute.pluck(:user_id)
else
[]
end
2017-09-10 17:25:29 +05:30
end
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def self.expire_allowed_user_ids_cache
2019-09-30 21:07:59 +05:30
l1_cache_backend.delete(ALLOWED_USER_IDS_KEY)
l2_cache_backend.delete(ALLOWED_USER_IDS_KEY)
end
def self.l1_cache_backend
2020-05-24 23:13:21 +05:30
Gitlab::ProcessMemoryCache.cache_backend
2019-09-30 21:07:59 +05:30
end
def self.l2_cache_backend
Rails.cache
2017-09-10 17:25:29 +05:30
end
end
end