configdirector-server-sdk 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/.yardopts +2 -0
- data/CHANGELOG.md +254 -0
- data/LICENSE +21 -0
- data/README.md +230 -0
- data/lib/configdirector/client_hooks.rb +46 -0
- data/lib/configdirector/config_director_client.rb +322 -0
- data/lib/configdirector/config_evaluated_event.rb +10 -0
- data/lib/configdirector/config_evaluation.rb +51 -0
- data/lib/configdirector/config_state.rb +39 -0
- data/lib/configdirector/configs_updated_event.rb +23 -0
- data/lib/configdirector/connection_error_event.rb +11 -0
- data/lib/configdirector/connection_options.rb +60 -0
- data/lib/configdirector/context.rb +53 -0
- data/lib/configdirector/errors.rb +29 -0
- data/lib/configdirector/internal/client/client_connection.rb +119 -0
- data/lib/configdirector/internal/client/client_factory.rb +104 -0
- data/lib/configdirector/internal/client/client_options_validation.rb +53 -0
- data/lib/configdirector/internal/client/config_read.rb +31 -0
- data/lib/configdirector/internal/client/config_reader.rb +55 -0
- data/lib/configdirector/internal/client/connection_intake.rb +59 -0
- data/lib/configdirector/internal/client/connection_options_validation.rb +94 -0
- data/lib/configdirector/internal/client/context_admission.rb +48 -0
- data/lib/configdirector/internal/client/context_attributes.rb +78 -0
- data/lib/configdirector/internal/client/context_kind.rb +24 -0
- data/lib/configdirector/internal/client/evaluated_read.rb +9 -0
- data/lib/configdirector/internal/client/event_handlers.rb +114 -0
- data/lib/configdirector/internal/client/event_tracking.rb +39 -0
- data/lib/configdirector/internal/client/live_client_connections.rb +58 -0
- data/lib/configdirector/internal/client/number_of_seconds.rb +21 -0
- data/lib/configdirector/internal/client/registrations.rb +43 -0
- data/lib/configdirector/internal/client/restart_in_forked_processes.rb +25 -0
- data/lib/configdirector/internal/client/served_configs.rb +83 -0
- data/lib/configdirector/internal/client/serving_state.rb +102 -0
- data/lib/configdirector/internal/client/watches.rb +51 -0
- data/lib/configdirector/internal/client/what_follows_a_fork.rb +24 -0
- data/lib/configdirector/internal/connection/condition_parser.rb +74 -0
- data/lib/configdirector/internal/connection/config_definition.rb +37 -0
- data/lib/configdirector/internal/connection/http_session.rb +15 -0
- data/lib/configdirector/internal/connection/in_memory_connection.rb +134 -0
- data/lib/configdirector/internal/connection/json_post_request.rb +24 -0
- data/lib/configdirector/internal/connection/not_a_server_sdk_payload.rb +9 -0
- data/lib/configdirector/internal/connection/offline_mode_connection.rb +96 -0
- data/lib/configdirector/internal/connection/offline_values_file.rb +49 -0
- data/lib/configdirector/internal/connection/payloads_delivered_in_turn.rb +46 -0
- data/lib/configdirector/internal/connection/polling_interval.rb +21 -0
- data/lib/configdirector/internal/connection/request_deadline.rb +45 -0
- data/lib/configdirector/internal/connection/retry_after_header.rb +24 -0
- data/lib/configdirector/internal/connection/server_sdk_payload.rb +37 -0
- data/lib/configdirector/internal/connection/server_sdk_payload_parser.rb +89 -0
- data/lib/configdirector/internal/connection/targeting_rules_parser.rb +70 -0
- data/lib/configdirector/internal/connection/test_client_initializations.rb +111 -0
- data/lib/configdirector/internal/connection/test_client_values.rb +53 -0
- data/lib/configdirector/internal/connection/unreadable_definition.rb +9 -0
- data/lib/configdirector/internal/connection/wire_fields.rb +59 -0
- data/lib/configdirector/internal/evaluation/array_comparison.rb +25 -0
- data/lib/configdirector/internal/evaluation/assigned_percentage.rb +21 -0
- data/lib/configdirector/internal/evaluation/attribute_condition.rb +48 -0
- data/lib/configdirector/internal/evaluation/config_evaluator.rb +29 -0
- data/lib/configdirector/internal/evaluation/datetime_comparison.rb +93 -0
- data/lib/configdirector/internal/evaluation/evaluation_context.rb +17 -0
- data/lib/configdirector/internal/evaluation/number_comparison.rb +119 -0
- data/lib/configdirector/internal/evaluation/rapidhash.rb +124 -0
- data/lib/configdirector/internal/evaluation/rollout_share.rb +9 -0
- data/lib/configdirector/internal/evaluation/rule.rb +70 -0
- data/lib/configdirector/internal/evaluation/scalar_text.rb +70 -0
- data/lib/configdirector/internal/evaluation/segment.rb +17 -0
- data/lib/configdirector/internal/evaluation/segment_condition.rb +26 -0
- data/lib/configdirector/internal/evaluation/semver_comparison.rb +49 -0
- data/lib/configdirector/internal/evaluation/served_value.rb +9 -0
- data/lib/configdirector/internal/evaluation/share.rb +9 -0
- data/lib/configdirector/internal/evaluation/targeting_rules.rb +18 -0
- data/lib/configdirector/internal/evaluation/text_comparison.rb +32 -0
- data/lib/configdirector/internal/evaluation/trait_pointer.rb +42 -0
- data/lib/configdirector/internal/evaluation.rb +21 -0
- data/lib/configdirector/internal/identity/sdk_identity.rb +39 -0
- data/lib/configdirector/internal/identity/wrapper.rb +48 -0
- data/lib/configdirector/internal/logging/client_log.rb +37 -0
- data/lib/configdirector/internal/logging/default_logger.rb +34 -0
- data/lib/configdirector/internal/polling/poll_answer.rb +111 -0
- data/lib/configdirector/internal/polling/poll_request.rb +106 -0
- data/lib/configdirector/internal/polling/poll_timing.rb +25 -0
- data/lib/configdirector/internal/polling/polling_endpoint.rb +36 -0
- data/lib/configdirector/internal/polling/polling_mode_connection.rb +53 -0
- data/lib/configdirector/internal/polling/polling_session.rb +99 -0
- data/lib/configdirector/internal/polling/polling_transport.rb +74 -0
- data/lib/configdirector/internal/polling/refusal_detail.rb +36 -0
- data/lib/configdirector/internal/server_sent_events/event_stream_limit_exceeded.rb +9 -0
- data/lib/configdirector/internal/server_sent_events/event_stream_parser.rb +95 -0
- data/lib/configdirector/internal/server_sent_events/server_sent_event.rb +9 -0
- data/lib/configdirector/internal/stoppable_thread.rb +69 -0
- data/lib/configdirector/internal/streaming/heartbeats.rb +51 -0
- data/lib/configdirector/internal/streaming/reconnecting_stream.rb +82 -0
- data/lib/configdirector/internal/streaming/reconnection_delay.rb +29 -0
- data/lib/configdirector/internal/streaming/stream_connection.rb +86 -0
- data/lib/configdirector/internal/streaming/stream_end.rb +69 -0
- data/lib/configdirector/internal/streaming/stream_endpoint.rb +43 -0
- data/lib/configdirector/internal/streaming/stream_timing.rb +33 -0
- data/lib/configdirector/internal/streaming/streaming_mode_connection.rb +60 -0
- data/lib/configdirector/internal/streaming/streaming_transport.rb +42 -0
- data/lib/configdirector/internal/telemetry/aggregated_wire_entries.rb +26 -0
- data/lib/configdirector/internal/telemetry/collected_telemetry.rb +98 -0
- data/lib/configdirector/internal/telemetry/compact_json_text.rb +64 -0
- data/lib/configdirector/internal/telemetry/discarded_telemetry.rb +31 -0
- data/lib/configdirector/internal/telemetry/event_reporter.rb +47 -0
- data/lib/configdirector/internal/telemetry/invalid_context_submissions.rb +76 -0
- data/lib/configdirector/internal/telemetry/report_answer.rb +31 -0
- data/lib/configdirector/internal/telemetry/report_schedule.rb +66 -0
- data/lib/configdirector/internal/telemetry/report_timing.rb +20 -0
- data/lib/configdirector/internal/telemetry/reported_context.rb +28 -0
- data/lib/configdirector/internal/telemetry/reported_details.rb +71 -0
- data/lib/configdirector/internal/telemetry/reported_evaluation.rb +82 -0
- data/lib/configdirector/internal/telemetry/reported_telemetry.rb +127 -0
- data/lib/configdirector/internal/telemetry/reported_text.rb +13 -0
- data/lib/configdirector/internal/telemetry/reported_time.rb +13 -0
- data/lib/configdirector/internal/telemetry/reported_value.rb +35 -0
- data/lib/configdirector/internal/telemetry/telemetry_endpoint.rb +54 -0
- data/lib/configdirector/internal/telemetry/telemetry_report.rb +50 -0
- data/lib/configdirector/internal/telemetry/test_client_telemetry.rb +37 -0
- data/lib/configdirector/internal/telemetry/tracked_event_admission.rb +72 -0
- data/lib/configdirector/internal/telemetry/tracked_event_entry.rb +27 -0
- data/lib/configdirector/internal/telemetry/tracked_event_queue.rb +47 -0
- data/lib/configdirector/internal/telemetry/tracked_event_warning.rb +28 -0
- data/lib/configdirector/internal/telemetry/value_id.rb +23 -0
- data/lib/configdirector/internal/telemetry/written_json.rb +9 -0
- data/lib/configdirector/internal/unchangeable_copy.rb +38 -0
- data/lib/configdirector/internal/value/encoded_native_value.rb +45 -0
- data/lib/configdirector/internal/value/hash_keys.rb +34 -0
- data/lib/configdirector/internal/value/in_code_default_value.rb +33 -0
- data/lib/configdirector/internal/value/json_contents.rb +81 -0
- data/lib/configdirector/internal/value/json_position.rb +27 -0
- data/lib/configdirector/internal/value/native_values.rb +41 -0
- data/lib/configdirector/internal/value/parsed_json.rb +79 -0
- data/lib/configdirector/internal/value/plain_decimal_text.rb +36 -0
- data/lib/configdirector/internal/value/served_text.rb +76 -0
- data/lib/configdirector/internal/value/typed_value.rb +9 -0
- data/lib/configdirector/internal/value/unservable_value.rb +9 -0
- data/lib/configdirector/internal/value/utf8_text.rb +19 -0
- data/lib/configdirector/internal.rb +9 -0
- data/lib/configdirector/metadata.rb +26 -0
- data/lib/configdirector/subscription.rb +34 -0
- data/lib/configdirector/telemetry_options.rb +28 -0
- data/lib/configdirector/testing/test_client.rb +122 -0
- data/lib/configdirector/testing/tracked_event.rb +46 -0
- data/lib/configdirector/testing.rb +44 -0
- data/lib/configdirector/variation.rb +21 -0
- data/lib/configdirector/version.rb +7 -0
- data/lib/configdirector-server-sdk.rb +3 -0
- data/lib/configdirector.rb +164 -0
- data/sig/configdirector/internal/client.rbs +518 -0
- data/sig/configdirector/internal/connection.rbs +389 -0
- data/sig/configdirector/internal/evaluation.rbs +364 -0
- data/sig/configdirector/internal/identity.rbs +51 -0
- data/sig/configdirector/internal/logging.rbs +33 -0
- data/sig/configdirector/internal/polling.rbs +240 -0
- data/sig/configdirector/internal/server_sent_events.rbs +51 -0
- data/sig/configdirector/internal/streaming.rbs +266 -0
- data/sig/configdirector/internal/telemetry.rbs +556 -0
- data/sig/configdirector/internal/value.rbs +159 -0
- data/sig/configdirector/testing.rbs +70 -0
- data/sig/configdirector.rbs +465 -0
- metadata +231 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Connection
|
|
6
|
+
class InMemoryConnection
|
|
7
|
+
REFUSAL_MESSAGE = "Connection failed with status: 401. Error: the test client failed this initialization. " \
|
|
8
|
+
"This is an unrecoverable error, will not attempt to reconnect."
|
|
9
|
+
|
|
10
|
+
private_constant :REFUSAL_MESSAGE
|
|
11
|
+
|
|
12
|
+
def initialize(values, timeout_seconds:, logger:)
|
|
13
|
+
@values = TestClientValues.serving(values || {})
|
|
14
|
+
@log = Logging::ClientLog.new(logger, component: "InMemoryConnection")
|
|
15
|
+
@lock = Mutex.new
|
|
16
|
+
@initializations = TestClientInitializations.new(@lock, timeout_seconds)
|
|
17
|
+
@payloads_delivered_in_turn = PayloadsDeliveredInTurn.new
|
|
18
|
+
@deliver_payload = ->(_payload) {}
|
|
19
|
+
@report_unrecoverable_error = ->(_connection_error) {}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def set_value(config_key, value)
|
|
23
|
+
values_set = TestClientValues.serving({ config_key => value })
|
|
24
|
+
@lock.synchronize do
|
|
25
|
+
@values = @values.merged_with(values_set)
|
|
26
|
+
add_if_connected(values_set.delta_payload)
|
|
27
|
+
end
|
|
28
|
+
deliver_the_waiting_payloads
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def remove_value(given_key)
|
|
32
|
+
@lock.synchronize do
|
|
33
|
+
@values = @values.without_the_value_of(given_key)
|
|
34
|
+
add_if_connected(@values.full_payload)
|
|
35
|
+
end
|
|
36
|
+
deliver_the_waiting_payloads
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def replace_values(values)
|
|
40
|
+
replacing_values = TestClientValues.serving(values)
|
|
41
|
+
@lock.synchronize do
|
|
42
|
+
@values = replacing_values
|
|
43
|
+
@initializations.serve_the_values_at_the_next_initialization
|
|
44
|
+
add_if_connected(@values.full_payload)
|
|
45
|
+
end
|
|
46
|
+
deliver_the_waiting_payloads
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def hold_initialization
|
|
50
|
+
@lock.synchronize { @initializations.hold_the_next_initialization }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def complete_initialization
|
|
54
|
+
case @lock.synchronize { @initializations.complete_initialization }
|
|
55
|
+
when :held_initialization_completed then deliver_the_values_to_the_held_initialization
|
|
56
|
+
when :no_initialization_held then nil
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def fail_initialization
|
|
61
|
+
case @lock.synchronize { @initializations.fail_initialization }
|
|
62
|
+
when :held_initialization_failed
|
|
63
|
+
@initializations.settling_the_held_initialization(:failed) { report_the_refusal }
|
|
64
|
+
when :next_initialization_fails then nil
|
|
65
|
+
end
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def open(report_unrecoverable_error:, &deliver_payload)
|
|
70
|
+
@report_unrecoverable_error = report_unrecoverable_error
|
|
71
|
+
@deliver_payload = deliver_payload
|
|
72
|
+
|
|
73
|
+
case @lock.synchronize { begin_the_next_initialization }
|
|
74
|
+
when :serves_the_values then deliver_the_stored_values
|
|
75
|
+
when :fails then refuse_the_initialization
|
|
76
|
+
when :is_held then what_follows_the_held_initialization
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def restart_in_a_forked_process(**); end
|
|
81
|
+
|
|
82
|
+
def close; end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def begin_the_next_initialization
|
|
87
|
+
next_initialization = @initializations.begin_the_next_initialization
|
|
88
|
+
add_if_connected(@values.full_payload)
|
|
89
|
+
|
|
90
|
+
next_initialization
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def deliver_the_stored_values
|
|
94
|
+
deliver_the_waiting_payloads
|
|
95
|
+
|
|
96
|
+
:values_awaited
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def refuse_the_initialization
|
|
100
|
+
report_the_refusal
|
|
101
|
+
|
|
102
|
+
:first_attempt_failed
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def what_follows_the_held_initialization
|
|
106
|
+
case @lock.synchronize { @initializations.outcome_of_the_held_initialization }
|
|
107
|
+
when :completed, :ended_at_the_timeout then :values_awaited
|
|
108
|
+
when :failed then :first_attempt_failed
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def deliver_the_values_to_the_held_initialization
|
|
113
|
+
@initializations.settling_the_held_initialization(:completed) do
|
|
114
|
+
@lock.synchronize { @payloads_delivered_in_turn.add(@values.full_payload) }
|
|
115
|
+
deliver_the_waiting_payloads
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def report_the_refusal
|
|
120
|
+
@log.error(REFUSAL_MESSAGE)
|
|
121
|
+
@report_unrecoverable_error.call(ConfigDirectorConnectionError.new(REFUSAL_MESSAGE, status: 401))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def add_if_connected(payload)
|
|
125
|
+
@payloads_delivered_in_turn.add(payload) if @initializations.connected?
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def deliver_the_waiting_payloads
|
|
129
|
+
@payloads_delivered_in_turn.deliver_each(&@deliver_payload)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
|
|
6
|
+
module ConfigDirector
|
|
7
|
+
module Internal
|
|
8
|
+
module Connection
|
|
9
|
+
module JsonPostRequest
|
|
10
|
+
def self.to(uri, sdk_identity, body)
|
|
11
|
+
carrying(JSON.generate(body), uri, sdk_identity)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.carrying(json_text, uri, sdk_identity)
|
|
15
|
+
request = Net::HTTP::Post.new(uri)
|
|
16
|
+
request["Content-Type"] = "application/json"
|
|
17
|
+
request["User-Agent"] = sdk_identity.user_agent
|
|
18
|
+
request.body = json_text
|
|
19
|
+
request
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pathname"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Connection
|
|
8
|
+
class OfflineModeConnection
|
|
9
|
+
CONFIG_ID_PREFIX = "offline:"
|
|
10
|
+
|
|
11
|
+
private_constant :CONFIG_ID_PREFIX
|
|
12
|
+
|
|
13
|
+
def self.for_a_client_with(connection_options:, **)
|
|
14
|
+
file = connection_options.file
|
|
15
|
+
values = connection_options.values
|
|
16
|
+
|
|
17
|
+
case offline_source_given_by(file, values)
|
|
18
|
+
when :no_values then serving_the_payload(ServerSdkPayload.without_definitions)
|
|
19
|
+
when :values then serving_the_payload(payload_serving_the_given_values(values))
|
|
20
|
+
when :file then reading_the_file(file)
|
|
21
|
+
when :file_and_values
|
|
22
|
+
raise ConfigDirectorValidationError, "An offline connection takes either 'file' or 'values', not both."
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.offline_source_given_by(file, values)
|
|
27
|
+
if file.nil? && values.nil? then :no_values
|
|
28
|
+
elsif file.nil? then :values
|
|
29
|
+
elsif values.nil? then :file
|
|
30
|
+
else :file_and_values
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.serving_the_payload(payload)
|
|
35
|
+
new { payload }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.reading_the_file(file)
|
|
39
|
+
unless file.is_a?(String) || file.is_a?(Pathname)
|
|
40
|
+
raise ConfigDirectorValidationError,
|
|
41
|
+
"Invalid 'file' option of class #{file.class}. It must be a String or a Pathname."
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
new { payload_serving_the_values_in_the_file(file) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.payload_serving_the_given_values(values)
|
|
48
|
+
Value::NativeValues.reject_values_of_another_class(values)
|
|
49
|
+
payload_serving(values)
|
|
50
|
+
rescue Value::UnservableValue => e
|
|
51
|
+
raise ConfigDirectorValidationError, "The offline values could not be loaded. #{e.message}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.payload_serving_the_values_in_the_file(file)
|
|
55
|
+
payload_serving(OfflineValuesFile.values_in(file))
|
|
56
|
+
rescue Value::UnservableValue => e
|
|
57
|
+
raise ConfigDirectorInitializationError, "The offline values file '#{file}' could not be loaded. #{e.message}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.payload_serving(values)
|
|
61
|
+
ServerSdkPayload.of_definitions(
|
|
62
|
+
ConfigDefinition.serving_each_value_to_every_context(values, CONFIG_ID_PREFIX)
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private_class_method :new, :offline_source_given_by, :serving_the_payload, :reading_the_file,
|
|
67
|
+
:payload_serving_the_given_values, :payload_serving_the_values_in_the_file,
|
|
68
|
+
:payload_serving
|
|
69
|
+
|
|
70
|
+
def initialize(&payload_of_the_offline_values)
|
|
71
|
+
@payload_of_the_offline_values = payload_of_the_offline_values
|
|
72
|
+
@delivering_the_offline_values = false
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def open(**, &)
|
|
76
|
+
deliver_the_offline_values(&) unless @delivering_the_offline_values
|
|
77
|
+
|
|
78
|
+
:values_awaited
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def restart_in_a_forked_process(**); end
|
|
82
|
+
|
|
83
|
+
def close; end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def deliver_the_offline_values
|
|
88
|
+
@delivering_the_offline_values = true
|
|
89
|
+
yield @payload_of_the_offline_values.call
|
|
90
|
+
ensure
|
|
91
|
+
@delivering_the_offline_values = false
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Connection
|
|
8
|
+
module OfflineValuesFile
|
|
9
|
+
def self.values_in(path)
|
|
10
|
+
document = document_in(text_of(path), path)
|
|
11
|
+
return document if document.is_a?(Hash)
|
|
12
|
+
|
|
13
|
+
raise ConfigDirectorInitializationError,
|
|
14
|
+
"The offline values file '#{path}' must hold a JSON object with one value per config key, " \
|
|
15
|
+
"but holds #{description_of(document)}."
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.text_of(path)
|
|
19
|
+
File.open(path, "r:UTF-8", &:read)
|
|
20
|
+
rescue SystemCallError, IOError, ArgumentError => e
|
|
21
|
+
raise ConfigDirectorInitializationError, "The offline values file '#{path}' could not be read: #{e.message}"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.document_in(text, path)
|
|
25
|
+
unless text.valid_encoding?
|
|
26
|
+
raise ConfigDirectorInitializationError,
|
|
27
|
+
"The offline values file '#{path}' is not valid JSON: the text is not valid UTF-8."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
JSON.parse(text, allow_duplicate_key: true)
|
|
31
|
+
rescue JSON::ParserError => e
|
|
32
|
+
raise ConfigDirectorInitializationError, "The offline values file '#{path}' is not valid JSON: #{e.message}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.description_of(document)
|
|
36
|
+
case document
|
|
37
|
+
when nil then "null"
|
|
38
|
+
when Array then "an array"
|
|
39
|
+
when String then "a string"
|
|
40
|
+
when true, false then "a boolean"
|
|
41
|
+
else "a number"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private_class_method :text_of, :document_in, :description_of
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Connection
|
|
6
|
+
class PayloadsDeliveredInTurn
|
|
7
|
+
def initialize
|
|
8
|
+
@lock = Mutex.new
|
|
9
|
+
@waiting_payloads = []
|
|
10
|
+
@delivering = false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add(payload)
|
|
14
|
+
@lock.synchronize { @waiting_payloads << payload }
|
|
15
|
+
nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def deliver_each(&deliver_payload)
|
|
19
|
+
loop do
|
|
20
|
+
case @lock.synchronize { delivery_of_the_waiting_payloads }
|
|
21
|
+
when :left_to_another_call then return nil
|
|
22
|
+
when :taken_by_this_call then deliver_every_waiting_payload(&deliver_payload)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def delivery_of_the_waiting_payloads
|
|
30
|
+
return :left_to_another_call if @delivering || @waiting_payloads.empty?
|
|
31
|
+
|
|
32
|
+
@delivering = true
|
|
33
|
+
:taken_by_this_call
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def deliver_every_waiting_payload
|
|
37
|
+
while (payload = @lock.synchronize { @waiting_payloads.shift })
|
|
38
|
+
yield payload
|
|
39
|
+
end
|
|
40
|
+
ensure
|
|
41
|
+
@lock.synchronize { @delivering = false }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Connection
|
|
6
|
+
PollingInterval = Data.define(:configured_seconds)
|
|
7
|
+
|
|
8
|
+
class PollingInterval
|
|
9
|
+
MINIMUM_SECONDS = 60
|
|
10
|
+
|
|
11
|
+
def raised_to_the_minimum?
|
|
12
|
+
configured_seconds < MINIMUM_SECONDS
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def seconds
|
|
16
|
+
raised_to_the_minimum? ? MINIMUM_SECONDS : configured_seconds
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "timeout"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Connection
|
|
8
|
+
class RequestDeadline
|
|
9
|
+
SECONDS_THE_TIMER_OF_THE_SESSION_STAYS_BEHIND = 1
|
|
10
|
+
|
|
11
|
+
private_constant :SECONDS_THE_TIMER_OF_THE_SESSION_STAYS_BEHIND
|
|
12
|
+
|
|
13
|
+
def self.on_the_clock_of_the_process(seconds_the_request_may_take)
|
|
14
|
+
new(
|
|
15
|
+
seconds_the_request_may_take: seconds_the_request_may_take,
|
|
16
|
+
monotonic_seconds: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(seconds_the_request_may_take:, monotonic_seconds:)
|
|
21
|
+
@seconds_the_request_may_take = seconds_the_request_may_take
|
|
22
|
+
@monotonic_seconds = monotonic_seconds
|
|
23
|
+
@passes_at = monotonic_seconds.call + seconds_the_request_may_take
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def description_of_having_passed
|
|
27
|
+
"the SDK server gave no complete answer within #{format("%g", @seconds_the_request_may_take)} seconds"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def ending_the_exchange_when_it_passes(http_session, &exchange)
|
|
31
|
+
seconds_before_it_passes = seconds_left
|
|
32
|
+
http_session.open_timeout = seconds_before_it_passes + SECONDS_THE_TIMER_OF_THE_SESSION_STAYS_BEHIND
|
|
33
|
+
Timeout.timeout(seconds_before_it_passes) { exchange.call }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def seconds_left
|
|
37
|
+
seconds_left = @passes_at - @monotonic_seconds.call
|
|
38
|
+
raise Timeout::Error unless seconds_left.positive?
|
|
39
|
+
|
|
40
|
+
seconds_left
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Connection
|
|
8
|
+
module RetryAfterHeader
|
|
9
|
+
ASCII_DIGITS = /\A[0-9]+\z/
|
|
10
|
+
|
|
11
|
+
private_constant :ASCII_DIGITS
|
|
12
|
+
|
|
13
|
+
def self.seconds_to_wait(header_value, now)
|
|
14
|
+
return nil if header_value.nil?
|
|
15
|
+
return Integer(header_value, 10) if header_value.match?(ASCII_DIGITS)
|
|
16
|
+
|
|
17
|
+
[Time.httpdate(header_value) - now, 0].max
|
|
18
|
+
rescue ArgumentError
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Connection
|
|
6
|
+
ServerSdkPayload = Data.define(:delta, :configs, :segments, :unreadable_configs, :unreadable_segments)
|
|
7
|
+
|
|
8
|
+
class ServerSdkPayload
|
|
9
|
+
alias delta? delta
|
|
10
|
+
|
|
11
|
+
def self.without_definitions
|
|
12
|
+
of_definitions({})
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.of_definitions(configs)
|
|
16
|
+
new(delta: false, configs: configs, segments: {}, unreadable_configs: {}, unreadable_segments: {})
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.delta_of_definitions(configs)
|
|
20
|
+
new(delta: true, configs: configs, segments: {}, unreadable_configs: {}, unreadable_segments: {})
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def configs_held_after(held_configs)
|
|
24
|
+
delta? ? held_configs.except(*unreadable_configs.keys).merge(configs) : configs
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def segments_held_after(held_segments)
|
|
28
|
+
delta? ? held_segments.except(*unreadable_segments.keys).merge(segments) : segments
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def segment_ids
|
|
32
|
+
segments.keys + unreadable_segments.keys
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Connection
|
|
8
|
+
module ServerSdkPayloadParser
|
|
9
|
+
DELTA_KIND = "delta"
|
|
10
|
+
|
|
11
|
+
private_constant :DELTA_KIND
|
|
12
|
+
|
|
13
|
+
def self.payload_in(payload_text)
|
|
14
|
+
wire_payload = Value::ParsedJson.of(payload_text)
|
|
15
|
+
raise NotAServerSdkPayload, "it is not a JSON object" unless wire_payload.is_a?(Hash)
|
|
16
|
+
|
|
17
|
+
wire_configs = wire_payload["configs"]
|
|
18
|
+
raise NotAServerSdkPayload, "it carries no configs object" unless wire_configs.is_a?(Hash)
|
|
19
|
+
|
|
20
|
+
wire_segments = wire_payload["segments"]
|
|
21
|
+
payload_defined_by(wire_payload["kind"], wire_configs, wire_segments.is_a?(Hash) ? wire_segments : {})
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.payload_defined_by(wire_kind, wire_configs, wire_segments)
|
|
25
|
+
configs, unreadable_configs = definitions_read_in(wire_configs) do |wire_config|
|
|
26
|
+
config_defined_by(wire_config)
|
|
27
|
+
end
|
|
28
|
+
segments, unreadable_segments = definitions_read_in(wire_segments) do |wire_segment|
|
|
29
|
+
segment_defined_by(wire_segment)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
ServerSdkPayload.new(
|
|
33
|
+
delta: wire_kind == DELTA_KIND, configs: configs, segments: segments,
|
|
34
|
+
unreadable_configs: unreadable_configs, unreadable_segments: unreadable_segments
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.definitions_read_in(wire_definitions)
|
|
39
|
+
definitions_or_failures = wire_definitions.map do |config_key_or_segment_id, wire_definition|
|
|
40
|
+
[config_key_or_segment_id, yield(wire_definition)]
|
|
41
|
+
rescue UnreadableDefinition => e
|
|
42
|
+
[config_key_or_segment_id, e]
|
|
43
|
+
end
|
|
44
|
+
failures, definitions = definitions_or_failures.partition do |_, definition_or_failure|
|
|
45
|
+
definition_or_failure.is_a?(UnreadableDefinition)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
[definitions.to_h, failures.to_h.transform_values(&:message)]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.config_defined_by(wire_config)
|
|
52
|
+
wire_target = WireFields.object_or_nil_in(wire_config, "target")
|
|
53
|
+
raise UnreadableDefinition, "the config has no target" if wire_target.nil?
|
|
54
|
+
|
|
55
|
+
ConfigDefinition.new(
|
|
56
|
+
id: WireFields.text_required_in(wire_config, "id", "the config"),
|
|
57
|
+
key: WireFields.text_required_in(wire_config, "key", "the config"),
|
|
58
|
+
type: WireFields.text_required_in(wire_config, "type", "the config"),
|
|
59
|
+
targeting_rules: TargetingRulesParser.targeting_rules_defined_by(wire_target),
|
|
60
|
+
variations: variations_listed_in(wire_config)
|
|
61
|
+
)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.variations_listed_in(wire_config)
|
|
65
|
+
WireFields.list_in(wire_config, "variations", "the config").filter_map do |wire_variation|
|
|
66
|
+
variation_id = wire_variation.is_a?(Hash) ? WireFields.text_or_nil_in(wire_variation, "id") : nil
|
|
67
|
+
next if variation_id.nil?
|
|
68
|
+
|
|
69
|
+
Variation.new(id: variation_id, name: WireFields.text_or_nil_in(wire_variation, "name"))
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.segment_defined_by(wire_segment)
|
|
74
|
+
wire_condition_groups = WireFields.list_or_nil_in(wire_segment, "groups")
|
|
75
|
+
raise UnreadableDefinition, "the segment has no groups" if wire_condition_groups.nil?
|
|
76
|
+
|
|
77
|
+
Evaluation::Segment.new(
|
|
78
|
+
condition_groups: wire_condition_groups.map do |wire_condition_group|
|
|
79
|
+
ConditionParser.condition_group_defined_by(wire_condition_group)
|
|
80
|
+
end
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private_class_method :payload_defined_by, :definitions_read_in, :config_defined_by, :variations_listed_in,
|
|
85
|
+
:segment_defined_by
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Connection
|
|
6
|
+
module TargetingRulesParser
|
|
7
|
+
def self.targeting_rules_defined_by(wire_target)
|
|
8
|
+
Evaluation::TargetingRules.new(
|
|
9
|
+
default_value: Evaluation::ScalarText.of(wire_target["defaultValue"]) || "",
|
|
10
|
+
default_value_id: WireFields.text_or_nil_in(wire_target, "defaultValueId"),
|
|
11
|
+
default_variation_id: WireFields.text_or_nil_in(wire_target, "defaultVariationId"),
|
|
12
|
+
rules: WireFields.objects_listed_in(wire_target, "rules", "the config", "a rule").map do |wire_rule|
|
|
13
|
+
rule_defined_by(wire_rule)
|
|
14
|
+
end
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.rule_defined_by(wire_rule)
|
|
19
|
+
rule_id = WireFields.text_required_in(wire_rule, "id", "a rule")
|
|
20
|
+
Evaluation::Rule.new(
|
|
21
|
+
id: rule_id, order: WireFields.whole_number_or_nil_in(wire_rule, "order"),
|
|
22
|
+
type: WireFields.text_or_empty_in(wire_rule, "type"),
|
|
23
|
+
target: WireFields.text_or_empty_in(wire_rule, "target"),
|
|
24
|
+
value: Evaluation::ScalarText.of(wire_rule["value"]),
|
|
25
|
+
value_id: WireFields.text_or_nil_in(wire_rule, "valueId"),
|
|
26
|
+
variation_id: WireFields.text_or_nil_in(wire_rule, "variationId"),
|
|
27
|
+
conditions: conditions_listed_in(wire_rule, rule_id), shares: shares_listed_in(wire_rule, rule_id)
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.conditions_listed_in(wire_rule, rule_id)
|
|
32
|
+
wire_conditions = WireFields.objects_listed_in(wire_rule, "conditions", "rule #{rule_id}", "a condition")
|
|
33
|
+
|
|
34
|
+
wire_conditions.map { |wire_condition| ConditionParser.condition_defined_by(wire_condition) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.shares_listed_in(wire_rule, rule_id)
|
|
38
|
+
return nil if wire_rule["percentages"].nil?
|
|
39
|
+
|
|
40
|
+
WireFields.list_in(wire_rule, "percentages", "rule #{rule_id}").map do |wire_share|
|
|
41
|
+
share_defined_by(wire_share)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.share_defined_by(wire_share)
|
|
46
|
+
percentage = finite_percentage_of(wire_share)
|
|
47
|
+
|
|
48
|
+
Evaluation::Share.new(
|
|
49
|
+
id: WireFields.text_required_in(wire_share, "id", "a rollout share"), percentage: percentage,
|
|
50
|
+
value: Evaluation::ScalarText.of(wire_share["value"]),
|
|
51
|
+
value_id: WireFields.text_or_nil_in(wire_share, "valueId"),
|
|
52
|
+
variation_id: WireFields.text_or_nil_in(wire_share, "variationId")
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.finite_percentage_of(wire_share)
|
|
57
|
+
percentage = wire_share.is_a?(Hash) ? wire_share["percentage"] : nil
|
|
58
|
+
unless percentage.is_a?(Integer) || percentage.is_a?(Float)
|
|
59
|
+
raise UnreadableDefinition, "a rollout share has no percentage"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
percentage.clamp(-Float::MAX, Float::MAX).to_f
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private_class_method :rule_defined_by, :conditions_listed_in, :shares_listed_in, :share_defined_by,
|
|
66
|
+
:finite_percentage_of
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|