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,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class EventHandlers
|
|
7
|
+
HOOKS_BY_EVENT_NAME = {
|
|
8
|
+
client_ready: ->(hooks) { hooks.client_ready },
|
|
9
|
+
configs_updated: ->(hooks) { hooks.configs_updated },
|
|
10
|
+
config_evaluated: ->(hooks) { hooks.config_evaluated },
|
|
11
|
+
connection_error: ->(hooks) { hooks.connection_error }
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
HandlerRegistration = Data.define(:handler)
|
|
15
|
+
|
|
16
|
+
private_constant :HOOKS_BY_EVENT_NAME, :HandlerRegistration
|
|
17
|
+
|
|
18
|
+
def self.one_handler_of(handler, block)
|
|
19
|
+
raise ConfigDirectorValidationError, "Give a handler or a block, not both." unless handler.nil? || block.nil?
|
|
20
|
+
|
|
21
|
+
chosen_handler = handler.nil? ? block : handler
|
|
22
|
+
return chosen_handler if chosen_handler.respond_to?(:call)
|
|
23
|
+
|
|
24
|
+
raise ConfigDirectorValidationError,
|
|
25
|
+
"Invalid handler of class #{chosen_handler.class}. Give a block or an object that responds to call."
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(hooks, log)
|
|
29
|
+
@log = log
|
|
30
|
+
@registrations = Registrations.new
|
|
31
|
+
register_the_hooks_in(hooks)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def add(event_name, handler)
|
|
35
|
+
reject_an_unknown_event_name(event_name)
|
|
36
|
+
register(event_name, handler)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def remove(event_name, handler)
|
|
40
|
+
reject_an_unknown_event_name(event_name)
|
|
41
|
+
if handler.nil?
|
|
42
|
+
@registrations.remove_all_on(event_name)
|
|
43
|
+
else
|
|
44
|
+
@registrations.remove_first(event_name) { |registration| registration.handler == handler }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def remove_all
|
|
49
|
+
@registrations.remove_all
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def deliver_client_ready
|
|
53
|
+
deliver(:client_ready)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def deliver_configs_updated(update)
|
|
57
|
+
deliver(:configs_updated, update)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def deliver_config_evaluated(evaluation)
|
|
61
|
+
deliver(:config_evaluated, ConfigEvaluatedEvent.new(evaluation: evaluation))
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def deliver_connection_error(error)
|
|
65
|
+
deliver(:connection_error, ConnectionErrorEvent.new(error: error))
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def reject_an_unknown_event_name(event_name)
|
|
71
|
+
return if HOOKS_BY_EVENT_NAME.key?(event_name)
|
|
72
|
+
|
|
73
|
+
raise ConfigDirectorValidationError,
|
|
74
|
+
"Unknown event #{event_name.inspect}. " \
|
|
75
|
+
"Expected one of: #{HOOKS_BY_EVENT_NAME.keys.map(&:inspect).join(", ")}."
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def register(event_name, handler)
|
|
79
|
+
@registrations.add(event_name, HandlerRegistration.new(handler: handler))
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def register_the_hooks_in(hooks)
|
|
83
|
+
HOOKS_BY_EVENT_NAME.each do |event_name, hooks_of_the_event|
|
|
84
|
+
handlers_given(hooks_of_the_event.call(hooks)).each { |handler| register_a_hook(event_name, handler) }
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def register_a_hook(event_name, handler)
|
|
89
|
+
return register(event_name, handler) if handler.respond_to?(:call)
|
|
90
|
+
|
|
91
|
+
@log.warn(
|
|
92
|
+
"A #{event_name.inspect} hook of class #{handler.class} was ignored because it does not respond to call."
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def handlers_given(hooks_of_the_event)
|
|
97
|
+
case hooks_of_the_event
|
|
98
|
+
when nil then []
|
|
99
|
+
when Array then hooks_of_the_event
|
|
100
|
+
else [hooks_of_the_event]
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def deliver(event_name, *arguments)
|
|
105
|
+
@registrations.on([event_name]).each do |registration|
|
|
106
|
+
@log.ignoring_errors_from("A handler for the event #{event_name.inspect}") do
|
|
107
|
+
registration.handler.call(*arguments)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class EventTracking
|
|
7
|
+
TRACKS_EVENTS_BY_MODE = {
|
|
8
|
+
"streaming" => true,
|
|
9
|
+
"polling" => true,
|
|
10
|
+
"offline" => false,
|
|
11
|
+
"test" => true
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
private_constant :TRACKS_EVENTS_BY_MODE
|
|
15
|
+
|
|
16
|
+
def initialize(mode:, telemetry:, log:)
|
|
17
|
+
@tracks_events = TRACKS_EVENTS_BY_MODE.fetch(mode)
|
|
18
|
+
@telemetry = telemetry
|
|
19
|
+
@log = log
|
|
20
|
+
@context_admission = ContextAdmission.new(log, telemetry)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def track(event_name, context, value:, details:)
|
|
24
|
+
return unless @tracks_events
|
|
25
|
+
|
|
26
|
+
admission = Telemetry::TrackedEventAdmission.of(
|
|
27
|
+
name: event_name, context: @context_admission.admitted_context_or_none(context), value: value,
|
|
28
|
+
details: details, happened_at: Time.now
|
|
29
|
+
)
|
|
30
|
+
@telemetry.record_tracked_event(admission).each { |warning| @log.warn(warning) }
|
|
31
|
+
nil
|
|
32
|
+
rescue StandardError => e
|
|
33
|
+
@log.warn("A tracked event was not reported because it could not be read: #{e.class}: #{e.message.inspect}")
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class LiveClientConnections
|
|
7
|
+
def initialize
|
|
8
|
+
@lock = Mutex.new
|
|
9
|
+
@client_connections = []
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def add(client_connection)
|
|
13
|
+
@lock.synchronize { @client_connections = without(client_connection) + [client_connection] }
|
|
14
|
+
nil
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def remove(client_connection)
|
|
18
|
+
@lock.synchronize { @client_connections = without(client_connection) }
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def restart_in_a_forked_process(forked_through)
|
|
23
|
+
client_connections = @client_connections
|
|
24
|
+
return if client_connections.empty?
|
|
25
|
+
|
|
26
|
+
client_connections.each do |client_connection|
|
|
27
|
+
client_connection.choose_the_reporter_of_what_the_parent_process_collected(forked_through)
|
|
28
|
+
end
|
|
29
|
+
restart_each_on_a_thread_of_the_forked_process(client_connections)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
OF_THE_PROCESS = new
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def without(client_connection)
|
|
37
|
+
@client_connections.reject { |added| added.equal?(client_connection) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def restart_each_on_a_thread_of_the_forked_process(client_connections)
|
|
41
|
+
Thread.new { restart_each_in_a_forked_process(client_connections) }
|
|
42
|
+
nil
|
|
43
|
+
rescue ThreadError
|
|
44
|
+
restart_each_in_a_forked_process(client_connections)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def restart_each_in_a_forked_process(client_connections)
|
|
48
|
+
client_connections.each do |client_connection|
|
|
49
|
+
client_connection.restart_in_a_forked_process
|
|
50
|
+
rescue StandardError
|
|
51
|
+
nil
|
|
52
|
+
end
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
module NumberOfSeconds
|
|
7
|
+
def self.finite?(value)
|
|
8
|
+
case value
|
|
9
|
+
when Integer then value.abs <= Float::MAX
|
|
10
|
+
when Float then value.finite?
|
|
11
|
+
else false
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.finite_and_positive?(value)
|
|
16
|
+
finite?(value) && value.positive?
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class Registrations
|
|
7
|
+
def initialize
|
|
8
|
+
@lock = Mutex.new
|
|
9
|
+
@registrations_by_topic = {}
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def add(topic, registration)
|
|
13
|
+
@lock.synchronize { (@registrations_by_topic[topic] ||= []) << registration }
|
|
14
|
+
Subscription.new { remove_first(topic) { |registered| registered.equal?(registration) } }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def remove_first(topic, &matches)
|
|
18
|
+
@lock.synchronize do
|
|
19
|
+
registrations = @registrations_by_topic.fetch(topic, [])
|
|
20
|
+
position = registrations.index(&matches)
|
|
21
|
+
registrations.delete_at(position) unless position.nil?
|
|
22
|
+
@registrations_by_topic.delete(topic) if registrations.empty?
|
|
23
|
+
end
|
|
24
|
+
nil
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def remove_all_on(topic)
|
|
28
|
+
@lock.synchronize { @registrations_by_topic.delete(topic) }
|
|
29
|
+
nil
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def remove_all
|
|
33
|
+
@lock.synchronize { @registrations_by_topic.clear }
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def on(topics)
|
|
38
|
+
@lock.synchronize { topics.flat_map { |topic| @registrations_by_topic.fetch(topic, []).dup } }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
module RestartInForkedProcesses
|
|
7
|
+
if Process.respond_to?(:_fork)
|
|
8
|
+
def _fork
|
|
9
|
+
process_id = super
|
|
10
|
+
LiveClientConnections::OF_THE_PROCESS.restart_in_a_forked_process(:fork) if process_id.zero?
|
|
11
|
+
process_id
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
if Process.respond_to?(:daemon)
|
|
16
|
+
def daemon(*)
|
|
17
|
+
result = super
|
|
18
|
+
LiveClientConnections::OF_THE_PROCESS.restart_in_a_forked_process(:daemon)
|
|
19
|
+
result
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
ServedConfigs = Data.define(:configs, :segments, :metadata)
|
|
7
|
+
|
|
8
|
+
class ServedConfigs
|
|
9
|
+
REASON_BY_SERVED_BY = {
|
|
10
|
+
matching_rule: "found-match",
|
|
11
|
+
default_targeting_rule: "found-match",
|
|
12
|
+
default_targeting_rule_after_skipped_rollout: "rollout-skipped-no-identifier"
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
private_constant :REASON_BY_SERVED_BY
|
|
16
|
+
|
|
17
|
+
def self.without_definitions(metadata)
|
|
18
|
+
new(configs: {}, segments: {}, metadata: metadata)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def after(payload)
|
|
22
|
+
ServedConfigs.new(
|
|
23
|
+
configs: payload.configs_held_after(configs), segments: payload.segments_held_after(segments),
|
|
24
|
+
metadata: metadata
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def update_since(replaced_configs, payload)
|
|
29
|
+
ConfigsUpdatedEvent.new(
|
|
30
|
+
keys: payload.configs.keys | keys_of_configs_using_any_segment_of(payload.segment_ids),
|
|
31
|
+
removed_keys: replaced_configs.configs.keys - configs.keys
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def evaluated_read(config_read)
|
|
36
|
+
definition = configs[config_read.config_key]
|
|
37
|
+
return config_read.evaluated_to_the_in_code_default_value("config-state-missing", nil) if definition.nil?
|
|
38
|
+
|
|
39
|
+
evaluated_read_of(definition, config_read)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def states(context, config_keys)
|
|
43
|
+
configs
|
|
44
|
+
.select { |config_key, _definition| config_keys.nil? || config_keys.include?(config_key) }
|
|
45
|
+
.transform_values { |definition| definition.state_serving(value_served_to(context, definition)) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def keys_of_configs_using_any_segment_of(segment_ids)
|
|
51
|
+
configs.select { |_config_key, definition| definition.uses_any_segment_of?(segment_ids) }.keys
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def evaluated_read_of(definition, config_read)
|
|
55
|
+
served = value_served_to(config_read.context, definition)
|
|
56
|
+
state = definition.state_serving(served)
|
|
57
|
+
typed_value_or_reason = Value::ServedText.typed_as(
|
|
58
|
+
config_read.in_code_default_value.kind, state.value, state.type
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
case typed_value_or_reason
|
|
62
|
+
when Value::TypedValue
|
|
63
|
+
config_read.evaluated_to(typed_value_or_reason, state, REASON_BY_SERVED_BY.fetch(served.served_by))
|
|
64
|
+
else config_read.evaluated_to_the_in_code_default_value(typed_value_or_reason, state)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def value_served_to(context, definition)
|
|
69
|
+
Evaluation::ConfigEvaluator.evaluate(
|
|
70
|
+
definition.id, definition.targeting_rules, evaluation_context_of(context), segments
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def evaluation_context_of(context)
|
|
75
|
+
Evaluation::EvaluationContext.new(
|
|
76
|
+
identifier: context&.id, kind: context&.kind, name: context&.name, traits: context&.traits || {},
|
|
77
|
+
app_name: metadata.app_name, app_version: metadata.app_version
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class ServingState
|
|
7
|
+
def initialize(metadata)
|
|
8
|
+
@metadata = metadata
|
|
9
|
+
@lock = Mutex.new
|
|
10
|
+
@first_values_no_longer_awaited = ConditionVariable.new
|
|
11
|
+
@served_configs = nil
|
|
12
|
+
@thread_delivering_the_first_values = nil
|
|
13
|
+
@first_values_delivered = false
|
|
14
|
+
@closed = false
|
|
15
|
+
@connection_failed_unrecoverably = false
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def served_configs
|
|
19
|
+
@lock.synchronize { @served_configs }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def closed?
|
|
23
|
+
@lock.synchronize { @closed }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def connection_failed_unrecoverably?
|
|
27
|
+
@lock.synchronize { @connection_failed_unrecoverably }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def serve(payload)
|
|
31
|
+
@lock.synchronize do
|
|
32
|
+
arrival = arrival_of_values
|
|
33
|
+
replaced_configs = @served_configs || ServedConfigs.without_definitions(@metadata)
|
|
34
|
+
served_configs = replaced_configs.after(payload)
|
|
35
|
+
update = served_configs.update_since(replaced_configs, payload)
|
|
36
|
+
@served_configs = served_configs unless arrival == :after_close
|
|
37
|
+
@thread_delivering_the_first_values = Thread.current if arrival == :first_values
|
|
38
|
+
|
|
39
|
+
[arrival, update]
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def end_the_delivery_of_the_first_values
|
|
44
|
+
@lock.synchronize do
|
|
45
|
+
@first_values_delivered = true
|
|
46
|
+
@thread_delivering_the_first_values = nil
|
|
47
|
+
@first_values_no_longer_awaited.broadcast
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def close
|
|
52
|
+
@lock.synchronize do
|
|
53
|
+
@closed = true
|
|
54
|
+
@served_configs = nil
|
|
55
|
+
@first_values_no_longer_awaited.broadcast
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def expect_values_from_the_connection
|
|
60
|
+
@lock.synchronize { @connection_failed_unrecoverably = false }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def expect_no_values_from_the_connection
|
|
64
|
+
@lock.synchronize do
|
|
65
|
+
@connection_failed_unrecoverably = true
|
|
66
|
+
@first_values_no_longer_awaited.broadcast
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def leave_the_delivery_of_the_first_values_to_the_parent_process
|
|
71
|
+
@lock.synchronize do
|
|
72
|
+
@first_values_delivered = true unless @served_configs.nil?
|
|
73
|
+
@first_values_no_longer_awaited.broadcast
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def timed_out_waiting_for_the_first_values?(give_up_at_monotonic_seconds)
|
|
78
|
+
@lock.synchronize do
|
|
79
|
+
loop do
|
|
80
|
+
return false if @closed || @connection_failed_unrecoverably
|
|
81
|
+
return false if @first_values_delivered || @thread_delivering_the_first_values.equal?(Thread.current)
|
|
82
|
+
|
|
83
|
+
remaining_seconds = give_up_at_monotonic_seconds - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
84
|
+
return @served_configs.nil? unless remaining_seconds.positive?
|
|
85
|
+
|
|
86
|
+
@first_values_no_longer_awaited.wait(@lock, remaining_seconds)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
def arrival_of_values
|
|
94
|
+
if @closed then :after_close
|
|
95
|
+
elsif @served_configs.nil? then :first_values
|
|
96
|
+
else :newer_values
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class Watches
|
|
7
|
+
Watch = Data.define(:config_read, :callback)
|
|
8
|
+
|
|
9
|
+
class Watch
|
|
10
|
+
def config_key
|
|
11
|
+
config_read.config_key
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def receive(value)
|
|
15
|
+
callback.call(value)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private_constant :Watch
|
|
20
|
+
|
|
21
|
+
def initialize(log)
|
|
22
|
+
@log = log
|
|
23
|
+
@registrations = Registrations.new
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def add(config_read, callback)
|
|
27
|
+
@registrations.add(config_read.config_key, Watch.new(config_read: config_read, callback: callback))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def remove(config_key, callback)
|
|
31
|
+
if callback.nil?
|
|
32
|
+
@registrations.remove_all_on(config_key)
|
|
33
|
+
else
|
|
34
|
+
@registrations.remove_first(config_key) { |watch| watch.callback == callback }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def remove_all
|
|
39
|
+
@registrations.remove_all
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def deliver_on(config_keys)
|
|
43
|
+
@registrations.on(config_keys).each do |watch|
|
|
44
|
+
value = yield watch.config_read
|
|
45
|
+
@log.ignoring_errors_from("A watch on #{watch.config_key.inspect}") { watch.receive(value) }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
module WhatFollowsAFork
|
|
7
|
+
WHAT_FOLLOWS_A_FORK_BY_MODE = {
|
|
8
|
+
"streaming" => :restart_the_connection,
|
|
9
|
+
"polling" => :restart_the_connection,
|
|
10
|
+
"offline" => :nothing_to_restart,
|
|
11
|
+
"test" => :nothing_to_restart
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
private_constant :WHAT_FOLLOWS_A_FORK_BY_MODE
|
|
15
|
+
|
|
16
|
+
def self.for_a_client_with(mode:, connection_opened:, closed:, connection_failed_unrecoverably:)
|
|
17
|
+
return :nothing_to_restart if !connection_opened || closed || connection_failed_unrecoverably
|
|
18
|
+
|
|
19
|
+
WHAT_FOLLOWS_A_FORK_BY_MODE.fetch(mode)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Connection
|
|
8
|
+
module ConditionParser
|
|
9
|
+
ATTRIBUTE_KIND = "attribute"
|
|
10
|
+
SEGMENT_KIND = "segment"
|
|
11
|
+
|
|
12
|
+
private_constant :ATTRIBUTE_KIND, :SEGMENT_KIND
|
|
13
|
+
|
|
14
|
+
def self.condition_defined_by(wire_condition)
|
|
15
|
+
kind = kind_named_in(wire_condition)
|
|
16
|
+
case kind
|
|
17
|
+
when ATTRIBUTE_KIND then attribute_condition_defined_by(wire_condition)
|
|
18
|
+
when SEGMENT_KIND then segment_condition_defined_by(wire_condition)
|
|
19
|
+
else
|
|
20
|
+
raise UnreadableDefinition,
|
|
21
|
+
"a condition has the unknown kind '#{Evaluation::ScalarText.of(kind) || JSON.generate(kind)}'"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.condition_group_defined_by(wire_condition_group)
|
|
26
|
+
unless wire_condition_group.is_a?(Array)
|
|
27
|
+
raise UnreadableDefinition, "a condition group of the segment is not a list"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
wire_condition_group.map do |wire_condition|
|
|
31
|
+
unless wire_condition.is_a?(Hash) && kind_named_in(wire_condition) == ATTRIBUTE_KIND
|
|
32
|
+
raise UnreadableDefinition, "a condition group of the segment holds attribute conditions only"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
attribute_condition_defined_by(wire_condition)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.kind_named_in(wire_condition)
|
|
40
|
+
kind = wire_condition["kind"]
|
|
41
|
+
|
|
42
|
+
kind.nil? ? ATTRIBUTE_KIND : kind
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.attribute_condition_defined_by(wire_condition)
|
|
46
|
+
Evaluation::AttributeCondition.new(
|
|
47
|
+
attribute: WireFields.text_required_in(wire_condition, "attribute", "a condition"),
|
|
48
|
+
trait: WireFields.text_or_nil_in(wire_condition, "trait"),
|
|
49
|
+
operator: WireFields.text_required_in(wire_condition, "operator", "a condition"),
|
|
50
|
+
target_type: WireFields.text_required_in(wire_condition, "targetType", "a condition"),
|
|
51
|
+
target_values: WireFields.list_in(wire_condition, "targetValues", "a condition").map do |wire_target_value|
|
|
52
|
+
target_value_text_of(wire_target_value)
|
|
53
|
+
end
|
|
54
|
+
)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.target_value_text_of(wire_target_value)
|
|
58
|
+
Evaluation::ScalarText.of(wire_target_value) ||
|
|
59
|
+
raise(UnreadableDefinition, "a condition has a target value that is not text")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.segment_condition_defined_by(wire_condition)
|
|
63
|
+
Evaluation::SegmentCondition.new(
|
|
64
|
+
operator: WireFields.text_required_in(wire_condition, "operator", "a segment condition"),
|
|
65
|
+
segment_id: WireFields.text_required_in(wire_condition, "segmentId", "a segment condition")
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private_class_method :kind_named_in, :attribute_condition_defined_by, :target_value_text_of,
|
|
70
|
+
:segment_condition_defined_by
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Connection
|
|
6
|
+
ConfigDefinition = Data.define(:id, :key, :type, :targeting_rules, :variations)
|
|
7
|
+
|
|
8
|
+
class ConfigDefinition
|
|
9
|
+
def self.serving_each_value_to_every_context(values, id_prefix)
|
|
10
|
+
Value::NativeValues.config_keys_of(values).zip(values.values).to_h do |config_key, value|
|
|
11
|
+
encoded_value = Value::EncodedNativeValue.of(value, config_key)
|
|
12
|
+
targeting_rules = Evaluation::TargetingRules.new(
|
|
13
|
+
default_value: encoded_value.served_text, default_value_id: nil, default_variation_id: nil, rules: []
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
[config_key, new(id: "#{id_prefix}#{config_key}", key: config_key, type: encoded_value.config_type,
|
|
17
|
+
targeting_rules: targeting_rules, variations: [])]
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def uses_any_segment_of?(segment_ids)
|
|
22
|
+
targeting_rules.rules.any? { |rule| rule.uses_any_segment_of?(segment_ids) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def state_serving(served)
|
|
26
|
+
rollout_share = served.rollout_share
|
|
27
|
+
|
|
28
|
+
ConfigState.new(
|
|
29
|
+
id: id, key: key, type: type, value: served.value, value_id: served.value_id,
|
|
30
|
+
variation: variations.find { |variation| variation.id == served.variation_id },
|
|
31
|
+
rule_id: rollout_share&.rule_id, share_id: rollout_share&.share_id
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Connection
|
|
8
|
+
module HttpSession
|
|
9
|
+
def self.to(uri)
|
|
10
|
+
Net::HTTP.new(uri.hostname.to_s, uri.port).tap { |http_session| http_session.use_ssl = uri.scheme == "https" }
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|