Class: Featureflip::SharedCore

Inherits:
Object
  • Object
show all
Defined in:
lib/featureflip/shared_core.rb

Constant Summary collapse

LIVE_CORES =
{}
LIVE_CORES_MUTEX =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key:, config:) ⇒ SharedCore

— Instance methods —



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/featureflip/shared_core.rb', line 52

def initialize(sdk_key:, config:)
  @sdk_key = sdk_key
  @config = config
  @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



35
36
37
38
39
# File 'lib/featureflip/shared_core.rb', line 35

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 —



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/featureflip/shared_core.rb', line 10

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_testingObject



41
42
43
44
45
46
47
48
# File 'lib/featureflip/shared_core.rb', line 41

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

#_acquireObject



72
73
74
75
76
77
78
# File 'lib/featureflip/shared_core.rb', line 72

def _acquire
  @ref_mutex.synchronize do
    return false if @ref_count <= 0
    @ref_count += 1
    true
  end
end

#_configObject



93
94
95
# File 'lib/featureflip/shared_core.rb', line 93

def _config
  @config
end

#_ref_countObject



97
98
99
# File 'lib/featureflip/shared_core.rb', line 97

def _ref_count
  @ref_mutex.synchronize { @ref_count }
end

#_releaseObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/featureflip/shared_core.rb', line 80

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

— Evaluation methods —



107
108
109
# File 'lib/featureflip/shared_core.rb', line 107

def bool_variation(key, context, default_value)
  evaluate_flag(key, context, default_value)
end

#flushObject



188
189
190
# File 'lib/featureflip/shared_core.rb', line 188

def flush
  @event_processor&.flush
end

#identify(context) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/featureflip/shared_core.rb', line 176

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

Returns:

  • (Boolean)


101
102
103
# File 'lib/featureflip/shared_core.rb', line 101

def initialized?
  @initialized
end

#json_variation(key, context, default_value) ⇒ Object



119
120
121
# File 'lib/featureflip/shared_core.rb', line 119

def json_variation(key, context, default_value)
  evaluate_flag(key, context, default_value)
end

#number_variation(key, context, default_value) ⇒ Object



115
116
117
# File 'lib/featureflip/shared_core.rb', line 115

def number_variation(key, context, default_value)
  evaluate_flag(key, context, default_value)
end

#restartObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/featureflip/shared_core.rb', line 192

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



111
112
113
# File 'lib/featureflip/shared_core.rb', line 111

def string_variation(key, context, default_value)
  evaluate_flag(key, context, default_value)
end

#track(event_key, context, metadata = nil) ⇒ Object

— Event methods —



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/featureflip/shared_core.rb', line 163

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) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/featureflip/shared_core.rb', line 123

def variation_detail(key, context, default_value)
  context = normalize_context(context)

  if @test_mode
    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)
    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
  )
  value = result.value.nil? ? default_value : result.value
  record_evaluation(key, context, result.variation_key)

  Models::EvaluationDetail.new(
    value: value,
    reason: result.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.
  Models::EvaluationDetail.new(value: default_value, reason: "Error", prerequisite_key: nil)
end