diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-17 14:33:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-17 14:33:21 +0300 |
commit | 7021455bd1ed7b125c55eb1b33c5a01f2bc55ee0 (patch) | |
tree | 5bdc2229f5198d516781f8d24eace62fc7e589e9 /lib/feature | |
parent | 185b095e93520f96e9cfc31d9c3e69b498cdab7c (diff) |
Add latest changes from gitlab-org/gitlab@15-6-stable-eev15.6.0-rc42
Diffstat (limited to 'lib/feature')
-rw-r--r-- | lib/feature/gitaly.rb | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/lib/feature/gitaly.rb b/lib/feature/gitaly.rb index 0c6b9dfde7a..fd798862fa8 100644 --- a/lib/feature/gitaly.rb +++ b/lib/feature/gitaly.rb @@ -4,26 +4,60 @@ module Feature class Gitaly PREFIX = "gitaly_" + # Wrapper for feature flag actor to avoid unnecessarily SQL queries + class ActorWrapper + def initialize(klass, id) + @klass = klass + @id = id + end + + def flipper_id + "#{@klass.name}:#{@id}" + end + end + class << self - def enabled?(feature_flag, project = nil) + def enabled_for_any?(feature_flag, *actors) return false unless Feature::FlipperFeature.table_exists? - Feature.enabled?("#{PREFIX}#{feature_flag}", project, type: :undefined, default_enabled_if_undefined: false) + actors = actors.compact + return Feature.enabled?(feature_flag, type: :undefined, default_enabled_if_undefined: false) if actors.empty? + + actors.any? do |actor| + Feature.enabled?(feature_flag, actor, type: :undefined, default_enabled_if_undefined: false) + end rescue ActiveRecord::NoDatabaseError, PG::ConnectionBad false end - def server_feature_flags(project = nil) + def server_feature_flags(repository: nil, user: nil, project: nil, group: nil) # We need to check that both the DB connection and table exists return {} unless FlipperFeature.database.cached_table_exists? + # The order of actors here is significant. Percentage-based actor selection may not work as expected if this + # order changes. We want repository actor to take highest precedence. + actors = [repository, user, project, group].compact + Feature.persisted_names .select { |f| f.start_with?(PREFIX) } .to_h do |f| - flag = f.delete_prefix(PREFIX) + ["gitaly-feature-#{f.delete_prefix(PREFIX).tr('_', '-')}", enabled_for_any?(f, *actors).to_s] + end + end - ["gitaly-feature-#{flag.tr('_', '-')}", enabled?(flag, project).to_s] - end + def user_actor(user = nil) + return ::Feature::Gitaly::ActorWrapper.new(::User, user.id) if user.is_a?(::User) + + user_id = Gitlab::ApplicationContext.current_context_attribute(:user_id) + ::Feature::Gitaly::ActorWrapper.new(::User, user_id) if user_id + end + + def project_actor(container) + ::Feature::Gitaly::ActorWrapper.new(::Project, container.id) if container.is_a?(::Project) + end + + def group_actor(container) + ::Feature::Gitaly::ActorWrapper.new(::Group, container.namespace_id) if container.is_a?(::Project) end end end |