satee-openfeature-provider 0.1.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 +7 -0
- data/CHANGELOG.md +1 -0
- data/LICENSE +21 -0
- data/README.md +210 -0
- data/lib/satee/openfeature/provider/clock.rb +38 -0
- data/lib/satee/openfeature/provider/configuration_error.rb +17 -0
- data/lib/satee/openfeature/provider/defect.rb +18 -0
- data/lib/satee/openfeature/provider/error.rb +10 -0
- data/lib/satee/openfeature/provider/flag.rb +11 -0
- data/lib/satee/openfeature/provider/flag_file.rb +76 -0
- data/lib/satee/openfeature/provider/fork_restart.rb +41 -0
- data/lib/satee/openfeature/provider/invalid_flag_file.rb +10 -0
- data/lib/satee/openfeature/provider/log.rb +90 -0
- data/lib/satee/openfeature/provider/poller.rb +325 -0
- data/lib/satee/openfeature/provider/sdk_key.rb +59 -0
- data/lib/satee/openfeature/provider/stderr_logger.rb +31 -0
- data/lib/satee/openfeature/provider/transport.rb +117 -0
- data/lib/satee/openfeature/provider/version.rb +9 -0
- data/lib/satee/openfeature/provider.rb +137 -0
- data/sig/satee/openfeature/provider/clock.rbs +38 -0
- data/sig/satee/openfeature/provider/configuration_error.rbs +11 -0
- data/sig/satee/openfeature/provider/defect.rbs +14 -0
- data/sig/satee/openfeature/provider/error.rbs +8 -0
- data/sig/satee/openfeature/provider/flag.rbs +16 -0
- data/sig/satee/openfeature/provider/flag_file.rbs +26 -0
- data/sig/satee/openfeature/provider/fork_restart.rbs +28 -0
- data/sig/satee/openfeature/provider/invalid_flag_file.rbs +8 -0
- data/sig/satee/openfeature/provider/log.rbs +54 -0
- data/sig/satee/openfeature/provider/poller.rbs +101 -0
- data/sig/satee/openfeature/provider/sdk_key.rbs +26 -0
- data/sig/satee/openfeature/provider/stderr_logger.rbs +19 -0
- data/sig/satee/openfeature/provider/transport.rbs +57 -0
- data/sig/satee/openfeature/provider.rbs +57 -0
- metadata +116 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Satee
|
|
6
|
+
module OpenFeature
|
|
7
|
+
class Provider
|
|
8
|
+
# Keeps the flag file current on one thread, which owns every timer and status change. Responses reach it
|
|
9
|
+
# through its mailbox, so it shares no state with the request threads or the host.
|
|
10
|
+
class Poller
|
|
11
|
+
STARTUP_WAIT = 5
|
|
12
|
+
STARTUP_RETRY = 1
|
|
13
|
+
REQUEST_TIMEOUT = 5
|
|
14
|
+
KEY_ERROR_INTERVAL = 60
|
|
15
|
+
IMF_FIXDATE = /\A(Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4} \d{2}:\d{2}:\d{2} GMT\z/
|
|
16
|
+
private_constant :STARTUP_WAIT, :STARTUP_RETRY, :REQUEST_TIMEOUT, :KEY_ERROR_INTERVAL, :IMF_FIXDATE
|
|
17
|
+
|
|
18
|
+
def initialize(transport:, clock:, poll_interval:, on_flags:, on_event:, log:)
|
|
19
|
+
@transport = transport
|
|
20
|
+
@clock = clock
|
|
21
|
+
@poll_interval = poll_interval
|
|
22
|
+
@on_flags = on_flags
|
|
23
|
+
@on_event = on_event
|
|
24
|
+
@log = log
|
|
25
|
+
@status = :not_ready
|
|
26
|
+
@error_reason = nil
|
|
27
|
+
@rejected_etags = Set.new
|
|
28
|
+
@flag_file = nil
|
|
29
|
+
@etag = nil
|
|
30
|
+
@request_id = 0
|
|
31
|
+
@mailbox = clock.mailbox
|
|
32
|
+
@stopped = false
|
|
33
|
+
@lock = Mutex.new
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Blocks until startup ends: a file is accepted, the key is refused, or the startup wait runs out. Does
|
|
37
|
+
# nothing once stopped.
|
|
38
|
+
def start
|
|
39
|
+
started = Thread::Queue.new
|
|
40
|
+
@lock.synchronize do
|
|
41
|
+
return if @stopped
|
|
42
|
+
|
|
43
|
+
@started = started
|
|
44
|
+
@startup_deadline = @clock.now + STARTUP_WAIT
|
|
45
|
+
ForkRestart.track(self)
|
|
46
|
+
@thread, @ended = spawn { run(started) }
|
|
47
|
+
end
|
|
48
|
+
started.pop
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Called in a forked child, where the polling thread did not survive. Carries on from the state the parent
|
|
52
|
+
# held, on a new thread and without blocking, and sends again a request the parent had in flight. A child
|
|
53
|
+
# that cannot start the thread reports ERROR, since its flags will no longer refresh.
|
|
54
|
+
def restart_in_child
|
|
55
|
+
return if @stopped || @thread == Thread.current
|
|
56
|
+
|
|
57
|
+
@next_poll ||= @clock.now
|
|
58
|
+
# If spawn fails, a stop must not wait on the parent's queue: its thread did not survive the fork, so
|
|
59
|
+
# nothing would close it.
|
|
60
|
+
@thread = @ended = nil
|
|
61
|
+
@thread, @ended = spawn { serve }
|
|
62
|
+
rescue => e
|
|
63
|
+
@log.restart_failed(e)
|
|
64
|
+
enter_error unless @status == :error
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Safe to call before #start, more than once, and from a thread still blocked in #start.
|
|
68
|
+
def stop
|
|
69
|
+
thread, ended = @lock.synchronize do
|
|
70
|
+
@stopped = true
|
|
71
|
+
ForkRestart.untrack(self)
|
|
72
|
+
[@thread, @ended]
|
|
73
|
+
end
|
|
74
|
+
@mailbox.push(:stop)
|
|
75
|
+
ended.pop if ended && thread != Thread.current
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# Thread#join would raise again the exception that ended the thread, so stop waits for the queue to close
|
|
81
|
+
# instead.
|
|
82
|
+
def spawn(&)
|
|
83
|
+
ended = Thread::Queue.new
|
|
84
|
+
thread = Thread.new do
|
|
85
|
+
reporting_death(&)
|
|
86
|
+
ensure
|
|
87
|
+
ended.close
|
|
88
|
+
end
|
|
89
|
+
thread.name = "satee-flags"
|
|
90
|
+
[thread, ended]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# An exception that ends polling is not rescued: it still ends the thread and reaches Ruby's thread report.
|
|
94
|
+
# The provider only stops claiming that its flags will refresh. Its message is not logged, as it may name the
|
|
95
|
+
# flag file URL.
|
|
96
|
+
def reporting_death
|
|
97
|
+
yield
|
|
98
|
+
ensure
|
|
99
|
+
error = $!
|
|
100
|
+
if error && !@stopped
|
|
101
|
+
enter_error unless @status == :error
|
|
102
|
+
@log.polling_stopped(error.class)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def run(started)
|
|
107
|
+
recovering { request }
|
|
108
|
+
serve
|
|
109
|
+
ensure
|
|
110
|
+
started.close
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def serve
|
|
114
|
+
loop do
|
|
115
|
+
message = @mailbox.pop(next_deadline)
|
|
116
|
+
break if message == :stop
|
|
117
|
+
|
|
118
|
+
recovering { handle(message) }
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def handle(message)
|
|
123
|
+
case message
|
|
124
|
+
in nil then fire_due_timers
|
|
125
|
+
in [:response, id, result] then finish(result) if id == @request_id && @request_deadline
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# A defect in handling one message must not end polling, nor leave it waiting on a timer that never comes.
|
|
130
|
+
# The poller reports ERROR and polls again on schedule; any response still in flight is dropped.
|
|
131
|
+
def recovering
|
|
132
|
+
yield
|
|
133
|
+
rescue => e
|
|
134
|
+
@log.defect("polling", Defect.from(e))
|
|
135
|
+
@request_deadline = nil
|
|
136
|
+
@poll_after = @clock.now + @poll_interval
|
|
137
|
+
@next_poll = @startup_deadline ? @clock.now + STARTUP_RETRY : @poll_after
|
|
138
|
+
enter_error unless @status == :error
|
|
139
|
+
@started&.close unless @startup_deadline
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def next_deadline
|
|
143
|
+
[@request_deadline, @startup_deadline, @next_poll].compact.min
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def fire_due_timers
|
|
147
|
+
now = @clock.now
|
|
148
|
+
end_startup if due?(@startup_deadline, now)
|
|
149
|
+
finish(nil) if due?(@request_deadline, now)
|
|
150
|
+
request if due?(@next_poll, now)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def due?(deadline, now)
|
|
154
|
+
!deadline.nil? && deadline <= now
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def request
|
|
158
|
+
id = @request_id += 1
|
|
159
|
+
@next_poll = nil
|
|
160
|
+
@request_deadline = @clock.now + REQUEST_TIMEOUT
|
|
161
|
+
@transport.get(etag: @etag) { |result| @mailbox.push([:response, id, result]) }
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def finish(result)
|
|
165
|
+
@request_deadline = nil
|
|
166
|
+
finished = @clock.now
|
|
167
|
+
wait = retry_wait(result)
|
|
168
|
+
outcome = apply(result)
|
|
169
|
+
@poll_after = finished + delay(outcome, @poll_interval, wait)
|
|
170
|
+
startup_deadline = @startup_deadline
|
|
171
|
+
if startup_deadline && outcome == :failure
|
|
172
|
+
retry_at = finished + delay(outcome, STARTUP_RETRY, wait)
|
|
173
|
+
@next_poll = (retry_at < startup_deadline) ? retry_at : @poll_after
|
|
174
|
+
elsif startup_deadline
|
|
175
|
+
end_startup
|
|
176
|
+
else
|
|
177
|
+
@next_poll = @poll_after
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def delay(outcome, usual, wait)
|
|
182
|
+
backoff = [KEY_ERROR_INTERVAL, @poll_interval].max
|
|
183
|
+
return backoff if outcome == :key_error
|
|
184
|
+
|
|
185
|
+
(wait || 0).clamp(usual, backoff)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Seconds a 429 or 503 asks the poller to wait, measured from when the response finished; nil when absent or
|
|
189
|
+
# not a value the spec allows.
|
|
190
|
+
def retry_wait(result)
|
|
191
|
+
return unless result.is_a?(Transport::Response) && [429, 503].include?(result.status)
|
|
192
|
+
|
|
193
|
+
value = result.retry_after or return
|
|
194
|
+
case value
|
|
195
|
+
when /\A\d+\z/ then value.to_i
|
|
196
|
+
when IMF_FIXDATE then Time.httpdate(value) - @clock.wall_now
|
|
197
|
+
else ignore_retry_after(value)
|
|
198
|
+
end
|
|
199
|
+
rescue ArgumentError
|
|
200
|
+
ignore_retry_after(value.to_s)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def ignore_retry_after(value)
|
|
204
|
+
@log.retry_after_ignored(value)
|
|
205
|
+
nil
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def apply(result)
|
|
209
|
+
case result
|
|
210
|
+
in Transport::Response[status: 200, body:, etag:]
|
|
211
|
+
accept(body, etag)
|
|
212
|
+
in Transport::Response[status: 304] if @flag_file
|
|
213
|
+
succeed
|
|
214
|
+
in Transport::Response[status: 404 | 403 => code]
|
|
215
|
+
refuse(code)
|
|
216
|
+
in Transport::Response[status:]
|
|
217
|
+
fail_poll("HTTP #{status}")
|
|
218
|
+
in Transport::Failure[error_class:]
|
|
219
|
+
fail_poll(error_class.to_s)
|
|
220
|
+
in Defect => defect
|
|
221
|
+
@log.defect("requesting the flags", defect)
|
|
222
|
+
fail_poll
|
|
223
|
+
in nil
|
|
224
|
+
fail_poll("no response within #{REQUEST_TIMEOUT} s")
|
|
225
|
+
in _
|
|
226
|
+
@log.defect("requesting the flags", Defect.new(error_class: result.class, message: "not a transport result"))
|
|
227
|
+
fail_poll
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def accept(body, etag)
|
|
232
|
+
flag_file = FlagFile.parse(body)
|
|
233
|
+
held = @flag_file
|
|
234
|
+
if held && flag_file.revision < held.revision
|
|
235
|
+
return reject(etag, "revision #{flag_file.revision} is lower than the held revision #{held.revision}")
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
begin
|
|
239
|
+
@on_flags.call(flag_file)
|
|
240
|
+
rescue => e
|
|
241
|
+
@log.defect("taking the flags", Defect.from(e))
|
|
242
|
+
return fail_poll
|
|
243
|
+
end
|
|
244
|
+
@flag_file = flag_file
|
|
245
|
+
@etag = etag
|
|
246
|
+
outcome = succeed
|
|
247
|
+
changed = held && flag_file.changed_keys(held)
|
|
248
|
+
if changed&.any?
|
|
249
|
+
notify { @on_event.call(::OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, {flags_changed: changed}) }
|
|
250
|
+
end
|
|
251
|
+
outcome
|
|
252
|
+
rescue InvalidFlagFile => e
|
|
253
|
+
reject(etag, e.message)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# A file served again under an ETag already rejected since the last success is not logged as rejected again.
|
|
257
|
+
def reject(etag, reason)
|
|
258
|
+
if etag && @rejected_etags.include?(etag)
|
|
259
|
+
@log.poll_failed("the same file was rejected again: #{reason}")
|
|
260
|
+
else
|
|
261
|
+
@log.file_rejected(reason)
|
|
262
|
+
@rejected_etags << etag if etag
|
|
263
|
+
end
|
|
264
|
+
fail_poll
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def succeed
|
|
268
|
+
@rejected_etags.clear
|
|
269
|
+
unless @status == :ready
|
|
270
|
+
@log.recovered unless @status == :not_ready
|
|
271
|
+
@error_reason = nil
|
|
272
|
+
change_status(:ready, ::OpenFeature::SDK::ProviderEvent::PROVIDER_READY)
|
|
273
|
+
end
|
|
274
|
+
:success
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def fail_poll(reason = nil)
|
|
278
|
+
@log.poll_failed(reason) if reason
|
|
279
|
+
if @status == :ready
|
|
280
|
+
@log.stale
|
|
281
|
+
change_status(:stale, ::OpenFeature::SDK::ProviderEvent::PROVIDER_STALE)
|
|
282
|
+
end
|
|
283
|
+
:failure
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# A key error is logged even while in error for another reason, so the host learns the key is the problem.
|
|
287
|
+
def refuse(code)
|
|
288
|
+
enter_error unless @status == :error
|
|
289
|
+
if @error_reason != code
|
|
290
|
+
(code == 404) ? @log.key_not_found : @log.request_refused
|
|
291
|
+
end
|
|
292
|
+
@error_reason = code
|
|
293
|
+
:key_error
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def end_startup
|
|
297
|
+
@startup_deadline = nil
|
|
298
|
+
@next_poll = @poll_after unless @request_deadline
|
|
299
|
+
if @status == :not_ready
|
|
300
|
+
enter_error
|
|
301
|
+
@log.not_ready
|
|
302
|
+
end
|
|
303
|
+
@started.close
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def enter_error
|
|
307
|
+
code = @flag_file ? ErrorCode::GENERAL : ErrorCode::PROVIDER_NOT_READY
|
|
308
|
+
change_status(:error, ::OpenFeature::SDK::ProviderEvent::PROVIDER_ERROR, error_code: code)
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def change_status(status, event, **details)
|
|
312
|
+
@status = status
|
|
313
|
+
notify { @on_event.call(event, details) }
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# A host callback that raises must not stop the polling thread.
|
|
317
|
+
def notify
|
|
318
|
+
yield
|
|
319
|
+
rescue => e
|
|
320
|
+
@log.handler_failed(e)
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module Satee
|
|
6
|
+
module OpenFeature
|
|
7
|
+
class Provider
|
|
8
|
+
# A server SDK key. It holds the key only in a closure, never in an instance variable, so inspect, YAML,
|
|
9
|
+
# Marshal and JSON serializers that walk instance variables cannot reach it.
|
|
10
|
+
class SdkKey
|
|
11
|
+
SERVER_PREFIX = "satee_server_"
|
|
12
|
+
CLIENT_PREFIX = "satee_client_"
|
|
13
|
+
BODY = /\A[1-9A-HJ-NP-Za-km-z]{24}\z/
|
|
14
|
+
PATH_LABEL = "satee-flags-v1:"
|
|
15
|
+
|
|
16
|
+
def initialize(value)
|
|
17
|
+
body = validated_body(value)
|
|
18
|
+
define_singleton_method(:path_id) do
|
|
19
|
+
Digest::SHA256.hexdigest(PATH_LABEL + body)[0, 32] #: String
|
|
20
|
+
end
|
|
21
|
+
hint = body[-4..] #: String
|
|
22
|
+
define_singleton_method(:hint) { hint }
|
|
23
|
+
freeze
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def inspect
|
|
27
|
+
"#<#{self.class.name} [REDACTED]>"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
alias_method :to_s, :inspect
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def validated_body(value)
|
|
35
|
+
missing = "Satee SDK key is missing. Set SATEE_SDK_KEY or pass the sdk_key: option."
|
|
36
|
+
malformed = "Satee SDK key must be satee_server_ followed by 24 base58 characters. Check SATEE_SDK_KEY."
|
|
37
|
+
raise ConfigurationError.new(missing, category: :missing) if value.nil? || value == ""
|
|
38
|
+
unless value.is_a?(String) && value.valid_encoding? && value.encoding.ascii_compatible?
|
|
39
|
+
raise ConfigurationError.new(malformed, category: :invalid)
|
|
40
|
+
end
|
|
41
|
+
if value.start_with?(CLIENT_PREFIX)
|
|
42
|
+
raise ConfigurationError.new(
|
|
43
|
+
"Satee SDK key is a browser key (satee_client_). Set SATEE_SDK_KEY to the server key (satee_server_).",
|
|
44
|
+
category: :client_key
|
|
45
|
+
)
|
|
46
|
+
end
|
|
47
|
+
unless value.start_with?(SERVER_PREFIX)
|
|
48
|
+
raise ConfigurationError.new("Satee SDK key must start with satee_server_. Check SATEE_SDK_KEY.",
|
|
49
|
+
category: :invalid)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
body = value.delete_prefix(SERVER_PREFIX)
|
|
53
|
+
raise ConfigurationError.new(malformed, category: :invalid) unless BODY.match?(body)
|
|
54
|
+
body
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Satee
|
|
4
|
+
module OpenFeature
|
|
5
|
+
class Provider
|
|
6
|
+
# The logger used when the host supplies none: info and above to standard error, debug discarded.
|
|
7
|
+
class StderrLogger
|
|
8
|
+
def debug(_message)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def info(message)
|
|
12
|
+
write("INFO", message)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def warn(message)
|
|
16
|
+
write("WARN", message)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def error(message)
|
|
20
|
+
write("ERROR", message)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def write(level, message)
|
|
26
|
+
$stderr.write("satee-openfeature #{level}: #{message}\n")
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "openssl"
|
|
5
|
+
require "zlib"
|
|
6
|
+
|
|
7
|
+
module Satee
|
|
8
|
+
module OpenFeature
|
|
9
|
+
class Provider
|
|
10
|
+
# Requests the flag file, each request on its own thread and its own connection. The timeout bounds each
|
|
11
|
+
# connect, read and write, and reading the body stops once it has passed since the request started, but a server
|
|
12
|
+
# can still hold a request past it, so a caller that stops waiting can ignore a late result.
|
|
13
|
+
class Transport
|
|
14
|
+
Response = Data.define(:status, :etag, :body, :retry_after)
|
|
15
|
+
Failure = Data.define(:error_class)
|
|
16
|
+
|
|
17
|
+
# The decoded body counts against it too, so a small compressed body cannot expand past it.
|
|
18
|
+
MAX_BODY_BYTES = 10 * 1024 * 1024
|
|
19
|
+
COMPRESSED = %w[gzip x-gzip deflate].freeze
|
|
20
|
+
|
|
21
|
+
class BodyTooLarge < StandardError; end
|
|
22
|
+
|
|
23
|
+
class BodyTooSlow < StandardError; end
|
|
24
|
+
|
|
25
|
+
# What a server, a network or a proxy can cause. Anything else raised while fetching is a defect in this
|
|
26
|
+
# provider or the standard library.
|
|
27
|
+
NETWORK_ERRORS = [
|
|
28
|
+
Timeout::Error, SocketError, SystemCallError, IOError, OpenSSL::SSL::SSLError,
|
|
29
|
+
Net::ProtocolError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Zlib::Error, BodyTooLarge, BodyTooSlow
|
|
30
|
+
].freeze
|
|
31
|
+
|
|
32
|
+
def initialize(uri, user_agent:, timeout: 5)
|
|
33
|
+
@uri = uri
|
|
34
|
+
@user_agent = user_agent
|
|
35
|
+
@timeout = timeout
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Yields a Response, a Failure when no whole response arrived in time, or a Defect.
|
|
39
|
+
def get(etag:, &on_finish)
|
|
40
|
+
headers = {"User-Agent" => @user_agent}
|
|
41
|
+
headers["If-None-Match"] = etag if etag
|
|
42
|
+
Thread.new { on_finish.call(fetch(headers)) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def fetch(headers)
|
|
48
|
+
deadline = now + @timeout
|
|
49
|
+
connection = Net::HTTP.new(@uri.hostname.to_s, @uri.port)
|
|
50
|
+
connection.use_ssl = @uri.scheme == "https"
|
|
51
|
+
connection.max_retries = 0
|
|
52
|
+
connection.ignore_eof = false
|
|
53
|
+
connection.open_timeout = @timeout
|
|
54
|
+
connection.read_timeout = @timeout
|
|
55
|
+
connection.write_timeout = @timeout
|
|
56
|
+
begin
|
|
57
|
+
connection.start
|
|
58
|
+
body = ""
|
|
59
|
+
response = connection.request(Net::HTTP::Get.new(@uri.request_uri, headers)) { |answer| body = read_body(answer, deadline) }
|
|
60
|
+
Response.new(status: response.code.to_i, etag: response["ETag"], body:, retry_after: response["Retry-After"])
|
|
61
|
+
rescue *NETWORK_ERRORS => e
|
|
62
|
+
Failure.new(error_class: e.class)
|
|
63
|
+
rescue => e
|
|
64
|
+
Defect.from(e, hiding: url_parts)
|
|
65
|
+
ensure
|
|
66
|
+
close(connection)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Inflates the body itself rather than through Net::HTTP, so the deadline is checked on every chunk read
|
|
71
|
+
# from the socket, including those a gzip header swallows without producing output.
|
|
72
|
+
def read_body(response, deadline)
|
|
73
|
+
raise BodyTooLarge if response.content_length.to_i > MAX_BODY_BYTES
|
|
74
|
+
response.decode_content = false
|
|
75
|
+
inflater = Zlib::Inflate.new(32 + Zlib::MAX_WBITS) if COMPRESSED.include?(response["Content-Encoding"]&.downcase)
|
|
76
|
+
body = +""
|
|
77
|
+
begin
|
|
78
|
+
response.read_body do |chunk|
|
|
79
|
+
raise BodyTooSlow if now > deadline
|
|
80
|
+
if inflater
|
|
81
|
+
inflater.inflate(chunk) { |piece| append_within_limit(body, piece) }
|
|
82
|
+
else
|
|
83
|
+
append_within_limit(body, chunk)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
append_within_limit(body, inflater.finish) if inflater&.total_in&.positive?
|
|
87
|
+
ensure
|
|
88
|
+
inflater&.close
|
|
89
|
+
end
|
|
90
|
+
body
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def append_within_limit(body, piece)
|
|
94
|
+
raise BodyTooLarge if body.bytesize + piece.bytesize > MAX_BODY_BYTES
|
|
95
|
+
body << piece
|
|
96
|
+
nil
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def now
|
|
100
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# The URL and its path, which holds the path ID, as a defect's message must never show them.
|
|
104
|
+
def url_parts
|
|
105
|
+
path = @uri.path.to_s
|
|
106
|
+
{@uri.to_s => "[flag file URL]"}.merge((path.length > 1) ? {path => "[flag file path]"} : {})
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def close(connection)
|
|
110
|
+
connection.finish if connection.started?
|
|
111
|
+
rescue IOError, SystemCallError, OpenSSL::SSL::SSLError
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open_feature/sdk"
|
|
4
|
+
require "uri"
|
|
5
|
+
require "zeitwerk"
|
|
6
|
+
|
|
7
|
+
module Satee
|
|
8
|
+
module OpenFeature
|
|
9
|
+
class Provider
|
|
10
|
+
# The loader owns only this class's directory, so it never claims the Satee or
|
|
11
|
+
# Satee::OpenFeature namespaces another gem may also define.
|
|
12
|
+
loader = Zeitwerk::Loader.new
|
|
13
|
+
loader.tag = "satee-openfeature-provider"
|
|
14
|
+
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
|
15
|
+
loader.push_dir("#{__dir__}/provider", namespace: self)
|
|
16
|
+
loader.setup
|
|
17
|
+
|
|
18
|
+
include ::OpenFeature::SDK::Provider::EventEmitter
|
|
19
|
+
|
|
20
|
+
NAME = "Satee"
|
|
21
|
+
HOST = "flags.satee.dev"
|
|
22
|
+
DEFAULT_POLL_INTERVAL = 5
|
|
23
|
+
MIN_POLL_INTERVAL = 1
|
|
24
|
+
|
|
25
|
+
ResolutionDetails = ::OpenFeature::SDK::Provider::ResolutionDetails
|
|
26
|
+
ErrorCode = ::OpenFeature::SDK::Provider::ErrorCode
|
|
27
|
+
Reason = ::OpenFeature::SDK::Provider::Reason
|
|
28
|
+
private_constant :NAME, :HOST, :DEFAULT_POLL_INTERVAL, :MIN_POLL_INTERVAL, :ResolutionDetails, :ErrorCode, :Reason
|
|
29
|
+
|
|
30
|
+
attr_reader :metadata
|
|
31
|
+
|
|
32
|
+
# poll_interval is in seconds; a value under one second is raised to one. logger: takes any object with debug,
|
|
33
|
+
# info, warn and error methods that accept a string. clock: and transport: are seams for tests.
|
|
34
|
+
def initialize(sdk_key: ENV["SATEE_SDK_KEY"], poll_interval: DEFAULT_POLL_INTERVAL, logger: StderrLogger.new, clock: Clock.new, transport: Transport)
|
|
35
|
+
@sdk_key = SdkKey.new(sdk_key)
|
|
36
|
+
@poll_interval = [poll_interval, MIN_POLL_INTERVAL].max
|
|
37
|
+
@log = Log.new(logger, key_hint: @sdk_key.hint)
|
|
38
|
+
@clock = clock
|
|
39
|
+
@transport = transport
|
|
40
|
+
@metadata = ::OpenFeature::SDK::Provider::ProviderMetadata.new(name: NAME).freeze
|
|
41
|
+
@flag_file = nil
|
|
42
|
+
@poller = nil
|
|
43
|
+
@lock = Mutex.new
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Called by the SDK when the provider is set. Returns once flags load, the key is refused, or five seconds pass;
|
|
47
|
+
# polling goes on in the background either way.
|
|
48
|
+
def init(_evaluation_context = nil)
|
|
49
|
+
poller, replaced = @lock.synchronize do
|
|
50
|
+
# The SDK detaches a provider before shutting it down, so an init that runs after its shutdown must not poll.
|
|
51
|
+
return unless configuration_attached?
|
|
52
|
+
|
|
53
|
+
replaced = @poller
|
|
54
|
+
@poller = build_poller
|
|
55
|
+
[@poller, replaced]
|
|
56
|
+
end
|
|
57
|
+
replaced&.stop
|
|
58
|
+
@flag_file = nil
|
|
59
|
+
poller.start
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def shutdown
|
|
63
|
+
poller = @lock.synchronize { @poller.tap { @poller = nil } }
|
|
64
|
+
poller&.stop
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The provider reports READY, STALE and ERROR itself, so the SDK must not infer them from #init returning.
|
|
68
|
+
def emits_lifecycle_events?
|
|
69
|
+
true
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil)
|
|
73
|
+
resolve(flag_key, default_value, :boolean)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def fetch_string_value(flag_key:, default_value:, evaluation_context: nil)
|
|
77
|
+
resolve(flag_key, default_value, :string)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def fetch_number_value(flag_key:, default_value:, evaluation_context: nil)
|
|
81
|
+
resolve(flag_key, default_value, :number)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil)
|
|
85
|
+
resolve(flag_key, default_value, :integer)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def fetch_float_value(flag_key:, default_value:, evaluation_context: nil)
|
|
89
|
+
resolve(flag_key, default_value, :float)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def fetch_object_value(flag_key:, default_value:, evaluation_context: nil)
|
|
93
|
+
resolve(flag_key, default_value, :object)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
def build_poller
|
|
99
|
+
Poller.new(
|
|
100
|
+
transport: @transport.new(
|
|
101
|
+
URI::HTTPS.build(host: HOST, path: "/v1/#{@sdk_key.path_id}/flags.json"),
|
|
102
|
+
user_agent: "satee-openfeature-ruby/#{VERSION}"
|
|
103
|
+
),
|
|
104
|
+
clock: @clock,
|
|
105
|
+
poll_interval: @poll_interval,
|
|
106
|
+
on_flags: ->(flag_file) { replace_flags(flag_file) },
|
|
107
|
+
on_event: ->(event, details) { emit_event(event, details) },
|
|
108
|
+
log: @log
|
|
109
|
+
)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# The one way held flags change. An evaluation reads whichever file was held when it started.
|
|
113
|
+
def replace_flags(flag_file)
|
|
114
|
+
@flag_file = flag_file
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def resolve(flag_key, default_value, type)
|
|
118
|
+
flag_file = @flag_file
|
|
119
|
+
return error(default_value, ErrorCode::PROVIDER_NOT_READY, "Flags have not loaded yet") unless flag_file
|
|
120
|
+
|
|
121
|
+
flag = flag_file.flags[flag_key]
|
|
122
|
+
return error(default_value, ErrorCode::FLAG_NOT_FOUND, "Flag #{flag_key.inspect} was not found") unless flag
|
|
123
|
+
|
|
124
|
+
return ResolutionDetails.new(value: default_value, reason: Reason::DISABLED) unless flag.enabled
|
|
125
|
+
unless type == :boolean
|
|
126
|
+
return error(default_value, ErrorCode::TYPE_MISMATCH, "Flag #{flag_key.inspect} is a boolean, not #{type}")
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
ResolutionDetails.new(value: flag.value, variant: flag.variant, reason: Reason::STATIC)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def error(default_value, code, message)
|
|
133
|
+
ResolutionDetails.new(value: default_value, reason: Reason::ERROR, error_code: code, error_message: message)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|