2019-09-04 21:01:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
module Feature
|
2019-09-04 21:01:54 +05:30
|
|
|
class Gitaly
|
2020-03-13 15:44:24 +05:30
|
|
|
PREFIX = "gitaly_"
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
# 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
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
class << self
|
2023-01-13 00:05:48 +05:30
|
|
|
def enabled_for_any?(feature_flag, *actors)
|
2019-09-04 21:01:54 +05:30
|
|
|
return false unless Feature::FlipperFeature.table_exists?
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
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
|
2019-12-26 22:10:19 +05:30
|
|
|
rescue ActiveRecord::NoDatabaseError, PG::ConnectionBad
|
2019-09-04 21:01:54 +05:30
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
def server_feature_flags(repository: nil, user: nil, project: nil, group: nil)
|
2020-04-08 14:13:33 +05:30
|
|
|
# We need to check that both the DB connection and table exists
|
2021-12-11 22:18:48 +05:30
|
|
|
return {} unless FlipperFeature.database.cached_table_exists?
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
# 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
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
Feature.persisted_names
|
|
|
|
.select { |f| f.start_with?(PREFIX) }
|
2021-03-11 19:13:27 +05:30
|
|
|
.to_h do |f|
|
2023-01-13 00:05:48 +05:30
|
|
|
["gitaly-feature-#{f.delete_prefix(PREFIX).tr('_', '-')}", enabled_for_any?(f, *actors).to_s]
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
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)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|