featureflip 2.13.1 → 2.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/featureflip/client.rb +7 -0
- data/lib/featureflip/config.rb +42 -1
- data/lib/featureflip/data_source/streaming.rb +5 -1
- data/lib/featureflip/redacting_logger.rb +121 -0
- data/lib/featureflip/shared_core.rb +344 -15
- data/lib/featureflip/snapshot.rb +256 -0
- data/lib/featureflip/store/flag_store.rb +4 -0
- data/lib/featureflip/version.rb +1 -1
- data/lib/featureflip.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 15b0723d1722a5537c1d86f006e1b31f44041a3375821037bd120f0638cf4ea8
|
|
4
|
+
data.tar.gz: 2ee4100d6030d53db2d8ed3d6b281d2d365fe0ffbcb927ed2754236c404cc20f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 29e129490227e460f958a12f5912127df85617cffa97365f909a83d7b638cae0c379cbf47bdcb32242b0671f0ccf934adceb4c4b3757eeabe928a14186131718
|
|
7
|
+
data.tar.gz: 906d860e2606afb9db5a5d83d7ad561fbf97b7d97899215f9f7504e14b0e990f749846246d3041bf22422e48c488c7c996a5f16111a9daeb84888fc219bd1f19
|
data/lib/featureflip/client.rb
CHANGED
|
@@ -28,6 +28,13 @@ module Featureflip
|
|
|
28
28
|
@core.initialized?
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
# Where the flag data came from: a StartupInfo whose +source+ is "live" after a
|
|
32
|
+
# successful fetch, "snapshot" when started from the configured snapshot_store, or
|
|
33
|
+
# "none" (defaults served). It moves to "live" once the Evaluation API is reached.
|
|
34
|
+
def startup_info
|
|
35
|
+
@core.startup_info
|
|
36
|
+
end
|
|
37
|
+
|
|
31
38
|
def bool_variation(key, context, default_value)
|
|
32
39
|
return default_value if @closed
|
|
33
40
|
@core.bool_variation(key, context, default_value)
|
data/lib/featureflip/config.rb
CHANGED
|
@@ -13,6 +13,30 @@ module Featureflip
|
|
|
13
13
|
# than changing behavior.
|
|
14
14
|
attr_accessor :wrapper_name, :wrapper_version
|
|
15
15
|
|
|
16
|
+
# Opt-in last-known-config store (see Featureflip::FileSnapshotStore). When the
|
|
17
|
+
# initial fetch fails or times out the client starts from the stored snapshot
|
|
18
|
+
# instead of serving defaults. Honored on the first Client.get per SDK key. Not
|
|
19
|
+
# part of the different-config comparison; its identity is compared separately.
|
|
20
|
+
attr_accessor :snapshot_store
|
|
21
|
+
|
|
22
|
+
# How old a stored snapshot may be and still be used, in seconds. Must be a
|
|
23
|
+
# positive, finite number; there is no "never expire" value.
|
|
24
|
+
DEFAULT_MAX_SNAPSHOT_AGE_SECONDS = 86_400
|
|
25
|
+
UNSET = Object.new.freeze
|
|
26
|
+
private_constant :UNSET
|
|
27
|
+
attr_reader :max_snapshot_age_seconds
|
|
28
|
+
|
|
29
|
+
def max_snapshot_age_seconds=(value)
|
|
30
|
+
@max_snapshot_age_explicit = true
|
|
31
|
+
@max_snapshot_age_seconds = value
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# True when the caller passed a max age, so the "option ignored" warning can tell
|
|
35
|
+
# an omitted option from the default.
|
|
36
|
+
def max_snapshot_age_explicit?
|
|
37
|
+
@max_snapshot_age_explicit
|
|
38
|
+
end
|
|
39
|
+
|
|
16
40
|
# Evaluation inspectors -- callables invoked once per variation call with a
|
|
17
41
|
# Models::EvaluationEvent. Always an Array; non-callable entries are dropped
|
|
18
42
|
# on assignment rather than blowing up on the evaluation hot path.
|
|
@@ -33,7 +57,9 @@ module Featureflip
|
|
|
33
57
|
logger: nil,
|
|
34
58
|
inspectors: nil,
|
|
35
59
|
wrapper_name: nil,
|
|
36
|
-
wrapper_version: nil
|
|
60
|
+
wrapper_version: nil,
|
|
61
|
+
snapshot_store: nil,
|
|
62
|
+
max_snapshot_age_seconds: UNSET
|
|
37
63
|
)
|
|
38
64
|
@sdk_key = sdk_key
|
|
39
65
|
@base_url = base_url
|
|
@@ -50,6 +76,9 @@ module Featureflip
|
|
|
50
76
|
self.inspectors = inspectors
|
|
51
77
|
@wrapper_name = wrapper_name
|
|
52
78
|
@wrapper_version = wrapper_version
|
|
79
|
+
@snapshot_store = snapshot_store
|
|
80
|
+
@max_snapshot_age_explicit = !UNSET.equal?(max_snapshot_age_seconds)
|
|
81
|
+
@max_snapshot_age_seconds = @max_snapshot_age_explicit ? max_snapshot_age_seconds : DEFAULT_MAX_SNAPSHOT_AGE_SECONDS
|
|
53
82
|
|
|
54
83
|
validate!
|
|
55
84
|
end
|
|
@@ -76,6 +105,7 @@ module Featureflip
|
|
|
76
105
|
def validate!
|
|
77
106
|
finalize!
|
|
78
107
|
validate_positive_fields!
|
|
108
|
+
validate_snapshot_options!
|
|
79
109
|
end
|
|
80
110
|
|
|
81
111
|
private
|
|
@@ -93,6 +123,17 @@ module Featureflip
|
|
|
93
123
|
end
|
|
94
124
|
end
|
|
95
125
|
|
|
126
|
+
def validate_snapshot_options!
|
|
127
|
+
age = @max_snapshot_age_seconds
|
|
128
|
+
valid = age.is_a?(Numeric) && !age.is_a?(Complex) && age.to_f.finite? && age.positive?
|
|
129
|
+
unless valid
|
|
130
|
+
raise ConfigurationError, "max_snapshot_age_seconds must be a positive, finite number, got #{age.inspect}"
|
|
131
|
+
end
|
|
132
|
+
return if @snapshot_store.nil? || (@snapshot_store.respond_to?(:get) && @snapshot_store.respond_to?(:put))
|
|
133
|
+
|
|
134
|
+
raise ConfigurationError, "snapshot_store must respond to #get and #put"
|
|
135
|
+
end
|
|
136
|
+
|
|
96
137
|
def default_logger
|
|
97
138
|
if defined?(Rails) && Rails.respond_to?(:logger)
|
|
98
139
|
Rails.logger
|
|
@@ -36,7 +36,7 @@ module Featureflip
|
|
|
36
36
|
RECONNECT_BASE_DELAY_SECONDS = 1
|
|
37
37
|
MAX_BACKOFF_SECONDS = 30
|
|
38
38
|
|
|
39
|
-
def initialize(sdk_key:, config:, http_client:, on_flag_updated:, on_flag_deleted:, on_segment_updated:, on_error:, on_sync: nil, on_fallback_to_polling: nil, on_recovered: nil)
|
|
39
|
+
def initialize(sdk_key:, config:, http_client:, on_flag_updated:, on_flag_deleted:, on_segment_updated:, on_error:, on_sync: nil, on_fallback_to_polling: nil, on_recovered: nil, on_ping: nil)
|
|
40
40
|
@sdk_key = sdk_key
|
|
41
41
|
@config = config
|
|
42
42
|
@http_client = http_client
|
|
@@ -47,6 +47,7 @@ module Featureflip
|
|
|
47
47
|
@on_sync = on_sync
|
|
48
48
|
@on_fallback_to_polling = on_fallback_to_polling
|
|
49
49
|
@on_recovered = on_recovered
|
|
50
|
+
@on_ping = on_ping
|
|
50
51
|
@stop_flag = false
|
|
51
52
|
@thread = nil
|
|
52
53
|
@retry_count = 0
|
|
@@ -283,6 +284,9 @@ module Featureflip
|
|
|
283
284
|
# re-synced. Full replace, never a per-key merge.
|
|
284
285
|
flags, segments = @http_client.parse_flags_response(JSON.parse(data))
|
|
285
286
|
@on_sync&.call(flags, segments)
|
|
287
|
+
when "ping"
|
|
288
|
+
# The server's keep-alive: the stream is live and healthy.
|
|
289
|
+
@on_ping&.call
|
|
286
290
|
end
|
|
287
291
|
rescue UnevaluableEntityError => e
|
|
288
292
|
# Not a malformed payload: the frame was well-formed and simply described
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
module Featureflip
|
|
2
|
+
# Strips the SDK key out of anything the SDK logs (#5167).
|
|
3
|
+
#
|
|
4
|
+
# Failure paths report the underlying exception verbatim, so a config-load failure is
|
|
5
|
+
# never silent. That exception can come from a transport the SDK does not control, and
|
|
6
|
+
# the SDK sets the key as the Authorization header on every request: a client that
|
|
7
|
+
# quotes request headers in its message would hand the credential to whatever the host
|
|
8
|
+
# logs to (#5152 was the same leak from a call site).
|
|
9
|
+
#
|
|
10
|
+
# Applied once, as a decorator around the resolved logger when a core is built, so it
|
|
11
|
+
# covers every call site that exists today and any added later, and the default
|
|
12
|
+
# logger too -- stdout is no safer a place for a credential than an aggregator.
|
|
13
|
+
#
|
|
14
|
+
# @api private
|
|
15
|
+
class RedactingLogger
|
|
16
|
+
REPLACEMENT = "[redacted]".freeze
|
|
17
|
+
|
|
18
|
+
# Below this the value is not treated as a credential. A real key is always 54
|
|
19
|
+
# characters; redacting a short one corrupts every line it appears inside (with the
|
|
20
|
+
# key "k", "skipped" becomes "s[redacted]ipped"). A string too short to be a
|
|
21
|
+
# Featureflip key cannot authenticate, so there is nothing to protect.
|
|
22
|
+
MIN_REDACTABLE_LENGTH = 20
|
|
23
|
+
|
|
24
|
+
# Independent of any logger, so the exceptions the SDK raises can be scrubbed even
|
|
25
|
+
# when the host configured no logger.
|
|
26
|
+
def self.redact(text, secret)
|
|
27
|
+
secret = secret.to_s
|
|
28
|
+
return text unless text.is_a?(String) && secret.length >= MIN_REDACTABLE_LENGTH
|
|
29
|
+
|
|
30
|
+
text.gsub(secret, REPLACEMENT)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.wrap(logger, secret)
|
|
34
|
+
return logger if logger.nil? || logger.is_a?(RedactingLogger)
|
|
35
|
+
|
|
36
|
+
new(logger, secret)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def initialize(inner, secret)
|
|
40
|
+
@inner = inner
|
|
41
|
+
@secret = secret.to_s
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
%i[debug info warn error fatal unknown].each do |name|
|
|
45
|
+
define_method(name) do |message = nil, &block|
|
|
46
|
+
if block
|
|
47
|
+
@inner.public_send(name, redact_object(message)) { redact_object(block.call) }
|
|
48
|
+
else
|
|
49
|
+
@inner.public_send(name, redact_object(message))
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def add(severity, message = nil, progname = nil, &block)
|
|
55
|
+
if block
|
|
56
|
+
@inner.add(severity, redact_object(message), redact_object(progname)) { redact_object(block.call) }
|
|
57
|
+
else
|
|
58
|
+
@inner.add(severity, redact_object(message), redact_object(progname))
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
alias_method :log, :add
|
|
62
|
+
|
|
63
|
+
def <<(message)
|
|
64
|
+
@inner << redact_object(message)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def progname
|
|
68
|
+
redact_object(@inner.progname)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def method_missing(name, *args, **kwargs, &block)
|
|
72
|
+
return super unless @inner.respond_to?(name)
|
|
73
|
+
|
|
74
|
+
@inner.public_send(name, *args, **kwargs, &block)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def respond_to_missing?(name, include_private = false)
|
|
78
|
+
@inner.respond_to?(name) || super
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def redact(text)
|
|
82
|
+
self.class.redact(text, @secret)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def redactable?
|
|
88
|
+
@secret.length >= MIN_REDACTABLE_LENGTH
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def redact_object(object)
|
|
92
|
+
return object unless redactable?
|
|
93
|
+
|
|
94
|
+
case object
|
|
95
|
+
when String then redact(object)
|
|
96
|
+
when Exception then redact_exception(object)
|
|
97
|
+
when nil, Symbol, Numeric, true, false then object
|
|
98
|
+
else
|
|
99
|
+
# The logger would render this with #inspect; only touch it when the key is in it.
|
|
100
|
+
# Skipped entirely when the logger would discard the line anyway.
|
|
101
|
+
return object unless @inner.respond_to?(:level) && @inner.level.to_i <= Logger::FATAL
|
|
102
|
+
|
|
103
|
+
text = object.inspect
|
|
104
|
+
text.include?(@secret) ? redact(text) : object
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Logger's formatter prints "message (Class)" plus the backtrace. A clean exception
|
|
109
|
+
# is passed through as the original object (structured consumers keep it); a dirty
|
|
110
|
+
# one is rendered from the redacted message so the sink never sees the original.
|
|
111
|
+
def redact_exception(error)
|
|
112
|
+
message = error.message.to_s
|
|
113
|
+
backtrace = Array(error.backtrace)
|
|
114
|
+
return error unless message.include?(@secret) || backtrace.any? { |l| l.include?(@secret) }
|
|
115
|
+
|
|
116
|
+
lines = ["#{redact(message)} (#{error.class})"]
|
|
117
|
+
lines.concat(backtrace.map { |l| redact(l) })
|
|
118
|
+
lines.join("\n")
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -40,11 +40,12 @@ module Featureflip
|
|
|
40
40
|
if existing
|
|
41
41
|
if existing._acquire
|
|
42
42
|
unless _configs_equal(existing._config, config)
|
|
43
|
-
config.logger&.warn(
|
|
43
|
+
RedactingLogger.wrap(config.logger, sdk_key)&.warn(
|
|
44
44
|
"Featureflip: Client.get called with different config for same SDK key. " \
|
|
45
45
|
"Using existing configuration. Close all handles first to apply new config."
|
|
46
46
|
)
|
|
47
47
|
end
|
|
48
|
+
_warn_snapshot_options_ignored(existing._config, config, sdk_key)
|
|
48
49
|
return existing
|
|
49
50
|
else
|
|
50
51
|
# Stale entry — remove and replace
|
|
@@ -58,6 +59,22 @@ module Featureflip
|
|
|
58
59
|
end
|
|
59
60
|
end
|
|
60
61
|
|
|
62
|
+
# The snapshot store is honored on the first get per SDK key. A later get that
|
|
63
|
+
# passes a different store instance, or an explicit different max age, is told so.
|
|
64
|
+
# An omitted store or max age is not a request, so a wrapper that does not forward
|
|
65
|
+
# them never triggers the warning.
|
|
66
|
+
def self._warn_snapshot_options_ignored(existing, requested, sdk_key)
|
|
67
|
+
store_differs = !requested.snapshot_store.nil? && !requested.snapshot_store.equal?(existing.snapshot_store)
|
|
68
|
+
age_differs = requested.max_snapshot_age_explicit? &&
|
|
69
|
+
requested.max_snapshot_age_seconds != existing.max_snapshot_age_seconds
|
|
70
|
+
return unless store_differs || age_differs
|
|
71
|
+
|
|
72
|
+
RedactingLogger.wrap(requested.logger, sdk_key)&.warn(
|
|
73
|
+
"Featureflip: snapshot_store/max_snapshot_age_seconds option ignored because a client " \
|
|
74
|
+
"for this SDK key already exists. Close all handles first to apply it."
|
|
75
|
+
)
|
|
76
|
+
end
|
|
77
|
+
|
|
61
78
|
def self._create_for_testing(flags)
|
|
62
79
|
core = allocate
|
|
63
80
|
core.send(:init_test_mode, flags)
|
|
@@ -75,9 +92,24 @@ module Featureflip
|
|
|
75
92
|
|
|
76
93
|
# --- Instance methods ---
|
|
77
94
|
|
|
78
|
-
|
|
95
|
+
# Wall and monotonic clocks, injectable so the refresh rule is testable without
|
|
96
|
+
# waiting minutes.
|
|
97
|
+
# @api private
|
|
98
|
+
Clock = Struct.new(:wall_ms, :monotonic)
|
|
99
|
+
SYSTEM_CLOCK = Clock.new(
|
|
100
|
+
-> { (Time.now.to_f * 1000).to_i },
|
|
101
|
+
-> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
102
|
+
).freeze
|
|
103
|
+
|
|
104
|
+
def initialize(sdk_key:, config:, clock: SYSTEM_CLOCK)
|
|
79
105
|
@sdk_key = sdk_key
|
|
80
|
-
@config
|
|
106
|
+
# Every log line the SDK writes goes through @config.logger, so wrapping it here,
|
|
107
|
+
# once, keeps the key out of all of them (#5167). A copy: the caller's Config (and
|
|
108
|
+
# its logger) stay untouched.
|
|
109
|
+
@config = config.dup
|
|
110
|
+
@config.logger = RedactingLogger.wrap(config.logger, sdk_key)
|
|
111
|
+
@clock = clock
|
|
112
|
+
init_snapshot_state
|
|
81
113
|
# Snapshot the (already-filtered) inspector list at construction: config is
|
|
82
114
|
# immutable-after-init from the core's point of view, so the evaluation path
|
|
83
115
|
# needs no locking. Deliberately excluded from _configs_equal -- callables
|
|
@@ -147,6 +179,12 @@ module Featureflip
|
|
|
147
179
|
@initialized
|
|
148
180
|
end
|
|
149
181
|
|
|
182
|
+
# Where the flag data came from: live, a stored snapshot, or nowhere. Moves to
|
|
183
|
+
# live on the first successful full live apply.
|
|
184
|
+
def startup_info
|
|
185
|
+
@startup || StartupInfo.new(Snapshot::SOURCE_LIVE)
|
|
186
|
+
end
|
|
187
|
+
|
|
150
188
|
# --- Evaluation methods ---
|
|
151
189
|
|
|
152
190
|
# The typed accessors declare the JSON type they expect so a mismatch degrades
|
|
@@ -298,6 +336,9 @@ module Featureflip
|
|
|
298
336
|
# would leave a restarted non-streaming core with no data source at all.
|
|
299
337
|
@streaming_handler = nil
|
|
300
338
|
@polling_handler = nil
|
|
339
|
+
# A forked child has no writer thread, so a slot the parent claimed would stay
|
|
340
|
+
# claimed forever and wedge persistence.
|
|
341
|
+
reset_write_slot
|
|
301
342
|
|
|
302
343
|
if @config.streaming
|
|
303
344
|
start_streaming
|
|
@@ -339,7 +380,21 @@ module Featureflip
|
|
|
339
380
|
end
|
|
340
381
|
@event_processor = nil
|
|
341
382
|
|
|
342
|
-
@config.logger&.info("Featureflip: core shut down
|
|
383
|
+
@config.logger&.info("Featureflip: core shut down")
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def redact(text)
|
|
387
|
+
RedactingLogger.redact(text, @sdk_key)
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
# The caller sees the failure's class, message and backtrace with the key scrubbed.
|
|
391
|
+
# The original exception is not chained: its message may quote the Authorization
|
|
392
|
+
# header, and an explicit cause: stops Ruby attaching it as #cause implicitly.
|
|
393
|
+
def raise_sanitized_init_error(error)
|
|
394
|
+
sanitized = RuntimeError.new("#{error.class}: #{redact(error.message.to_s)}")
|
|
395
|
+
sanitized.set_backtrace(error.backtrace&.map { |l| redact(l) })
|
|
396
|
+
raise InitializationError.new("Failed to initialize: #{redact(error.message.to_s)} (#{error.class})"),
|
|
397
|
+
cause: sanitized
|
|
343
398
|
end
|
|
344
399
|
|
|
345
400
|
def bootstrap!
|
|
@@ -349,18 +404,118 @@ module Featureflip
|
|
|
349
404
|
start_event_processor if @config.send_events
|
|
350
405
|
end
|
|
351
406
|
|
|
407
|
+
# Without a snapshot store a failure raises InitializationError, as before. With
|
|
408
|
+
# one, the failure falls back to the stored snapshot (or defaults) and init
|
|
409
|
+
# completes; the data source started afterwards keeps trying and recovers.
|
|
352
410
|
def fetch_initial_flags
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
411
|
+
# Only the fetch is under the timeout. Applying inside it would let a timeout that
|
|
412
|
+
# lands after a successful fetch fall back to a snapshot over live data.
|
|
413
|
+
flags, segments = Timeout.timeout(@config.init_timeout) { @http_client.get_flags }
|
|
414
|
+
# Establishes the baseline; fires nothing (the store has no snapshot yet).
|
|
415
|
+
apply_snapshot(flags, segments)
|
|
416
|
+
@initialized = true
|
|
417
|
+
rescue Timeout::Error => e
|
|
418
|
+
if @config.snapshot_store.nil?
|
|
419
|
+
raise InitializationError, "Initialization timed out after #{@config.init_timeout}s"
|
|
357
420
|
end
|
|
358
|
-
|
|
359
|
-
|
|
421
|
+
|
|
422
|
+
fall_back_to_snapshot(e, timed_out: true)
|
|
360
423
|
rescue InitializationError
|
|
361
424
|
raise
|
|
362
425
|
rescue StandardError => e
|
|
363
|
-
|
|
426
|
+
raise_sanitized_init_error(e) if @config.snapshot_store.nil?
|
|
427
|
+
|
|
428
|
+
fall_back_to_snapshot(e, timed_out: false)
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
def fall_back_to_snapshot(error, timed_out:)
|
|
432
|
+
load_snapshot_fallback(describe_init_failure(error, timed_out))
|
|
433
|
+
@initialized = true
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
# A short, key-free reason the initial fetch failed. A revoked key (HTTP 401/403)
|
|
437
|
+
# must read differently from an unreachable host.
|
|
438
|
+
def describe_init_failure(error, timed_out)
|
|
439
|
+
return "timed out after #{@config.init_timeout}s" if timed_out
|
|
440
|
+
return "HTTP #{error.status}" if error.respond_to?(:status) && error.status.is_a?(Integer)
|
|
441
|
+
|
|
442
|
+
message = redact(error.message.to_s)
|
|
443
|
+
message.empty? ? error.class.name : message
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# Read the store on a thread, bounded by init_timeout. Returns [finished, value].
|
|
447
|
+
# A raising store is a miss (warned). A read still running at the deadline is
|
|
448
|
+
# abandoned and its late result ignored.
|
|
449
|
+
def read_snapshot_store(cause)
|
|
450
|
+
store = @config.snapshot_store
|
|
451
|
+
box = {}
|
|
452
|
+
reader = Thread.new do
|
|
453
|
+
box[:value] = store.get
|
|
454
|
+
rescue StandardError, ScriptError => e
|
|
455
|
+
box[:error] = e
|
|
456
|
+
end
|
|
457
|
+
reader.report_on_exception = false
|
|
458
|
+
unless reader.join(@config.init_timeout)
|
|
459
|
+
@config.logger&.warn(
|
|
460
|
+
"Featureflip: initial flag fetch failed (#{cause}) and the snapshot store read timed out " \
|
|
461
|
+
"after #{@config.init_timeout}s; serving defaults"
|
|
462
|
+
)
|
|
463
|
+
return [false, nil]
|
|
464
|
+
end
|
|
465
|
+
if box.key?(:error)
|
|
466
|
+
@config.logger&.warn(
|
|
467
|
+
"Featureflip: initial flag fetch failed (#{cause}) and the snapshot store read failed: " \
|
|
468
|
+
"#{box[:error].message}; serving defaults"
|
|
469
|
+
)
|
|
470
|
+
return [true, nil]
|
|
471
|
+
end
|
|
472
|
+
[true, box[:value]]
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
# Serve the stored snapshot after a failed initial fetch. Never raises. Applied
|
|
476
|
+
# without persisting, so loading it does not refresh its own fetchedAt. The store
|
|
477
|
+
# hands back opaque text; every check and every reason is decided here.
|
|
478
|
+
def load_snapshot_fallback(cause)
|
|
479
|
+
finished, value = read_snapshot_store(cause)
|
|
480
|
+
return unless finished # the read already warned
|
|
481
|
+
|
|
482
|
+
if value.nil? # a plain miss: no reason
|
|
483
|
+
@config.logger&.warn(
|
|
484
|
+
"Featureflip: initial flag fetch failed (#{cause}); no persisted snapshot; serving defaults"
|
|
485
|
+
)
|
|
486
|
+
return
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
now = @clock.wall_ms.call
|
|
490
|
+
begin
|
|
491
|
+
decoded = Snapshot.decode_envelope(value, @key_hash, now)
|
|
492
|
+
rescue Snapshot::InvalidError => e
|
|
493
|
+
reject_snapshot(cause, Snapshot::REASON_INVALID, e.why)
|
|
494
|
+
return
|
|
495
|
+
end
|
|
496
|
+
age_ms = [0, now - decoded.fetched_at_ms].max
|
|
497
|
+
age_seconds = age_ms / 1000
|
|
498
|
+
limit = @config.max_snapshot_age_seconds
|
|
499
|
+
if age_ms > limit * 1000
|
|
500
|
+
reject_snapshot(cause, Snapshot::REASON_EXPIRED, "#{age_seconds}s old, limit #{limit}s", age_seconds)
|
|
501
|
+
return
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
@store.init(decoded.flags, decoded.segments)
|
|
505
|
+
@startup = StartupInfo.new(Snapshot::SOURCE_SNAPSHOT, Snapshot::REASON_SNAPSHOT, age_seconds)
|
|
506
|
+
@config.logger&.warn(
|
|
507
|
+
"Featureflip: initial flag fetch failed (#{cause}); serving persisted snapshot (#{age_seconds}s old)"
|
|
508
|
+
)
|
|
509
|
+
rescue StandardError => e
|
|
510
|
+
@config.logger&.warn("Featureflip: snapshot load failed: #{e.class}: #{e.message}")
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
def reject_snapshot(cause, reason, why, age_seconds = nil)
|
|
514
|
+
@startup = StartupInfo.new(Snapshot::SOURCE_NONE, reason, age_seconds)
|
|
515
|
+
@config.logger&.warn(
|
|
516
|
+
"Featureflip: initial flag fetch failed (#{cause}); ignoring persisted snapshot " \
|
|
517
|
+
"(#{reason}: #{why}); serving defaults"
|
|
518
|
+
)
|
|
364
519
|
end
|
|
365
520
|
|
|
366
521
|
def start_data_source
|
|
@@ -382,6 +537,7 @@ module Featureflip
|
|
|
382
537
|
on_flag_deleted: ->(key) { apply_flag_delete(key) },
|
|
383
538
|
on_segment_updated: ->(flags, segments) { apply_snapshot(flags, segments) },
|
|
384
539
|
on_sync: ->(flags, segments) { apply_snapshot(flags, segments) },
|
|
540
|
+
on_ping: -> { on_streaming_ping },
|
|
385
541
|
on_error: ->(_err) { },
|
|
386
542
|
on_fallback_to_polling: -> { fallback_to_polling },
|
|
387
543
|
on_recovered: -> { stop_fallback_polling }
|
|
@@ -461,15 +617,185 @@ module Featureflip
|
|
|
461
617
|
# Every data-source write funnels through one of these three so the change
|
|
462
618
|
# set is computed by the store and reported in one place.
|
|
463
619
|
def apply_snapshot(flags, segments)
|
|
464
|
-
|
|
620
|
+
changed = @store.init(flags, segments)
|
|
621
|
+
@startup = StartupInfo.new(Snapshot::SOURCE_LIVE)
|
|
622
|
+
persist_full
|
|
623
|
+
notify_update_listeners(changed)
|
|
465
624
|
end
|
|
466
625
|
|
|
467
626
|
def apply_flag_update(flag)
|
|
468
|
-
|
|
627
|
+
changed = @store.upsert(flag)
|
|
628
|
+
persist_delta
|
|
629
|
+
notify_update_listeners(changed)
|
|
469
630
|
end
|
|
470
631
|
|
|
471
632
|
def apply_flag_delete(key)
|
|
472
|
-
|
|
633
|
+
changed = @store.remove_flag(key)
|
|
634
|
+
persist_delta
|
|
635
|
+
notify_update_listeners(changed)
|
|
636
|
+
end
|
|
637
|
+
|
|
638
|
+
# --- Snapshot persistence ---
|
|
639
|
+
|
|
640
|
+
def init_snapshot_state
|
|
641
|
+
@startup = StartupInfo.new(Snapshot::SOURCE_NONE)
|
|
642
|
+
@key_hash = Snapshot.compute_key_hash(@config.base_url, @sdk_key)
|
|
643
|
+
@snapshot_mutex = Mutex.new
|
|
644
|
+
@persist_in_flight = false
|
|
645
|
+
@persist_queued = false
|
|
646
|
+
@last_persist_at = nil
|
|
647
|
+
@last_persisted_data = nil
|
|
648
|
+
end
|
|
649
|
+
|
|
650
|
+
def snapshot_store
|
|
651
|
+
@config.snapshot_store
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
def snapshot_stopped?
|
|
655
|
+
@closed || @shut_down
|
|
656
|
+
end
|
|
657
|
+
|
|
658
|
+
def within_refresh_window?
|
|
659
|
+
last = @snapshot_mutex.synchronize { @last_persist_at }
|
|
660
|
+
!last.nil? && @clock.monotonic.call - last < Snapshot::REFRESH_INTERVAL_SECONDS
|
|
661
|
+
end
|
|
662
|
+
|
|
663
|
+
def serialize_state
|
|
664
|
+
Snapshot.serialize_flags_payload(@store.all_flags, @store.all_segments)
|
|
665
|
+
end
|
|
666
|
+
|
|
667
|
+
# After a single-flag delta, only once live: never right after a snapshot or
|
|
668
|
+
# none start, or stale data would get a fresh fetchedAt.
|
|
669
|
+
def persist_delta
|
|
670
|
+
persist if !snapshot_store.nil? && @startup.source == Snapshot::SOURCE_LIVE
|
|
671
|
+
end
|
|
672
|
+
|
|
673
|
+
# After a full live apply. Changed content is written at once. An unchanged state
|
|
674
|
+
# is only rewritten once the last write is a refresh interval old.
|
|
675
|
+
def persist_full
|
|
676
|
+
return if snapshot_store.nil? || snapshot_stopped?
|
|
677
|
+
|
|
678
|
+
if within_refresh_window?
|
|
679
|
+
begin
|
|
680
|
+
data = serialize_state
|
|
681
|
+
rescue StandardError => e
|
|
682
|
+
@config.logger&.warn("Featureflip: snapshot persist failed: #{e.class}: #{e.message}")
|
|
683
|
+
return
|
|
684
|
+
end
|
|
685
|
+
return if @snapshot_mutex.synchronize { data == @last_persisted_data }
|
|
686
|
+
end
|
|
687
|
+
persist
|
|
688
|
+
end
|
|
689
|
+
|
|
690
|
+
# The stream heartbeat: while live, refresh the stored fetchedAt at most once per
|
|
691
|
+
# refresh interval, so a healthy streaming client does not let its snapshot age out.
|
|
692
|
+
def on_streaming_ping
|
|
693
|
+
return if snapshot_store.nil? || @startup.source != Snapshot::SOURCE_LIVE
|
|
694
|
+
return if within_refresh_window?
|
|
695
|
+
|
|
696
|
+
persist
|
|
697
|
+
end
|
|
698
|
+
|
|
699
|
+
# Write the full state without blocking evaluation or init: at most one put in
|
|
700
|
+
# flight plus one trailing write, which reads the state when it runs.
|
|
701
|
+
def persist
|
|
702
|
+
return if snapshot_store.nil? || snapshot_stopped?
|
|
703
|
+
|
|
704
|
+
claimed = @snapshot_mutex.synchronize do
|
|
705
|
+
if @persist_in_flight
|
|
706
|
+
@persist_queued = true
|
|
707
|
+
false
|
|
708
|
+
else
|
|
709
|
+
@persist_in_flight = true
|
|
710
|
+
end
|
|
711
|
+
end
|
|
712
|
+
return unless claimed
|
|
713
|
+
|
|
714
|
+
@writer_started = false
|
|
715
|
+
begin
|
|
716
|
+
text = build_snapshot_text || finish_persist
|
|
717
|
+
if text.nil?
|
|
718
|
+
@writer_started = true # finish_persist already released the slot
|
|
719
|
+
return
|
|
720
|
+
end
|
|
721
|
+
|
|
722
|
+
start_write_thread(text)
|
|
723
|
+
rescue StandardError, ScriptError => e
|
|
724
|
+
# Can't start a thread. Don't leave the in-flight flag set, or every later
|
|
725
|
+
# write is wedged, and never let this escape into an apply.
|
|
726
|
+
@config.logger&.warn("Featureflip: snapshot persist failed: #{e.class}: #{e.message}")
|
|
727
|
+
ensure
|
|
728
|
+
# Also covers an Exception that is not a StandardError (a Timeout or stream
|
|
729
|
+
# stop landing here), which propagates after the slot is released.
|
|
730
|
+
reset_write_slot unless @writer_started
|
|
731
|
+
end
|
|
732
|
+
end
|
|
733
|
+
|
|
734
|
+
def reset_write_slot
|
|
735
|
+
@snapshot_mutex.synchronize do
|
|
736
|
+
@persist_in_flight = false
|
|
737
|
+
@persist_queued = false
|
|
738
|
+
end
|
|
739
|
+
end
|
|
740
|
+
|
|
741
|
+
def start_write_thread(text)
|
|
742
|
+
thread = Thread.new { persist_worker(text) }
|
|
743
|
+
# Marked the instant the thread exists: from here the writer owns the slot, so an
|
|
744
|
+
# exception landing later must not release it (that would allow a second writer).
|
|
745
|
+
@writer_started = true
|
|
746
|
+
silence_thread_report(thread)
|
|
747
|
+
thread
|
|
748
|
+
end
|
|
749
|
+
|
|
750
|
+
def silence_thread_report(thread)
|
|
751
|
+
thread.report_on_exception = false
|
|
752
|
+
end
|
|
753
|
+
|
|
754
|
+
# The envelope for the current state, stamped now. Never raises.
|
|
755
|
+
def build_snapshot_text
|
|
756
|
+
fetched_at = @clock.wall_ms.call
|
|
757
|
+
data = serialize_state
|
|
758
|
+
@snapshot_mutex.synchronize do
|
|
759
|
+
@last_persist_at = @clock.monotonic.call
|
|
760
|
+
@last_persisted_data = data
|
|
761
|
+
end
|
|
762
|
+
Snapshot.encode_envelope(@key_hash, fetched_at, data)
|
|
763
|
+
rescue StandardError => e
|
|
764
|
+
@config.logger&.warn("Featureflip: snapshot persist failed: #{e.class}: #{e.message}")
|
|
765
|
+
nil
|
|
766
|
+
end
|
|
767
|
+
|
|
768
|
+
# End one write. Returns the next envelope if a trailing write is due. No trailing
|
|
769
|
+
# write starts once the core is shut down.
|
|
770
|
+
def finish_persist
|
|
771
|
+
loop do
|
|
772
|
+
proceed = @snapshot_mutex.synchronize do
|
|
773
|
+
if @persist_queued && !snapshot_stopped?
|
|
774
|
+
@persist_queued = false
|
|
775
|
+
true
|
|
776
|
+
else
|
|
777
|
+
@persist_queued = false
|
|
778
|
+
@persist_in_flight = false
|
|
779
|
+
false
|
|
780
|
+
end
|
|
781
|
+
end
|
|
782
|
+
return nil unless proceed
|
|
783
|
+
|
|
784
|
+
text = build_snapshot_text
|
|
785
|
+
return text unless text.nil?
|
|
786
|
+
end
|
|
787
|
+
end
|
|
788
|
+
|
|
789
|
+
def persist_worker(text)
|
|
790
|
+
store = snapshot_store
|
|
791
|
+
until text.nil?
|
|
792
|
+
begin
|
|
793
|
+
store.put(text)
|
|
794
|
+
rescue StandardError, ScriptError => e
|
|
795
|
+
@config.logger&.warn("Featureflip: snapshot store write failed: #{e.class}: #{e.message}")
|
|
796
|
+
end
|
|
797
|
+
text = finish_persist
|
|
798
|
+
end
|
|
473
799
|
end
|
|
474
800
|
|
|
475
801
|
# Runs on the streaming/polling thread, OUTSIDE the store lock: listeners are
|
|
@@ -601,6 +927,9 @@ module Featureflip
|
|
|
601
927
|
def init_test_mode(flags)
|
|
602
928
|
@sdk_key = "test-key"
|
|
603
929
|
@config = Config.new
|
|
930
|
+
@clock = SYSTEM_CLOCK
|
|
931
|
+
init_snapshot_state
|
|
932
|
+
@startup = StartupInfo.new(Snapshot::SOURCE_LIVE)
|
|
604
933
|
@inspectors = []
|
|
605
934
|
@store = Store::FlagStore.new
|
|
606
935
|
@evaluator = Evaluation::Evaluator.new(logger: @config.logger)
|
|
@@ -633,6 +962,6 @@ module Featureflip
|
|
|
633
962
|
a.send_events == b.send_events
|
|
634
963
|
end
|
|
635
964
|
|
|
636
|
-
private_class_method :_configs_equal
|
|
965
|
+
private_class_method :_configs_equal, :_warn_snapshot_options_ignored
|
|
637
966
|
end
|
|
638
967
|
end
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
require "digest"
|
|
2
|
+
require "json"
|
|
3
|
+
require "logger"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
|
|
6
|
+
module Featureflip
|
|
7
|
+
# Where the client's flag data came from at startup.
|
|
8
|
+
#
|
|
9
|
+
# source: Snapshot::SOURCE_LIVE ("live") after a successful fetch from the
|
|
10
|
+
# Evaluation API, Snapshot::SOURCE_SNAPSHOT ("snapshot") when started from the
|
|
11
|
+
# snapshot store, Snapshot::SOURCE_NONE ("none") when there is no data yet
|
|
12
|
+
# (caller defaults are served).
|
|
13
|
+
# reason: "snapshot" for a snapshot start, "snapshot-expired" or
|
|
14
|
+
# "snapshot-invalid" when a stored snapshot was ignored. nil once the client is
|
|
15
|
+
# live, or when nothing was stored.
|
|
16
|
+
# snapshot_age_seconds: age of the snapshot that was used or rejected as
|
|
17
|
+
# expired, in whole seconds.
|
|
18
|
+
StartupInfo = Struct.new(:source, :reason, :snapshot_age_seconds) do
|
|
19
|
+
def initialize(source, reason = nil, snapshot_age_seconds = nil)
|
|
20
|
+
super
|
|
21
|
+
freeze
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Opaque text store for the last good flag snapshot (a duck type, not a base
|
|
26
|
+
# class). Implement two methods:
|
|
27
|
+
#
|
|
28
|
+
# get -> the String last stored, or nil if nothing is stored
|
|
29
|
+
# put(text) -> store the String, replacing any previous value
|
|
30
|
+
#
|
|
31
|
+
# Both may be called from a background thread. A store that raises is handled: a
|
|
32
|
+
# failing get is a miss and a failing put is a dropped write, each with a logged
|
|
33
|
+
# warning. The SDK owns the stored text end to end (envelope, environment check,
|
|
34
|
+
# age check and every startup reason), so one store instance may safely be given
|
|
35
|
+
# to several SDK keys.
|
|
36
|
+
#
|
|
37
|
+
# Everything on Snapshot except the constants is internal (@api private): the
|
|
38
|
+
# public surface is the store contract above, FileSnapshotStore, StartupInfo and
|
|
39
|
+
# Config#snapshot_store.
|
|
40
|
+
module Snapshot
|
|
41
|
+
# StartupInfo#source values.
|
|
42
|
+
SOURCE_LIVE = "live".freeze
|
|
43
|
+
SOURCE_SNAPSHOT = "snapshot".freeze
|
|
44
|
+
SOURCE_NONE = "none".freeze
|
|
45
|
+
|
|
46
|
+
# StartupInfo#reason values.
|
|
47
|
+
REASON_SNAPSHOT = "snapshot".freeze
|
|
48
|
+
REASON_EXPIRED = "snapshot-expired".freeze
|
|
49
|
+
REASON_INVALID = "snapshot-invalid".freeze
|
|
50
|
+
|
|
51
|
+
# How far in the future a stored fetchedAt may be (clock skew between the writer
|
|
52
|
+
# and the reader) and still be accepted, with its age clamped to 0.
|
|
53
|
+
FUTURE_FETCHED_AT_TOLERANCE_MS = 60_000
|
|
54
|
+
|
|
55
|
+
# While live, an unchanged state is rewritten (refreshing fetchedAt) at most
|
|
56
|
+
# this often. Changed content is never throttled.
|
|
57
|
+
REFRESH_INTERVAL_SECONDS = 300.0
|
|
58
|
+
|
|
59
|
+
# Stored text failed a check. +why+ names the check, never the contents or key.
|
|
60
|
+
class InvalidError < StandardError
|
|
61
|
+
attr_reader :why
|
|
62
|
+
|
|
63
|
+
def initialize(why)
|
|
64
|
+
super(why)
|
|
65
|
+
@why = why
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
Decoded = Struct.new(:flags, :segments, :fetched_at_ms, keyword_init: true)
|
|
70
|
+
|
|
71
|
+
NullConfig = Struct.new(:logger, :base_url)
|
|
72
|
+
private_constant :NullConfig
|
|
73
|
+
|
|
74
|
+
# Identify the (base URL, SDK key) pair without storing the key.
|
|
75
|
+
def self.compute_key_hash(base_url, sdk_key)
|
|
76
|
+
Digest::SHA256.hexdigest("#{base_url.to_s.sub(%r{/+\z}, "")}\n#{sdk_key}")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The stored text, from an already serialized +data+.
|
|
80
|
+
def self.encode_envelope(key_hash, fetched_at_ms, data_text)
|
|
81
|
+
"{\"v\":1,\"keyHash\":#{JSON.generate(key_hash)},\"fetchedAt\":#{Integer(fetched_at_ms)},\"data\":#{data_text}}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Parse and check stored text. Age is not checked here.
|
|
85
|
+
# Raises InvalidError for any failed check, in the pinned order.
|
|
86
|
+
def self.decode_envelope(text, key_hash, now_ms)
|
|
87
|
+
raise InvalidError, "store returned a non-string value" unless text.is_a?(String)
|
|
88
|
+
|
|
89
|
+
begin
|
|
90
|
+
env = JSON.parse(text)
|
|
91
|
+
rescue StandardError
|
|
92
|
+
raise InvalidError, text.empty? ? "empty text" : "not valid JSON"
|
|
93
|
+
end
|
|
94
|
+
raise InvalidError, "not a snapshot envelope" unless env.is_a?(Hash)
|
|
95
|
+
|
|
96
|
+
version = env["v"]
|
|
97
|
+
unless version.is_a?(Numeric) && version == 1
|
|
98
|
+
raise InvalidError, "unsupported envelope version"
|
|
99
|
+
end
|
|
100
|
+
raise InvalidError, "written for a different SDK key or base URL" unless env["keyHash"] == key_hash
|
|
101
|
+
|
|
102
|
+
fetched_at = positive_integer(env["fetchedAt"])
|
|
103
|
+
raise InvalidError, "fetchedAt is not a positive integer" if fetched_at.nil?
|
|
104
|
+
raise InvalidError, "fetchedAt is in the future" if fetched_at > now_ms + FUTURE_FETCHED_AT_TOLERANCE_MS
|
|
105
|
+
|
|
106
|
+
data = env["data"]
|
|
107
|
+
raise InvalidError, "missing data" unless data.is_a?(Hash)
|
|
108
|
+
|
|
109
|
+
begin
|
|
110
|
+
raise ArgumentError, "flags and segments must be arrays" unless data["flags"].is_a?(Array) && data["segments"].is_a?(Array)
|
|
111
|
+
|
|
112
|
+
flags, segments = payload_parser.parse_flags_response(data)
|
|
113
|
+
rescue StandardError
|
|
114
|
+
raise InvalidError, "data failed payload validation"
|
|
115
|
+
end
|
|
116
|
+
Decoded.new(flags: flags, segments: segments, fetched_at_ms: fetched_at)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def self.positive_integer(value)
|
|
120
|
+
case value
|
|
121
|
+
when Integer then value.positive? ? value : nil
|
|
122
|
+
when Float then value.finite? && value == value.floor && value.positive? ? value.to_i : nil
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# The SDK's own live-payload parser (so the same wholesale-discard validation
|
|
127
|
+
# and unevaluable-entity drops apply), with logging off.
|
|
128
|
+
def self.payload_parser
|
|
129
|
+
Http::Client.new(nil, NullConfig.new(nil, ""))
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# --- Serialization (in-memory state back to the /v1/sdk/flags wire shape) ---
|
|
133
|
+
#
|
|
134
|
+
# The models hold the wire spelling already (Boolean/Fixed/Rollout, And/Or, ...;
|
|
135
|
+
# an unknown value is kept verbatim), so this only renames fields. Flags and
|
|
136
|
+
# segments are sorted by key and the property order is fixed: the unchanged-state
|
|
137
|
+
# comparison relies on a deterministic text.
|
|
138
|
+
|
|
139
|
+
def self.serialize_flags_payload(flags, segments)
|
|
140
|
+
JSON.generate(
|
|
141
|
+
"flags" => flags.sort_by { |f| f.key.to_s }.map { |f| wire_flag(f) },
|
|
142
|
+
"segments" => segments.sort_by { |s| s.key.to_s }.map { |s| wire_segment(s) }
|
|
143
|
+
)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def self.wire_condition(c)
|
|
147
|
+
{ "attribute" => c.attribute, "operator" => c.operator, "values" => c.values, "negate" => c.negate }
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def self.wire_group(g)
|
|
151
|
+
{ "operator" => g.operator, "conditions" => g.conditions.map { |c| wire_condition(c) } }
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def self.wire_serve(s)
|
|
155
|
+
out = { "type" => s.type }
|
|
156
|
+
out["variation"] = s.variation unless s.variation.nil?
|
|
157
|
+
out["bucketBy"] = s.bucket_by unless s.bucket_by.nil?
|
|
158
|
+
out["salt"] = s.salt unless s.salt.nil?
|
|
159
|
+
unless s.variations.nil?
|
|
160
|
+
out["variations"] = s.variations.map { |v| { "key" => v.key, "weight" => v.weight } }
|
|
161
|
+
end
|
|
162
|
+
out
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def self.wire_rule(r)
|
|
166
|
+
out = {
|
|
167
|
+
"id" => r.id,
|
|
168
|
+
"priority" => r.priority,
|
|
169
|
+
"conditionGroups" => r.condition_groups.map { |g| wire_group(g) },
|
|
170
|
+
"serve" => wire_serve(r.serve)
|
|
171
|
+
}
|
|
172
|
+
out["segmentKey"] = r.segment_key unless r.segment_key.nil?
|
|
173
|
+
out
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def self.wire_flag(f)
|
|
177
|
+
{
|
|
178
|
+
"key" => f.key,
|
|
179
|
+
"version" => f.version,
|
|
180
|
+
"type" => f.type,
|
|
181
|
+
"enabled" => f.enabled,
|
|
182
|
+
"variations" => f.variations.map { |v| { "key" => v.key, "value" => v.value } },
|
|
183
|
+
"rules" => f.rules.map { |r| wire_rule(r) },
|
|
184
|
+
"fallthrough" => wire_serve(f.fallthrough),
|
|
185
|
+
"offVariation" => f.off_variation,
|
|
186
|
+
"prerequisites" => f.prerequisites.map do |p|
|
|
187
|
+
{ "prerequisiteFlagKey" => p.prerequisite_flag_key, "expectedVariationKey" => p.expected_variation_key }
|
|
188
|
+
end
|
|
189
|
+
}
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def self.wire_segment(s)
|
|
193
|
+
{
|
|
194
|
+
"key" => s.key,
|
|
195
|
+
"version" => s.version,
|
|
196
|
+
"conditions" => s.conditions.map { |c| wire_condition(c) },
|
|
197
|
+
"conditionLogic" => s.condition_logic
|
|
198
|
+
}
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
private_class_method :positive_integer, :payload_parser, :wire_condition, :wire_group,
|
|
202
|
+
:wire_serve, :wire_rule, :wire_flag, :wire_segment
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Persist the snapshot text to one file (mode 0600), written atomically.
|
|
206
|
+
#
|
|
207
|
+
# The store is opaque: it neither parses nor checks what it holds, so one instance
|
|
208
|
+
# can be shared by several SDK keys. It never raises: a failure is logged and
|
|
209
|
+
# treated as a miss (get) or a dropped write (put). The parent directory must
|
|
210
|
+
# already exist; it is never created.
|
|
211
|
+
#
|
|
212
|
+
# There is deliberately no fsync before the rename, so a power loss can leave a
|
|
213
|
+
# zero-length or partial file. The SDK reads that as "snapshot-invalid", logs a
|
|
214
|
+
# warning and serves defaults, and the next successful write replaces it.
|
|
215
|
+
#
|
|
216
|
+
# A write runs on a background thread, so a process killed mid-write can leave a
|
|
217
|
+
# "<name>.<pid>.<hex>.tmp" file next to the snapshot. It is never read.
|
|
218
|
+
#
|
|
219
|
+
# Owner-only permissions are POSIX semantics; on Windows the file mode bits are
|
|
220
|
+
# not enforced and the file inherits the directory's access control list.
|
|
221
|
+
class FileSnapshotStore
|
|
222
|
+
def initialize(path, logger: nil)
|
|
223
|
+
@path = path.to_s
|
|
224
|
+
@logger = logger || ::Logger.new($stderr, level: ::Logger::WARN)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def get
|
|
228
|
+
File.binread(@path).force_encoding(Encoding::UTF_8).scrub
|
|
229
|
+
rescue Errno::ENOENT
|
|
230
|
+
nil
|
|
231
|
+
rescue StandardError => e
|
|
232
|
+
@logger.warn("Featureflip: snapshot file read failed (#{@path}): #{e.class}: #{e.message}")
|
|
233
|
+
nil
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def put(text)
|
|
237
|
+
tmp = "#{@path}.#{Process.pid}.#{SecureRandom.hex(6)}.tmp"
|
|
238
|
+
begin
|
|
239
|
+
File.open(tmp, File::WRONLY | File::CREAT | File::EXCL, 0o600) do |f|
|
|
240
|
+
f.binmode
|
|
241
|
+
f.write(text.to_s.encode(Encoding::UTF_8))
|
|
242
|
+
f.flush
|
|
243
|
+
end
|
|
244
|
+
File.rename(tmp, @path)
|
|
245
|
+
rescue StandardError => e
|
|
246
|
+
@logger.warn("Featureflip: snapshot file write failed (#{@path}): #{e.class}: #{e.message}")
|
|
247
|
+
begin
|
|
248
|
+
File.unlink(tmp)
|
|
249
|
+
rescue StandardError
|
|
250
|
+
# nothing left to clean up
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
nil
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
end
|
data/lib/featureflip/version.rb
CHANGED
data/lib/featureflip.rb
CHANGED
|
@@ -2,6 +2,7 @@ require_relative "featureflip/version"
|
|
|
2
2
|
require_relative "featureflip/errors"
|
|
3
3
|
require_relative "featureflip/config"
|
|
4
4
|
require_relative "featureflip/sdk_identity"
|
|
5
|
+
require_relative "featureflip/redacting_logger"
|
|
5
6
|
require_relative "featureflip/models/flag"
|
|
6
7
|
require_relative "featureflip/models/segment"
|
|
7
8
|
require_relative "featureflip/models/evaluation_detail"
|
|
@@ -12,6 +13,7 @@ require_relative "featureflip/evaluation/evaluator"
|
|
|
12
13
|
require_relative "featureflip/store/update_diff"
|
|
13
14
|
require_relative "featureflip/store/flag_store"
|
|
14
15
|
require_relative "featureflip/http/client"
|
|
16
|
+
require_relative "featureflip/snapshot"
|
|
15
17
|
require_relative "featureflip/events/event"
|
|
16
18
|
require_relative "featureflip/events/event_processor"
|
|
17
19
|
require_relative "featureflip/data_source/streaming"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: featureflip
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.15.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Featureflip
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: logger
|
|
@@ -91,8 +91,10 @@ files:
|
|
|
91
91
|
- lib/featureflip/models/evaluation_event.rb
|
|
92
92
|
- lib/featureflip/models/flag.rb
|
|
93
93
|
- lib/featureflip/models/segment.rb
|
|
94
|
+
- lib/featureflip/redacting_logger.rb
|
|
94
95
|
- lib/featureflip/sdk_identity.rb
|
|
95
96
|
- lib/featureflip/shared_core.rb
|
|
97
|
+
- lib/featureflip/snapshot.rb
|
|
96
98
|
- lib/featureflip/store/flag_store.rb
|
|
97
99
|
- lib/featureflip/store/update_diff.rb
|
|
98
100
|
- lib/featureflip/version.rb
|