Skip to content

Commit 7d5b051

Browse files
authored
fix: Increment flag & segment versions when reloading from file data source (#285)
The file data source allows specifying flag information as a full flag definition, or as a shorted map of flag key:value mappings. In the case of the flag values, or in the case of a malformed flag definition, a flag version might not be specified. When this happens, users of the flag tracker will notice an error because the version comparison code will encounter an unexpected nil value. To prevent this from happening, the file data source should be setting a version for each flag or segment it reads. When these items are modified in the LaunchDarkly UI, we automatically increment the version associated with the item. To make this easier for the user going forward, the file data source will handle incrementing this version number each time the file is re-read.
1 parent d3c8d40 commit 7d5b051

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/ldclient-rb/impl/integrations/file_data_source.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def initialize(data_store, data_source_update_sink, logger, options={})
4040
@poll_interval = options[:poll_interval] || 1
4141
@initialized = Concurrent::AtomicBoolean.new(false)
4242
@ready = Concurrent::Event.new
43+
44+
@version_lock = Mutex.new
45+
@last_version = 1
4346
end
4447

4548
def initialized?
@@ -93,14 +96,22 @@ def load_all
9396
end
9497

9598
def load_file(path, all_data)
99+
version = 1
100+
@version_lock.synchronize {
101+
version = @last_version
102+
@last_version += 1
103+
}
104+
96105
parsed = parse_content(IO.read(path))
97106
(parsed[:flags] || {}).each do |key, flag|
107+
flag[:version] = version
98108
add_item(all_data, FEATURES, flag)
99109
end
100110
(parsed[:flagValues] || {}).each do |key, value|
101-
add_item(all_data, FEATURES, make_flag_with_value(key.to_s, value))
111+
add_item(all_data, FEATURES, make_flag_with_value(key.to_s, value, version))
102112
end
103113
(parsed[:segments] || {}).each do |key, segment|
114+
segment[:version] = version
104115
add_item(all_data, SEGMENTS, segment)
105116
end
106117
end
@@ -134,10 +145,11 @@ def add_item(all_data, kind, item)
134145
items[key] = Model.deserialize(kind, item)
135146
end
136147

137-
def make_flag_with_value(key, value)
148+
def make_flag_with_value(key, value, version)
138149
{
139150
key: key,
140151
on: true,
152+
version: version,
141153
fallthrough: { variation: 0 },
142154
variations: [ value ],
143155
}

0 commit comments

Comments
 (0)