flagnetic 0.1.1 → 0.1.3
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/README.md +13 -8
- data/lib/flagnetic/client.rb +10 -6
- data/lib/flagnetic/eval/evaluation.rb +37 -6
- data/lib/flagnetic/eval/regex.rb +141 -159
- data/lib/flagnetic/eval/regex_vm.rb +356 -0
- data/lib/flagnetic/eval/ruleset.rb +9 -0
- data/lib/flagnetic/eval/unicode_tables.rb +17 -0
- data/lib/flagnetic/event_batch.rb +33 -1
- data/lib/flagnetic/exposures.rb +6 -2
- data/lib/flagnetic/fallback_chain.rb +24 -5
- data/lib/flagnetic/logging.rb +3 -3
- data/lib/flagnetic/railtie.rb +18 -2
- data/lib/flagnetic/recorder.rb +32 -15
- data/lib/flagnetic/reporter.rb +7 -0
- data/lib/flagnetic/sse_parser.rb +19 -4
- data/lib/flagnetic/store.rb +65 -10
- data/lib/flagnetic/summaries.rb +4 -2
- data/lib/flagnetic/transport.rb +52 -7
- data/lib/flagnetic/version.rb +1 -1
- data/lib/flagnetic/worker.rb +21 -7
- metadata +2 -1
data/lib/flagnetic/sse_parser.rb
CHANGED
|
@@ -12,15 +12,18 @@ module Flagnetic
|
|
|
12
12
|
# last event ID seen, or nil.
|
|
13
13
|
Event = Struct.new(:type, :data, :id, keyword_init: true)
|
|
14
14
|
|
|
15
|
-
# Error is a stream the parser refuses: a line longer than max_line
|
|
15
|
+
# Error is a stream the parser refuses: a line longer than max_line, or
|
|
16
|
+
# an event whose data grows past max_event. A peer, or a proxy in
|
|
17
|
+
# between, controls the bytes, so neither may grow without bound.
|
|
16
18
|
class Error < StandardError; end
|
|
17
19
|
|
|
18
20
|
BOM = "\xEF\xBB\xBF".b.freeze
|
|
19
21
|
LINE_END = /[\r\n]/n
|
|
20
22
|
private_constant :BOM, :LINE_END
|
|
21
23
|
|
|
22
|
-
def initialize(max_line:
|
|
24
|
+
def initialize(max_line: 64 << 10, max_event: 64 << 10)
|
|
23
25
|
@max_line = max_line
|
|
26
|
+
@max_event = max_event
|
|
24
27
|
@buffer = +"".b
|
|
25
28
|
@skip_lf = false
|
|
26
29
|
@started = false
|
|
@@ -40,15 +43,20 @@ module Flagnetic
|
|
|
40
43
|
@buffer << bytes
|
|
41
44
|
strip_bom
|
|
42
45
|
while (i = @buffer.index(LINE_END))
|
|
46
|
+
# A line that arrives whole in one read is as long as one that
|
|
47
|
+
# arrives in pieces, so it is held to the same limit.
|
|
48
|
+
raise_line_too_long if i > @max_line
|
|
43
49
|
line = @buffer.byteslice(0, i)
|
|
44
50
|
@buffer = @buffer.byteslice((i + line_end_length(i))..)
|
|
45
51
|
line(line.force_encoding(Encoding::UTF_8).scrub, &)
|
|
46
52
|
end
|
|
47
|
-
|
|
53
|
+
raise_line_too_long if @buffer.bytesize > @max_line
|
|
48
54
|
end
|
|
49
55
|
|
|
50
56
|
private
|
|
51
57
|
|
|
58
|
+
def raise_line_too_long = raise(Error, "an event-stream line exceeds #{@max_line} bytes")
|
|
59
|
+
|
|
52
60
|
def strip_bom
|
|
53
61
|
return if @started || @buffer.bytesize < BOM.bytesize
|
|
54
62
|
|
|
@@ -74,12 +82,19 @@ module Flagnetic
|
|
|
74
82
|
value = value.to_s.delete_prefix(" ")
|
|
75
83
|
case field
|
|
76
84
|
when "event" then @type = value
|
|
77
|
-
when "data" then
|
|
85
|
+
when "data" then append_data(value)
|
|
78
86
|
when "id" then @id = value unless value.include?("\u0000")
|
|
79
87
|
when "retry" then yield :retry, value.to_i if value.match?(/\A\d+\z/)
|
|
80
88
|
end
|
|
81
89
|
end
|
|
82
90
|
|
|
91
|
+
# append_data adds one data line. Each line may be short and the
|
|
92
|
+
# event still never end, so the event's data is bounded as well.
|
|
93
|
+
def append_data(value)
|
|
94
|
+
@data << value << "\n"
|
|
95
|
+
raise Error, "an event-stream event exceeds #{@max_event} bytes" if @data.bytesize > @max_event
|
|
96
|
+
end
|
|
97
|
+
|
|
83
98
|
def dispatch
|
|
84
99
|
data = @data
|
|
85
100
|
type = @type
|
data/lib/flagnetic/store.rb
CHANGED
|
@@ -1,34 +1,89 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
|
|
3
7
|
module Flagnetic
|
|
4
|
-
# FileStore keeps the last good ruleset in one file:
|
|
5
|
-
#
|
|
6
|
-
#
|
|
8
|
+
# FileStore keeps the last good ruleset in one file: a header line naming
|
|
9
|
+
# the key the ruleset was received under, then the ruleset document exactly
|
|
10
|
+
# as the endpoint served it. The header holds a digest of the SDK key, never
|
|
11
|
+
# the key. Without it, a process restarted with another environment's key on
|
|
12
|
+
# the same path would serve that environment's ruleset as last_good, and
|
|
13
|
+
# refuse the right one as a downgrade whenever the old version was higher.
|
|
14
|
+
# The file's modification time is when the ruleset was received.
|
|
7
15
|
# Writes go to a unique temporary file renamed over the target, so
|
|
8
16
|
# processes sharing the path (Puma workers) never read a torn file.
|
|
9
17
|
class FileStore
|
|
10
|
-
|
|
18
|
+
FORMAT = 1
|
|
19
|
+
|
|
20
|
+
# Refused is raised for a file this client may not serve from: one written
|
|
21
|
+
# under no key, under another key or in another format.
|
|
22
|
+
class Refused < StandardError; end
|
|
23
|
+
|
|
24
|
+
def initialize(path, sdk_key)
|
|
11
25
|
@path = path.to_s
|
|
26
|
+
@digest = Digest::SHA256.hexdigest(sdk_key.to_s)
|
|
12
27
|
end
|
|
13
28
|
|
|
14
|
-
# load returns [json, received_at], or nil when there is no file.
|
|
29
|
+
# load returns [json, received_at], or nil when there is no file. It
|
|
30
|
+
# raises Refused for a file written under no key or another key.
|
|
15
31
|
def load
|
|
16
|
-
|
|
17
|
-
|
|
32
|
+
content = File.binread(@path)
|
|
33
|
+
received_at = File.mtime(@path)
|
|
34
|
+
[document_of(content), received_at]
|
|
18
35
|
rescue Errno::ENOENT
|
|
19
36
|
nil
|
|
20
37
|
end
|
|
21
38
|
|
|
39
|
+
# save writes json under the header. The temporary file sits beside the
|
|
40
|
+
# target, so the rename stays on one file system, under a random name
|
|
41
|
+
# created exclusively: anyone else able to write to the directory can
|
|
42
|
+
# neither guess the name nor plant a file or symlink there for this
|
|
43
|
+
# write to follow, and O_EXCL refuses to open what already exists.
|
|
22
44
|
def save(json)
|
|
23
|
-
tmp = "#{@path}.#{Process.pid}.#{
|
|
45
|
+
tmp = "#{@path}.#{Process.pid}.#{SecureRandom.hex(8)}.tmp"
|
|
46
|
+
created = false
|
|
24
47
|
# The server ruleset holds private segments: readable by the owner only.
|
|
25
|
-
File.open(tmp, File::WRONLY | File::CREAT | File::
|
|
48
|
+
File.open(tmp, File::WRONLY | File::CREAT | File::EXCL | File::BINARY, 0o600) do |f|
|
|
49
|
+
created = true
|
|
50
|
+
f.write(header, "\n", json)
|
|
51
|
+
end
|
|
26
52
|
File.rename(tmp, @path)
|
|
53
|
+
created = false
|
|
27
54
|
ensure
|
|
28
|
-
|
|
55
|
+
# Only a file this call created is removed: after a failed exclusive
|
|
56
|
+
# open the name belongs to someone else.
|
|
57
|
+
File.delete(tmp) if created && File.exist?(tmp)
|
|
29
58
|
end
|
|
30
59
|
|
|
31
60
|
def to_s = @path
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def header = JSON.generate({ "flagnetic_store" => FORMAT, "key" => @digest })
|
|
65
|
+
|
|
66
|
+
def document_of(content)
|
|
67
|
+
line, newline, document = content.partition("\n".b)
|
|
68
|
+
head = newline.empty? ? nil : parse_header(line)
|
|
69
|
+
raise Refused, "the store was written under no key" unless head.is_a?(Hash) && head.key?("flagnetic_store")
|
|
70
|
+
|
|
71
|
+
unless format?(head["flagnetic_store"])
|
|
72
|
+
raise Refused,
|
|
73
|
+
"the store was written in a format this client does not read"
|
|
74
|
+
end
|
|
75
|
+
raise Refused, "the store was written under another key" unless head["key"] == @digest
|
|
76
|
+
|
|
77
|
+
document
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def format?(value) = value.is_a?(Integer) && value == FORMAT
|
|
81
|
+
|
|
82
|
+
def parse_header(line)
|
|
83
|
+
JSON.parse(line.dup.force_encoding(Encoding::UTF_8))
|
|
84
|
+
rescue JSON::ParserError, EncodingError
|
|
85
|
+
nil
|
|
86
|
+
end
|
|
32
87
|
end
|
|
33
88
|
private_constant :FileStore
|
|
34
89
|
end
|
data/lib/flagnetic/summaries.rb
CHANGED
|
@@ -155,7 +155,7 @@ module Flagnetic
|
|
|
155
155
|
end
|
|
156
156
|
|
|
157
157
|
wire_events = events.map { |e| wire(e) }
|
|
158
|
-
batches = EventBatch.pack(wire_events, @transport.identity)
|
|
158
|
+
batches = EventBatch.pack(wire_events, @transport.identity) { |_, err| report_unserialisable(err) }
|
|
159
159
|
batches.each_with_index do |group, i|
|
|
160
160
|
result = attempt(group, deadline)
|
|
161
161
|
case result.kind
|
|
@@ -188,6 +188,8 @@ module Flagnetic
|
|
|
188
188
|
events
|
|
189
189
|
end
|
|
190
190
|
|
|
191
|
+
# wire scrubs the document because a flag key is whatever the caller
|
|
192
|
+
# passed to a getter, so it can be a string that is no UTF-8.
|
|
191
193
|
def wire(event)
|
|
192
194
|
doc = { "type" => "evaluation_summary", "event_id" => event.event_id,
|
|
193
195
|
"interval_start" => EventBatch.format_time(event.interval_start),
|
|
@@ -197,7 +199,7 @@ module Flagnetic
|
|
|
197
199
|
{ "flag" => r[:flag], "variation" => r[:variation], "reason" => r[:reason].to_s, "count" => r[:count] }
|
|
198
200
|
end }
|
|
199
201
|
doc["app_version"] = @app_version if @app_version
|
|
200
|
-
doc
|
|
202
|
+
EventBatch.scrub(doc)
|
|
201
203
|
end
|
|
202
204
|
|
|
203
205
|
def public_summary(event)
|
data/lib/flagnetic/transport.rb
CHANGED
|
@@ -16,9 +16,22 @@ module Flagnetic
|
|
|
16
16
|
# it. body is decompressed; retry_after is the raw header value.
|
|
17
17
|
EventsResponse = Struct.new(:status, :body, :retry_after, keyword_init: true)
|
|
18
18
|
|
|
19
|
+
# BodyTooLarge is a response body that grew past its limit. A fetch
|
|
20
|
+
# raises it like any network error, so it counts as a failed fetch.
|
|
21
|
+
class BodyTooLarge < StandardError; end
|
|
22
|
+
|
|
23
|
+
# RULESET_LIMIT bounds a ruleset body after decompression; a ruleset the
|
|
24
|
+
# server accepts is far smaller, so a larger one is a misbehaving peer.
|
|
25
|
+
RULESET_LIMIT = 256 << 20
|
|
26
|
+
# ANSWER_LIMIT bounds the answer to an events POST, a few counters and
|
|
27
|
+
# rejection reasons.
|
|
28
|
+
ANSWER_LIMIT = 1 << 20
|
|
29
|
+
|
|
19
30
|
attr_reader :stream_uri, :timeout
|
|
20
31
|
|
|
21
|
-
def initialize(sdk_key:, base_url:, platform:, timeout:)
|
|
32
|
+
def initialize(sdk_key:, base_url:, platform:, timeout:, ruleset_limit: RULESET_LIMIT, answer_limit: ANSWER_LIMIT)
|
|
33
|
+
@ruleset_limit = ruleset_limit
|
|
34
|
+
@answer_limit = answer_limit
|
|
22
35
|
base = URI(base_url.to_s.chomp("/"))
|
|
23
36
|
raise ArgumentError, "base_url must be an http or https URL, got #{base_url.inspect}" unless
|
|
24
37
|
base.is_a?(URI::HTTP) && base.host
|
|
@@ -49,22 +62,41 @@ module Flagnetic
|
|
|
49
62
|
request["Content-Encoding"] = "gzip"
|
|
50
63
|
request["Flagnetic-Schema-Version"] = SCHEMA_VERSION_MAX.to_s
|
|
51
64
|
request.body = gzipped_body
|
|
52
|
-
|
|
53
|
-
|
|
65
|
+
status = retry_after = body = nil
|
|
66
|
+
http_for(@events_uri, timeout).start do |h|
|
|
67
|
+
h.request(request) do |response|
|
|
68
|
+
status = response.code.to_i
|
|
69
|
+
retry_after = response["Retry-After"]
|
|
70
|
+
body = begin
|
|
71
|
+
read_body(response, @answer_limit)
|
|
72
|
+
rescue BodyTooLarge
|
|
73
|
+
# An answer past the limit is cut off, not an error: the batch
|
|
74
|
+
# reached the server, and EventBatch reads an undecodable
|
|
75
|
+
# answer as accepted without counts, so it is not sent twice.
|
|
76
|
+
""
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
EventsResponse.new(status: status, body: body, retry_after: retry_after)
|
|
54
81
|
end
|
|
55
82
|
|
|
56
83
|
# fetch_ruleset GETs the ruleset, revalidating the held version with
|
|
57
84
|
# If-None-Match. It raises on network errors and timeouts; Net::HTTP
|
|
58
|
-
# asks for and inflates gzip.
|
|
85
|
+
# asks for and inflates gzip, and the inflated body is bounded.
|
|
59
86
|
def fetch_ruleset(held_version)
|
|
60
87
|
request = Net::HTTP::Get.new(@ruleset_uri)
|
|
61
88
|
headers.each { |k, v| request[k] = v }
|
|
62
89
|
request["Accept"] = "application/json"
|
|
63
90
|
request["Flagnetic-Schema-Version"] = SCHEMA_VERSION_MAX.to_s
|
|
64
91
|
request["If-None-Match"] = %("#{held_version}") if held_version
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
92
|
+
status = body = nil
|
|
93
|
+
http_for(@ruleset_uri, @timeout).start do |h|
|
|
94
|
+
h.request(request) do |response|
|
|
95
|
+
status = response.code.to_i
|
|
96
|
+
body = read_body(response, @ruleset_limit)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
Response.new(status: status, body: body)
|
|
68
100
|
end
|
|
69
101
|
|
|
70
102
|
# stream_request is the change stream's request; Last-Event-ID is the
|
|
@@ -98,6 +130,19 @@ module Flagnetic
|
|
|
98
130
|
|
|
99
131
|
private
|
|
100
132
|
|
|
133
|
+
# read_body gathers the body in the pieces Net::HTTP yields, which are
|
|
134
|
+
# inflated when the answer is gzip, and raises as soon as their total
|
|
135
|
+
# passes limit: a Content-Length or a compressed size says nothing of
|
|
136
|
+
# what a peer will make the process hold.
|
|
137
|
+
def read_body(response, limit)
|
|
138
|
+
body = +"".b
|
|
139
|
+
response.read_body do |piece|
|
|
140
|
+
body << piece
|
|
141
|
+
raise BodyTooLarge, "the response body exceeds #{limit} bytes" if body.bytesize > limit
|
|
142
|
+
end
|
|
143
|
+
body.force_encoding(Encoding::UTF_8)
|
|
144
|
+
end
|
|
145
|
+
|
|
101
146
|
def headers
|
|
102
147
|
{ "Authorization" => "Bearer #{@sdk_key}", "User-Agent" => @user_agent }
|
|
103
148
|
end
|
data/lib/flagnetic/version.rb
CHANGED
data/lib/flagnetic/worker.rb
CHANGED
|
@@ -19,6 +19,7 @@ module Flagnetic
|
|
|
19
19
|
@client = client
|
|
20
20
|
@transport = transport
|
|
21
21
|
@timing = timing
|
|
22
|
+
@random = timing.random || Random.new
|
|
22
23
|
@stream_enabled = stream
|
|
23
24
|
@inbox = Queue.new
|
|
24
25
|
@stop = false
|
|
@@ -84,7 +85,7 @@ module Flagnetic
|
|
|
84
85
|
run
|
|
85
86
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
86
87
|
@client.__send__(:log, :error, :worker_died, cause: "#{e.class}: #{e.message}")
|
|
87
|
-
deaths.reset if now - started >= @timing.
|
|
88
|
+
deaths.reset if now - started >= @timing.worker_stable
|
|
88
89
|
sleep deaths.next_delay unless @stop
|
|
89
90
|
end
|
|
90
91
|
end
|
|
@@ -92,11 +93,12 @@ module Flagnetic
|
|
|
92
93
|
|
|
93
94
|
def run
|
|
94
95
|
@fetch_backoff = Backoff.new(base: @timing.fetch_base, cap: @timing.fetch_cap)
|
|
95
|
-
@stream_backoff = Backoff.new(base: @timing.stream_base, cap: @timing.stream_cap)
|
|
96
|
+
@stream_backoff = Backoff.new(base: @timing.stream_base, cap: @timing.stream_cap, random: @random)
|
|
96
97
|
@refused_backoff = Backoff.new(base: @timing.fetch_base, cap: @timing.unauthorized_cap)
|
|
97
98
|
@next_fetch = now
|
|
98
99
|
@stream_due = @stream_enabled ? now : nil
|
|
99
100
|
@connected = false
|
|
101
|
+
@stable = false
|
|
100
102
|
@want = nil
|
|
101
103
|
@event_retry = 0
|
|
102
104
|
step until @stop
|
|
@@ -208,26 +210,37 @@ module Flagnetic
|
|
|
208
210
|
def connected
|
|
209
211
|
@refused_backoff.reset
|
|
210
212
|
@connected = true
|
|
211
|
-
@
|
|
213
|
+
@stable = false
|
|
212
214
|
@client.__send__(:stream_connected)
|
|
213
215
|
@next_fetch = now # versions may have been missed while down
|
|
214
216
|
end
|
|
215
217
|
|
|
218
|
+
# close_stream forgets the stream's stability with it: a heartbeat on
|
|
219
|
+
# one stream says nothing of the next, so a failed reconnect after a
|
|
220
|
+
# stable stream still counts toward the backoff.
|
|
216
221
|
def close_stream
|
|
217
222
|
@head_pending = false
|
|
218
223
|
@stream&.close
|
|
219
224
|
@stream = nil
|
|
220
225
|
@connected = false
|
|
226
|
+
@stable = false
|
|
221
227
|
end
|
|
222
228
|
|
|
223
229
|
# drop ends the stream and schedules a reconnect: 0 to close_jitter
|
|
224
|
-
# seconds after a server-side close, else the backoff
|
|
225
|
-
#
|
|
230
|
+
# seconds after a server-side close of a stable stream, else the backoff.
|
|
231
|
+
# A stream is stable once it has delivered a heartbeat, which restarts
|
|
232
|
+
# the backoff from its base; one the server closes before that is a
|
|
233
|
+
# failed connection however it was answered, so that a draining replica
|
|
234
|
+
# or a proxy that ends the stream at once is backed off from.
|
|
226
235
|
def drop(cause, error:)
|
|
227
|
-
@stream_backoff.reset if @
|
|
236
|
+
@stream_backoff.reset if @stable
|
|
237
|
+
if !error && !@stable
|
|
238
|
+
error = true
|
|
239
|
+
cause = "#{cause} before its first heartbeat"
|
|
240
|
+
end
|
|
228
241
|
close_stream
|
|
229
242
|
@client.__send__(:log, error ? :warn : :info, :sse_dropped, cause: cause)
|
|
230
|
-
delay = error ? @stream_backoff.next_delay : rand * @timing.close_jitter
|
|
243
|
+
delay = error ? @stream_backoff.next_delay : @random.rand * @timing.close_jitter
|
|
231
244
|
@stream_due = now + delay
|
|
232
245
|
@next_fetch = [@next_fetch, now + @timing.poll_down].min
|
|
233
246
|
end
|
|
@@ -287,6 +300,7 @@ module Flagnetic
|
|
|
287
300
|
end
|
|
288
301
|
|
|
289
302
|
def frame(kind, value)
|
|
303
|
+
@stable = true if kind == :comment
|
|
290
304
|
return unless kind == :event && value.type == "ruleset"
|
|
291
305
|
|
|
292
306
|
version = begin
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: flagnetic
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Anatolii Zverev
|
|
@@ -29,6 +29,7 @@ files:
|
|
|
29
29
|
- lib/flagnetic/eval/murmur3.rb
|
|
30
30
|
- lib/flagnetic/eval/operators.rb
|
|
31
31
|
- lib/flagnetic/eval/regex.rb
|
|
32
|
+
- lib/flagnetic/eval/regex_vm.rb
|
|
32
33
|
- lib/flagnetic/eval/ruleset.rb
|
|
33
34
|
- lib/flagnetic/eval/semver.rb
|
|
34
35
|
- lib/flagnetic/eval/types.rb
|