flagnetic 0.1.2 → 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 +5 -3
- data/lib/flagnetic/eval/regex.rb +11 -1
- data/lib/flagnetic/railtie.rb +18 -2
- data/lib/flagnetic/recorder.rb +32 -15
- data/lib/flagnetic/sse_parser.rb +19 -4
- data/lib/flagnetic/store.rb +14 -3
- data/lib/flagnetic/transport.rb +52 -7
- data/lib/flagnetic/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 366e3febc729c08e8fc6c15f02ff5d68cc9c8820c436576955affc70f295515e
|
|
4
|
+
data.tar.gz: 449827190dfedce8c6c2976a2fd601ccb7866878c7ac88d1c65089fab8b41ed2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 177ac8930d64bd42d82d9df68cc191eb3f7ad719ddafcd325d5bf63774a694208d35faa247f35b871da96c9544183ba5a1e3ba62c4b0ca223111439f5fa2e197
|
|
7
|
+
data.tar.gz: 1cab276e7375fe1139e45f82c12ae3c3311dd676a2be7fafff5b969d3972c06af2116cd094c77da60575c57c621d103dbbb4e86c2dd037fbe309d1121e302a03
|
data/README.md
CHANGED
|
@@ -131,7 +131,7 @@ Log events have stable names (`Flagnetic::EVENTS`); alert on the name, never on
|
|
|
131
131
|
| `exposure_skipped` | debug | `flag`, `cause` (`no_experiment`, `no_detail`, `repeat`) |
|
|
132
132
|
| `exposures_disabled` | warn | |
|
|
133
133
|
| `ingest_failed` | warn | `kind`, `events`, `retry_in`, `status`, `error` |
|
|
134
|
-
| `client_not_configured` | warn | `rails_env`: the Rails integration found no SDK key and base URL and serves built-in defaults |
|
|
134
|
+
| `client_not_configured` | warn | `rails_env`: the Rails integration found no SDK key and base URL outside production and serves built-in defaults |
|
|
135
135
|
| `queue_dropped` | warn | `kind` (`exposure_or_alias`, `evaluation_summary`), `cause` (`queue_full`, `not_serialisable`, `client_error`, `refused_by_the_platform`, `window_older_than_one_hour`) and what was dropped (`fault` for `not_serialisable`) |
|
|
136
136
|
|
|
137
137
|
A ruleset from a newer server may hold constructs this SDK does not know. The client logs `rule_unsupported` once per flag and ruleset version, and the flag serves its default with the error code `inconsistent_ruleset`.
|
|
@@ -144,7 +144,7 @@ A condition this SDK cannot evaluate although the server accepted it, such as a
|
|
|
144
144
|
|
|
145
145
|
## Rails
|
|
146
146
|
|
|
147
|
-
Under Rails the gem loads a railtie (nothing of it loads without Rails) that sets `Flagnetic.client` up without an initializer. The SDK key and base URL come from the first of `config.flagnetic.sdk_key` and `config.flagnetic.base_url`, the `FLAGNETIC_SDK_KEY` and `FLAGNETIC_BASE_URL` environment variables, and credentials (`flagnetic.sdk_key`, `flagnetic.base_url`). Without both, the client serves built-in defaults and logs `client_not_configured`.
|
|
147
|
+
Under Rails the gem loads a railtie (nothing of it loads without Rails) that sets `Flagnetic.client` up without an initializer. The SDK key and base URL come from the first of `config.flagnetic.sdk_key` and `config.flagnetic.base_url`, the `FLAGNETIC_SDK_KEY` and `FLAGNETIC_BASE_URL` environment variables, and credentials (`flagnetic.sdk_key`, `flagnetic.base_url`). Without both and without `offline_file`, building the client in production raises `Flagnetic::NotConfigured`, naming what is missing and where to set it, so a deployment that lost its key fails at boot (in console, runner and rake, on first use) instead of serving built-in defaults to every user; in any other environment the client serves built-in defaults and logs `client_not_configured`.
|
|
148
148
|
|
|
149
149
|
A server or job process builds the client at boot and waits up to `ready_timeout` seconds for the ruleset; `rails console`, `rails runner` and rake build it on first use, so a task that never asks for a flag makes no request. Every forked child (Puma cluster mode, Unicorn) starts its own background thread at once, and every process closes its client at exit, which flushes what it queued. Local overrides are allowed in every environment but production, where no setting enables them.
|
|
150
150
|
|
|
@@ -192,6 +192,8 @@ client.recorder.aliases
|
|
|
192
192
|
client.recorder.reset
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
+
Each of the recorder's evaluation summaries, exposures and aliases keeps the latest 10,000 entries and drops the oldest beyond that, so an offline client left running in a long process stays bounded.
|
|
196
|
+
|
|
195
197
|
`with_flags` and `with_ruleset` make such a client `Flagnetic.client` for a block and restore the previous one afterwards, even when the block raises. They are process-wide, so tests running in parallel threads must not use them. Include the helpers in RSpec or Minitest:
|
|
196
198
|
|
|
197
199
|
```ruby
|
|
@@ -259,7 +261,7 @@ Every other flag evaluates normally. The fix is to upgrade the SDK.
|
|
|
259
261
|
|
|
260
262
|
## Regular expressions
|
|
261
263
|
|
|
262
|
-
A `matches` condition is RE2 syntax with RE2 semantics, as Go's `regexp` reads it, and not the dialect of Ruby's own engine. A pattern is parsed as RE2 and matched by an engine of its own that runs in time linear in the input, so a context value can never make a match slow or cut it off. `^` and `$` are not line anchors, `\s` is `[\t\n\f\r ]`, lookarounds and backreferences are refused and `(?i)` folds case by Go's rules, `\p{...}` included. A pattern RE2 refuses makes the condition `inconsistent_ruleset`.
|
|
264
|
+
A `matches` condition is RE2 syntax with RE2 semantics, as Go's `regexp` reads it, and not the dialect of Ruby's own engine. A pattern is parsed as RE2 and matched by an engine of its own that runs in time linear in the input, so a context value can never make a match slow or cut it off. `^` and `$` are not line anchors, `\s` is `[\t\n\f\r ]`, lookarounds and backreferences are refused and `(?i)` folds case by Go's rules, `\p{...}` included. A pattern RE2 refuses makes the condition `inconsistent_ruleset`. A string longer than 64 KiB (in bytes) is not matched at all: the condition is false for it, without an error, because the cost of a match grows with the input.
|
|
263
265
|
|
|
264
266
|
The case folding orbits and property names come from Go's `unicode` package, and `lib/flagnetic/eval/unicode_tables.rb` is generated from it:
|
|
265
267
|
|
data/lib/flagnetic/eval/regex.rb
CHANGED
|
@@ -34,6 +34,7 @@ module Flagnetic
|
|
|
34
34
|
# code point sharing a folding orbit with one, as Go does; whether a
|
|
35
35
|
# code point has the property is Ruby's Unicode data, the orbits are
|
|
36
36
|
# Go's.
|
|
37
|
+
# - A string over 64 KiB is not matched: it is a non-match, not an error.
|
|
37
38
|
# - A match searches anywhere in the string unless the pattern is
|
|
38
39
|
# anchored. Every byte of invalid UTF-8 in the matched string reads as
|
|
39
40
|
# one U+FFFD, as Go decodes it.
|
|
@@ -44,6 +45,12 @@ module Flagnetic
|
|
|
44
45
|
REPLACEMENT = [0xFFFD].pack("U")
|
|
45
46
|
private_constant :REPLACEMENT
|
|
46
47
|
|
|
48
|
+
# MAX_INPUT_BYTES is the longest string a pattern is matched against.
|
|
49
|
+
# The Pike VM's cost is the input times the program, and a context
|
|
50
|
+
# value is chosen by an end user, so a longer one is a non-match
|
|
51
|
+
# instead of a request that burns CPU.
|
|
52
|
+
MAX_INPUT_BYTES = 64 << 10
|
|
53
|
+
|
|
47
54
|
# Pattern is a compiled matches pattern.
|
|
48
55
|
class Pattern
|
|
49
56
|
def initialize(program)
|
|
@@ -51,8 +58,11 @@ module Flagnetic
|
|
|
51
58
|
freeze
|
|
52
59
|
end
|
|
53
60
|
|
|
54
|
-
# match? reports whether s contains a match
|
|
61
|
+
# match? reports whether s contains a match; a string over
|
|
62
|
+
# MAX_INPUT_BYTES contains none.
|
|
55
63
|
def match?(str)
|
|
64
|
+
return false if str.bytesize > MAX_INPUT_BYTES
|
|
65
|
+
|
|
56
66
|
str = str.scrub { |bad| REPLACEMENT * bad.bytesize } unless str.valid_encoding?
|
|
57
67
|
Vm.search(@program, str.codepoints)
|
|
58
68
|
end
|
data/lib/flagnetic/railtie.rb
CHANGED
|
@@ -13,8 +13,10 @@ module Flagnetic
|
|
|
13
13
|
# FLAGNETIC_BASE_URL environment variables, and credentials
|
|
14
14
|
# (flagnetic.sdk_key, flagnetic.base_url):
|
|
15
15
|
#
|
|
16
|
-
# - sdk_key, base_url: see above. Without both
|
|
17
|
-
#
|
|
16
|
+
# - sdk_key, base_url: see above. Without both (and without
|
|
17
|
+
# offline_file), building the client raises NotConfigured in
|
|
18
|
+
# production; elsewhere the client serves built-in defaults and logs
|
|
19
|
+
# client_not_configured.
|
|
18
20
|
# - offline_file: a ruleset document to serve instead; no network.
|
|
19
21
|
# - start: nil (default) starts the client at boot in a server or job
|
|
20
22
|
# process and on first use in rails console, rails runner and rake;
|
|
@@ -108,11 +110,25 @@ module Flagnetic
|
|
|
108
110
|
elsif sdk_key && base_url
|
|
109
111
|
Client.new(sdk_key, base_url: base_url, **options).tap { |c| c.ready(timeout: config.ready_timeout) }
|
|
110
112
|
else
|
|
113
|
+
not_configured!(sdk_key, base_url) if ::Rails.env.production?
|
|
111
114
|
log.call(:warn, :client_not_configured, { rails_env: ::Rails.env.to_s })
|
|
112
115
|
Client.new(offline: EMPTY, **options, send_summaries: false)
|
|
113
116
|
end
|
|
114
117
|
end
|
|
115
118
|
|
|
119
|
+
# not_configured! refuses to build a production client without an SDK
|
|
120
|
+
# key and base URL. Serving built-in defaults there would turn every
|
|
121
|
+
# flag off for every user behind nothing but a log line, the moment a
|
|
122
|
+
# deployment loses its secrets; failing the boot shows it at once.
|
|
123
|
+
def not_configured!(sdk_key, base_url)
|
|
124
|
+
missing = { sdk_key: ["SDK key", sdk_key], base_url: ["base URL", base_url] }
|
|
125
|
+
.filter_map { |name, (what, value)| "no #{what} (#{sources(name)})" unless value }
|
|
126
|
+
raise NotConfigured, "Flagnetic is not configured for production: #{missing.join(" and ")}. " \
|
|
127
|
+
"Set them, or set config.flagnetic.offline_file to serve a ruleset file"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def sources(name) = "config.flagnetic.#{name}, FLAGNETIC_#{name.upcase} or credentials flagnetic.#{name}"
|
|
131
|
+
|
|
116
132
|
# setting reads name from config, the environment variable, then
|
|
117
133
|
# credentials; blank is unset.
|
|
118
134
|
def setting(app, name)
|
data/lib/flagnetic/recorder.rb
CHANGED
|
@@ -7,7 +7,17 @@ module Flagnetic
|
|
|
7
7
|
# test can assert on it: the running evaluation counts, every evaluation
|
|
8
8
|
# summary a flush or close would have sent, and every exposure and alias
|
|
9
9
|
# the client recorded. It is thread-safe.
|
|
10
|
+
#
|
|
11
|
+
# Each of the three lists keeps the latest 10,000 entries and drops the
|
|
12
|
+
# oldest beyond that, so an offline client left running in a long
|
|
13
|
+
# process (a development server, a long test suite without reset) does
|
|
14
|
+
# not grow without bound. The running counts are one row per flag,
|
|
15
|
+
# variation and reason and need no cap.
|
|
10
16
|
class Recorder
|
|
17
|
+
# CAPACITY is how many entries each list keeps.
|
|
18
|
+
CAPACITY = 10_000
|
|
19
|
+
private_constant :CAPACITY
|
|
20
|
+
|
|
11
21
|
# SummaryCount is one row of an evaluation summary: how often a flag
|
|
12
22
|
# served a variation for an evaluation reason. It carries no unit IDs
|
|
13
23
|
# and no values; variation is nil for local_override and fallback.
|
|
@@ -79,7 +89,7 @@ module Flagnetic
|
|
|
79
89
|
# record keeps an evaluation summary the client would have sent once a
|
|
80
90
|
# window closed.
|
|
81
91
|
def record(summary)
|
|
82
|
-
@mutex.synchronize { @summaries
|
|
92
|
+
@mutex.synchronize { append(@summaries, summary) }
|
|
83
93
|
end
|
|
84
94
|
|
|
85
95
|
# evaluation_summaries returns the evaluation summaries the client
|
|
@@ -96,21 +106,19 @@ module Flagnetic
|
|
|
96
106
|
def record_event(event)
|
|
97
107
|
case event["type"]
|
|
98
108
|
when "exposure"
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
end
|
|
109
|
+
exposure = Exposure.new(event_id: event["event_id"], experiment_key: event["experiment_key"],
|
|
110
|
+
flag_key: event["flag_key"], variation_key: event["variation_key"],
|
|
111
|
+
unit_type: event["unit_type"], unit_id: event["unit_id"],
|
|
112
|
+
platform: event["platform"], app_version: event["app_version"],
|
|
113
|
+
reason: event["evaluation_reason"].to_sym, override: event["override"],
|
|
114
|
+
ruleset_version: event["ruleset_version"],
|
|
115
|
+
occurred_at: Time.parse(event["occurred_at"]))
|
|
116
|
+
@mutex.synchronize { append(@exposures, exposure) }
|
|
108
117
|
when "alias"
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
end
|
|
118
|
+
link = Alias.new(event_id: event["event_id"], from_unit_type: event["from_unit_type"],
|
|
119
|
+
from_unit_id: event["from_unit_id"], to_unit_type: event["to_unit_type"],
|
|
120
|
+
to_unit_id: event["to_unit_id"], occurred_at: Time.parse(event["occurred_at"]))
|
|
121
|
+
@mutex.synchronize { append(@aliases, link) }
|
|
114
122
|
end
|
|
115
123
|
end
|
|
116
124
|
|
|
@@ -131,5 +139,14 @@ module Flagnetic
|
|
|
131
139
|
# The client hands the recorder what it counts and reports; a test only
|
|
132
140
|
# reads it.
|
|
133
141
|
private :count, :record, :record_event
|
|
142
|
+
|
|
143
|
+
private
|
|
144
|
+
|
|
145
|
+
# append adds entry to list, dropping the oldest entry once the list
|
|
146
|
+
# holds CAPACITY. The caller holds the mutex.
|
|
147
|
+
def append(list, entry)
|
|
148
|
+
list.shift if list.size >= CAPACITY
|
|
149
|
+
list << entry
|
|
150
|
+
end
|
|
134
151
|
end
|
|
135
152
|
end
|
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
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "digest"
|
|
4
4
|
require "json"
|
|
5
|
+
require "securerandom"
|
|
5
6
|
|
|
6
7
|
module Flagnetic
|
|
7
8
|
# FileStore keeps the last good ruleset in one file: a header line naming
|
|
@@ -35,15 +36,25 @@ module Flagnetic
|
|
|
35
36
|
nil
|
|
36
37
|
end
|
|
37
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.
|
|
38
44
|
def save(json)
|
|
39
|
-
tmp = "#{@path}.#{Process.pid}.#{
|
|
45
|
+
tmp = "#{@path}.#{Process.pid}.#{SecureRandom.hex(8)}.tmp"
|
|
46
|
+
created = false
|
|
40
47
|
# The server ruleset holds private segments: readable by the owner only.
|
|
41
|
-
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
|
|
42
50
|
f.write(header, "\n", json)
|
|
43
51
|
end
|
|
44
52
|
File.rename(tmp, @path)
|
|
53
|
+
created = false
|
|
45
54
|
ensure
|
|
46
|
-
|
|
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)
|
|
47
58
|
end
|
|
48
59
|
|
|
49
60
|
def to_s = @path
|
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