Class: Featureflip::SharedCore
- Inherits:
-
Object
- Object
- Featureflip::SharedCore
- Defined in:
- lib/featureflip/shared_core.rb
Constant Summary collapse
- LIVE_CORES =
{}
- LIVE_CORES_MUTEX =
Mutex.new
Class Method Summary collapse
- ._create_for_testing(flags) ⇒ Object
-
._get_or_create(sdk_key, config) ⇒ Object
--- Class-level factory methods ---.
- ._reset_for_testing ⇒ Object
Instance Method Summary collapse
- #_acquire ⇒ Object
- #_config ⇒ Object
- #_ref_count ⇒ Object
- #_release ⇒ Object
-
#bool_variation(key, context, default_value) ⇒ Object
The typed accessors declare the JSON type they expect so a mismatch degrades to the caller's default with reason "Error" (#2281/#2286).
- #flush ⇒ Object
- #identify(context) ⇒ Object
-
#initialize(sdk_key:, config:) ⇒ SharedCore
constructor
--- Instance methods ---.
- #initialized? ⇒ Boolean
- #json_variation(key, context, default_value) ⇒ Object
- #number_variation(key, context, default_value) ⇒ Object
- #restart ⇒ Object
- #string_variation(key, context, default_value) ⇒ Object
-
#track(event_key, context, metadata = nil) ⇒ Object
--- Event methods ---.
-
#variation_detail(key, context, default_value, expected: nil) ⇒ Object
expectedis the JSON type the caller's accessor requires (:bool, :string or :number), or nil for the generic/JSON accessors, which are not type-checked because they have no single expected type.
Constructor Details
#initialize(sdk_key:, config:) ⇒ SharedCore
--- Instance methods ---
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/featureflip/shared_core.rb', line 78 def initialize(sdk_key:, config:) @sdk_key = sdk_key @config = config # Snapshot the (already-filtered) inspector list at construction: config is # immutable-after-init from the core's point of view, so the evaluation path # needs no locking. Deliberately excluded from _configs_equal -- callables # aren't structurally comparable and a differing inspector must not trigger # the "different config" warning. @inspectors = config.inspectors || [] @store = Store::FlagStore.new @evaluator = Evaluation::Evaluator.new @initialized = false @closed = false @test_mode = false @test_values = {} @http_client = nil @streaming_handler = nil @polling_handler = nil @event_processor = nil @ref_count = 1 @ref_mutex = Mutex.new @shut_down = false bootstrap! end |
Class Method Details
._create_for_testing(flags) ⇒ Object
61 62 63 64 65 |
# File 'lib/featureflip/shared_core.rb', line 61 def self._create_for_testing(flags) core = allocate core.send(:init_test_mode, flags) core end |
._get_or_create(sdk_key, config) ⇒ Object
--- Class-level factory methods ---
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/featureflip/shared_core.rb', line 36 def self._get_or_create(sdk_key, config) LIVE_CORES_MUTEX.synchronize do existing = LIVE_CORES[sdk_key] if existing if existing._acquire unless _configs_equal(existing._config, config) config.logger&.warn( "Featureflip: Client.get called with different config for same SDK key. " \ "Using existing configuration. Close all handles first to apply new config." ) end return existing else # Stale entry — remove and replace LIVE_CORES.delete(sdk_key) end end core = new(sdk_key: sdk_key, config: config) LIVE_CORES[sdk_key] = core core end end |
._reset_for_testing ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/featureflip/shared_core.rb', line 67 def self._reset_for_testing cores_to_release = LIVE_CORES_MUTEX.synchronize do snapshot = LIVE_CORES.values.dup LIVE_CORES.clear snapshot end cores_to_release.each { |c| c._release } end |
Instance Method Details
#_acquire ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/featureflip/shared_core.rb', line 104 def _acquire @ref_mutex.synchronize do return false if @ref_count <= 0 @ref_count += 1 true end end |
#_config ⇒ Object
125 126 127 |
# File 'lib/featureflip/shared_core.rb', line 125 def _config @config end |
#_ref_count ⇒ Object
129 130 131 |
# File 'lib/featureflip/shared_core.rb', line 129 def _ref_count @ref_mutex.synchronize { @ref_count } end |
#_release ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/featureflip/shared_core.rb', line 112 def _release run_shutdown = false @ref_mutex.synchronize do return if @ref_count <= 0 @ref_count -= 1 if @ref_count == 0 && !@shut_down @shut_down = true run_shutdown = true end end _shutdown if run_shutdown end |
#bool_variation(key, context, default_value) ⇒ Object
The typed accessors declare the JSON type they expect so a mismatch degrades to the caller's default with reason "Error" (#2281/#2286). json_variation takes no expectation: its value is an arbitrary structure, so there is no single type to check it against.
143 144 145 |
# File 'lib/featureflip/shared_core.rb', line 143 def bool_variation(key, context, default_value) evaluate_flag(key, context, default_value, expected: :bool) end |
#flush ⇒ Object
265 266 267 |
# File 'lib/featureflip/shared_core.rb', line 265 def flush @event_processor&.flush end |
#identify(context) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/featureflip/shared_core.rb', line 253 def identify(context) return unless @event_processor context = normalize_context(context) @event_processor.queue_event({ type: "Identify", flagKey: "$identify", userId: context["user_id"]&.to_s, timestamp: Time.now.utc.iso8601 }) end |
#initialized? ⇒ Boolean
133 134 135 |
# File 'lib/featureflip/shared_core.rb', line 133 def initialized? @initialized end |
#json_variation(key, context, default_value) ⇒ Object
155 156 157 |
# File 'lib/featureflip/shared_core.rb', line 155 def json_variation(key, context, default_value) evaluate_flag(key, context, default_value) end |
#number_variation(key, context, default_value) ⇒ Object
151 152 153 |
# File 'lib/featureflip/shared_core.rb', line 151 def number_variation(key, context, default_value) evaluate_flag(key, context, default_value, expected: :number) end |
#restart ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/featureflip/shared_core.rb', line 269 def restart return if @shut_down @streaming_handler&.stop @polling_handler&.stop @event_processor&.stop if @config.streaming start_streaming else start_polling end start_event_processor if @config.send_events end |
#string_variation(key, context, default_value) ⇒ Object
147 148 149 |
# File 'lib/featureflip/shared_core.rb', line 147 def string_variation(key, context, default_value) evaluate_flag(key, context, default_value, expected: :string) end |
#track(event_key, context, metadata = nil) ⇒ Object
--- Event methods ---
240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/featureflip/shared_core.rb', line 240 def track(event_key, context, = nil) return unless @event_processor context = normalize_context(context) @event_processor.queue_event({ type: "Custom", flagKey: event_key, userId: context["user_id"]&.to_s, metadata: || {}, timestamp: Time.now.utc.iso8601 }) end |
#variation_detail(key, context, default_value, expected: nil) ⇒ Object
expected is the JSON type the caller's accessor requires (:bool, :string
or :number), or nil for the generic/JSON accessors, which are not
type-checked because they have no single expected type.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/featureflip/shared_core.rb', line 162 def variation_detail(key, context, default_value, expected: nil) context = normalize_context(context) if @test_mode # Test-mode cores are built by _create_for_testing, which has no user # config, so there are never inspectors to notify here. value = @test_values.fetch(key, default_value) reason = @test_values.key?(key) ? "Fallthrough" : "FlagNotFound" return Models::EvaluationDetail.new(value: value, reason: reason) end flag = @store.get_flag(key) unless flag record_evaluation(key, context, nil) notify_inspectors(key, context, default_value, reason: "FlagNotFound") return Models::EvaluationDetail.new(value: default_value, reason: "FlagNotFound") end result = @evaluator.evaluate( flag, context, get_segment: method(:get_segment), all_flags: @store.all_flags_map ) # Malformed config: the evaluator selected a variation key the flag does # not define (e.g. a fallthrough/rule naming a since-deleted variation). # Degrade to the caller's default and report Error, mirroring the engine's # ServeVariation + the C#/Java SDKs (#1989). A variation that genuinely # exists with a nil value is NOT this case -- hence the key lookup rather # than a `value.nil?` check, which cannot tell the two apart. reason = if result.variation_key && !result.variation_key.empty? && flag.get_variation(result.variation_key).nil? "Error" else result.reason end served = result.value value = served.nil? ? default_value : served # A typed accessor asked for a specific JSON type and the SERVED value is not # it. Degrade to the caller's default and report Error so the mismatch is # detectable, rather than handing back a String where the caller's code # expects true/false (#2281/#2286). Checking `served` rather than `value` # matters: a variation whose value is genuinely JSON null has already been # replaced by default_value above, and that substitute would pass any check. if expected && !value_matches_type?(served, expected) value = default_value reason = "Error" end record_evaluation(key, context, result.variation_key) notify_inspectors( key, context, value, reason: reason, variation_key: result.variation_key, rule_id: result.rule_id, prerequisite_key: result.prerequisite_key ) Models::EvaluationDetail.new( value: value, reason: reason, rule_id: result.rule_id, variation_key: result.variation_key, prerequisite_key: result.prerequisite_key ) rescue StandardError # Prerequisite-resolution failures return PrerequisiteFailed cleanly through # the evaluator; this rescue only fires on unexpected exceptions (malformed # config, programming errors), so prerequisite_key has no defined value. notify_inspectors(key, context, default_value, reason: "Error") Models::EvaluationDetail.new(value: default_value, reason: "Error", prerequisite_key: nil) end |