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,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
class StoppableThread
|
|
6
|
+
class Stopped < Exception; end
|
|
7
|
+
|
|
8
|
+
ID_OF_THE_PROCESS_THAT_STARTED_THE_THREAD = :configdirector_id_of_the_process_that_started_the_thread
|
|
9
|
+
ASKED_ITSELF_TO_STOP = :configdirector_thread_asked_itself_to_stop
|
|
10
|
+
|
|
11
|
+
private_constant :Stopped, :ID_OF_THE_PROCESS_THAT_STARTED_THE_THREAD, :ASKED_ITSELF_TO_STOP
|
|
12
|
+
|
|
13
|
+
def self.start(name, &work)
|
|
14
|
+
thread = Thread.handle_interrupt(Stopped => :never) do
|
|
15
|
+
Thread.new do
|
|
16
|
+
Thread.current.thread_variable_set(ID_OF_THE_PROCESS_THAT_STARTED_THE_THREAD, Process.pid)
|
|
17
|
+
work.call
|
|
18
|
+
rescue Stopped
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
thread.name = name
|
|
23
|
+
new(thread)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.stoppable(&blocking_call)
|
|
27
|
+
stop_here_when_the_thread_must_end
|
|
28
|
+
Thread.handle_interrupt(Stopped => :on_blocking) { blocking_call.call }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.unstoppable(&work)
|
|
32
|
+
Thread.handle_interrupt(Stopped => :never) { work.call }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.stop_here_if_asked_to
|
|
36
|
+
stop_here_when_the_thread_must_end
|
|
37
|
+
Thread.handle_interrupt(Stopped => :immediate) { Thread.pass }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.stop_here_when_the_thread_must_end
|
|
41
|
+
raise Stopped if copied_by_a_forked_process? || Thread.current.thread_variable_get(ASKED_ITSELF_TO_STOP)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.copied_by_a_forked_process?
|
|
45
|
+
id_of_the_starting_process = Thread.current.thread_variable_get(ID_OF_THE_PROCESS_THAT_STARTED_THE_THREAD)
|
|
46
|
+
|
|
47
|
+
!id_of_the_starting_process.nil? && id_of_the_starting_process != Process.pid
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private_class_method :stop_here_when_the_thread_must_end, :copied_by_a_forked_process?
|
|
51
|
+
|
|
52
|
+
def initialize(thread)
|
|
53
|
+
@thread = thread
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def stop
|
|
57
|
+
if @thread.equal?(Thread.current)
|
|
58
|
+
@thread.thread_variable_set(ASKED_ITSELF_TO_STOP, true)
|
|
59
|
+
else
|
|
60
|
+
@thread.raise(Stopped)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def join_within(seconds)
|
|
65
|
+
@thread.join(seconds) unless @thread.equal?(Thread.current)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Streaming
|
|
6
|
+
class Heartbeats
|
|
7
|
+
def initialize(endpoint:, timing:, log:)
|
|
8
|
+
@endpoint = endpoint
|
|
9
|
+
@timing = timing
|
|
10
|
+
@log = log
|
|
11
|
+
@lock = Mutex.new
|
|
12
|
+
@session_id_of_the_open_stream = nil
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def stream_opened(session_id)
|
|
16
|
+
@lock.synchronize { @session_id_of_the_open_stream = session_id }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def stream_ended
|
|
20
|
+
@lock.synchronize { @session_id_of_the_open_stream = nil }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def send_on_every_interval
|
|
24
|
+
loop do
|
|
25
|
+
StoppableThread.stoppable { sleep(@timing.seconds_between_heartbeats) }
|
|
26
|
+
session_id = @lock.synchronize { @session_id_of_the_open_stream }
|
|
27
|
+
send_for(session_id) unless session_id.nil?
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def send_for(session_id)
|
|
34
|
+
answer = StoppableThread.stoppable { answer_to_a_heartbeat_of(session_id) }
|
|
35
|
+
@log.debug { "Heartbeat failed with status: #{answer.code}" } unless answer.is_a?(Net::HTTPSuccess)
|
|
36
|
+
rescue StandardError => e
|
|
37
|
+
@log.debug { "Heartbeat failed: #{e.class}: #{e.message}" }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def answer_to_a_heartbeat_of(session_id)
|
|
41
|
+
http_session = @endpoint.http_session(@timing)
|
|
42
|
+
@timing.deadline_of_a_heartbeat.ending_the_exchange_when_it_passes(http_session) do
|
|
43
|
+
http_session.start do
|
|
44
|
+
http_session.request(@endpoint.heartbeat_request(session_id: session_id)) { |response| break response }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Streaming
|
|
6
|
+
class ReconnectingStream
|
|
7
|
+
ATTEMPTS_LOGGED_AT_INFO = 5
|
|
8
|
+
|
|
9
|
+
private_constant :ATTEMPTS_LOGGED_AT_INFO
|
|
10
|
+
|
|
11
|
+
def initialize(endpoint:, timing:, heartbeats:, log:)
|
|
12
|
+
@endpoint = endpoint
|
|
13
|
+
@timing = timing
|
|
14
|
+
@heartbeats = heartbeats
|
|
15
|
+
@log = log
|
|
16
|
+
@attempt = 0
|
|
17
|
+
@last_event_id = ""
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def keep_open(report_unrecoverable_error:, &deliver_event_data)
|
|
21
|
+
loop do
|
|
22
|
+
stream_end = stream_until_it_ends(&deliver_event_data)
|
|
23
|
+
@last_event_id = stream_end.last_event_id
|
|
24
|
+
case stream_end.what_follows
|
|
25
|
+
when :no_reconnection then return
|
|
26
|
+
when :unrecoverable_error then return stop_for_good(stream_end.status, report_unrecoverable_error)
|
|
27
|
+
when :reconnection_after_an_open_stream then pause_before_attempt(1, stream_end)
|
|
28
|
+
when :reconnection_after_a_failed_attempt then pause_before_attempt(@attempt + 1, stream_end)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def stream_until_it_ends(&deliver_event_data)
|
|
36
|
+
connection = StreamConnection.new(
|
|
37
|
+
endpoint: @endpoint, timing: @timing, last_event_id: @last_event_id
|
|
38
|
+
) { |event_data| deliver(event_data, &deliver_event_data) }
|
|
39
|
+
stream_end = connection.stream_until_it_ends { take_note_of_the_open_stream_of(connection) }
|
|
40
|
+
@log.debug { "Disconnected: #{stream_end.cause}" }
|
|
41
|
+
stream_end
|
|
42
|
+
ensure
|
|
43
|
+
@heartbeats.stream_ended
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def take_note_of_the_open_stream_of(connection)
|
|
47
|
+
@heartbeats.stream_opened(connection.session_id)
|
|
48
|
+
@log.debug { "Connected" }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def deliver(event_data)
|
|
52
|
+
begin
|
|
53
|
+
yield event_data
|
|
54
|
+
rescue StandardError => e
|
|
55
|
+
@log.error("Error parsing and dispatching config data update: #{e.class}: #{e.message.inspect}")
|
|
56
|
+
end
|
|
57
|
+
StoppableThread.stop_here_if_asked_to
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def stop_for_good(status, report_unrecoverable_error)
|
|
61
|
+
error = ConfigDirectorConnectionError.new(
|
|
62
|
+
"Connection failed with status: #{status}. This is an unrecoverable error, will not attempt to reconnect.",
|
|
63
|
+
status: status
|
|
64
|
+
)
|
|
65
|
+
@log.error(error.message)
|
|
66
|
+
report_unrecoverable_error.call(error)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def pause_before_attempt(attempt, stream_end)
|
|
70
|
+
@attempt = attempt
|
|
71
|
+
seconds = ReconnectionDelay.seconds_before_attempt(
|
|
72
|
+
attempt, seconds_the_server_asked_to_wait: stream_end.seconds_the_server_asked_to_wait,
|
|
73
|
+
random_fraction: @timing.random_fraction.call
|
|
74
|
+
)
|
|
75
|
+
line = "Scheduling reconnect attempt ##{attempt} in #{format("%g", seconds)} seconds."
|
|
76
|
+
attempt > ATTEMPTS_LOGGED_AT_INFO ? @log.warn(line) : @log.info(line)
|
|
77
|
+
StoppableThread.stoppable { @timing.pause_before_reconnecting.call(seconds) }
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Streaming
|
|
6
|
+
module ReconnectionDelay
|
|
7
|
+
MAXIMUM_BACKOFF_EXPONENT = 9
|
|
8
|
+
SHORTEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS = 1
|
|
9
|
+
LONGEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS = 3600
|
|
10
|
+
|
|
11
|
+
private_constant :MAXIMUM_BACKOFF_EXPONENT, :SHORTEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS,
|
|
12
|
+
:LONGEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS
|
|
13
|
+
|
|
14
|
+
def self.seconds_before_attempt(attempt, seconds_the_server_asked_to_wait:, random_fraction:)
|
|
15
|
+
unless seconds_the_server_asked_to_wait.nil?
|
|
16
|
+
return seconds_the_server_asked_to_wait.clamp(
|
|
17
|
+
SHORTEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS, LONGEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS
|
|
18
|
+
)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
ceiling_seconds = 1 << [attempt, MAXIMUM_BACKOFF_EXPONENT].min
|
|
22
|
+
fixed_seconds = ceiling_seconds / 2.0
|
|
23
|
+
|
|
24
|
+
fixed_seconds + (random_fraction * (ceiling_seconds - fixed_seconds))
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Streaming
|
|
8
|
+
class StreamConnection
|
|
9
|
+
attr_reader :session_id
|
|
10
|
+
|
|
11
|
+
def initialize(endpoint:, timing:, last_event_id:, &event_data_received)
|
|
12
|
+
@endpoint = endpoint
|
|
13
|
+
@timing = timing
|
|
14
|
+
@session_id = SecureRandom.uuid
|
|
15
|
+
@process_id_of_the_process_that_streams = Process.pid
|
|
16
|
+
@last_event_id_sent = last_event_id
|
|
17
|
+
@parser = ServerSentEvents::EventStreamParser.new(last_event_id: last_event_id) do |event|
|
|
18
|
+
event_data_received.call(event.data)
|
|
19
|
+
end
|
|
20
|
+
@answer = nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def stream_until_it_ends(&stream_opened)
|
|
24
|
+
StoppableThread.stoppable { request_the_stream(stream_opened) }
|
|
25
|
+
|
|
26
|
+
stream_end_caused_by("the SDK server closed the stream")
|
|
27
|
+
rescue StandardError => e
|
|
28
|
+
stream_end_caused_by("#{e.class}: #{e.message}")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def request_the_stream(stream_opened)
|
|
34
|
+
http_session = @endpoint.http_session(@timing)
|
|
35
|
+
http_session.start
|
|
36
|
+
begin
|
|
37
|
+
read_the_answer_to_the_stream_request(http_session, stream_opened)
|
|
38
|
+
ensure
|
|
39
|
+
finish_unless_a_forked_process_copied(http_session)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def read_the_answer_to_the_stream_request(http_session, stream_opened)
|
|
44
|
+
request = @endpoint.stream_request(session_id: @session_id, last_event_id: @last_event_id_sent)
|
|
45
|
+
http_session.request(request) do |response|
|
|
46
|
+
@answer = response
|
|
47
|
+
break unless StreamEnd.stream_opened_by?(status_of(response))
|
|
48
|
+
|
|
49
|
+
read_the_stream_in(response, stream_opened)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def finish_unless_a_forked_process_copied(http_session)
|
|
54
|
+
http_session.finish if Process.pid == @process_id_of_the_process_that_streams && http_session.started?
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def read_the_stream_in(response, stream_opened)
|
|
58
|
+
StoppableThread.unstoppable { stream_opened.call }
|
|
59
|
+
response.read_body { |chunk| StoppableThread.unstoppable { @parser.feed(chunk) } }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def stream_end_caused_by(cause)
|
|
63
|
+
answer = @answer
|
|
64
|
+
return StreamEnd.unreachable(cause, last_event_id: @parser.last_event_id) if answer.nil?
|
|
65
|
+
return dropped_stream_end_caused_by(cause) if StreamEnd.stream_opened_by?(status_of(answer))
|
|
66
|
+
|
|
67
|
+
seconds_named_by_retry_after = Connection::RetryAfterHeader.seconds_to_wait(answer["Retry-After"], Time.now)
|
|
68
|
+
StreamEnd.answered_without_a_stream(
|
|
69
|
+
status_of(answer), seconds_named_by_retry_after: seconds_named_by_retry_after,
|
|
70
|
+
last_event_id: @parser.last_event_id
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def status_of(answer)
|
|
75
|
+
Integer(answer.code, 10)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def dropped_stream_end_caused_by(cause)
|
|
79
|
+
StreamEnd.dropped(
|
|
80
|
+
cause, milliseconds_named_by_the_stream: @parser.retry_milliseconds, last_event_id: @parser.last_event_id
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Streaming
|
|
6
|
+
StreamEnd = Data.define(:kind, :cause, :status, :seconds_the_server_asked_to_wait, :last_event_id)
|
|
7
|
+
|
|
8
|
+
class StreamEnd
|
|
9
|
+
WHAT_FOLLOWS_BY_KIND = {
|
|
10
|
+
unreachable: :reconnection_after_a_failed_attempt,
|
|
11
|
+
refused: :reconnection_after_a_failed_attempt,
|
|
12
|
+
throttled: :reconnection_after_a_failed_attempt,
|
|
13
|
+
dropped: :reconnection_after_an_open_stream,
|
|
14
|
+
refused_unrecoverably: :unrecoverable_error,
|
|
15
|
+
ended_without_content: :no_reconnection
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
SUCCESS_STATUSES = (200..299)
|
|
19
|
+
NO_CONTENT_STATUS = 204
|
|
20
|
+
TOO_MANY_REQUESTS_STATUS = 429
|
|
21
|
+
CLIENT_ERROR_STATUSES = (400..499)
|
|
22
|
+
MILLISECONDS_PER_SECOND = 1000.0
|
|
23
|
+
|
|
24
|
+
private_constant :WHAT_FOLLOWS_BY_KIND, :SUCCESS_STATUSES, :NO_CONTENT_STATUS, :TOO_MANY_REQUESTS_STATUS,
|
|
25
|
+
:CLIENT_ERROR_STATUSES, :MILLISECONDS_PER_SECOND
|
|
26
|
+
|
|
27
|
+
def self.stream_opened_by?(status)
|
|
28
|
+
SUCCESS_STATUSES.cover?(status) && status != NO_CONTENT_STATUS
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.unreachable(cause, last_event_id:)
|
|
32
|
+
new(kind: :unreachable, cause: cause, status: nil, seconds_the_server_asked_to_wait: nil,
|
|
33
|
+
last_event_id: last_event_id)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.dropped(cause, milliseconds_named_by_the_stream:, last_event_id:)
|
|
37
|
+
new(
|
|
38
|
+
kind: :dropped, cause: cause, status: nil,
|
|
39
|
+
seconds_the_server_asked_to_wait: milliseconds_named_by_the_stream&.fdiv(MILLISECONDS_PER_SECOND),
|
|
40
|
+
last_event_id: last_event_id
|
|
41
|
+
)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.answered_without_a_stream(status, seconds_named_by_retry_after:, last_event_id:)
|
|
45
|
+
case status
|
|
46
|
+
when NO_CONTENT_STATUS then without_a_stream(:ended_without_content, status, last_event_id)
|
|
47
|
+
when TOO_MANY_REQUESTS_STATUS
|
|
48
|
+
without_a_stream(:throttled, status, last_event_id, seconds_named_by_retry_after)
|
|
49
|
+
when CLIENT_ERROR_STATUSES then without_a_stream(:refused_unrecoverably, status, last_event_id)
|
|
50
|
+
else without_a_stream(:refused, status, last_event_id)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.without_a_stream(kind, status, last_event_id, seconds_the_server_asked_to_wait = nil)
|
|
55
|
+
new(
|
|
56
|
+
kind: kind, cause: "the SDK server answered with status #{status}", status: status,
|
|
57
|
+
seconds_the_server_asked_to_wait: seconds_the_server_asked_to_wait, last_event_id: last_event_id
|
|
58
|
+
)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private_class_method :without_a_stream
|
|
62
|
+
|
|
63
|
+
def what_follows
|
|
64
|
+
WHAT_FOLLOWS_BY_KIND.fetch(kind)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Streaming
|
|
8
|
+
class StreamEndpoint
|
|
9
|
+
def initialize(url:, server_sdk_key:, metadata:, sdk_identity:)
|
|
10
|
+
base_url = url.delete_suffix("/")
|
|
11
|
+
@stream_uri = URI("#{base_url}/server/sse/v1")
|
|
12
|
+
@heartbeat_uri = URI("#{base_url}/server/heartbeat/v1")
|
|
13
|
+
@server_sdk_key = server_sdk_key
|
|
14
|
+
@sdk_identity = sdk_identity
|
|
15
|
+
@meta_context = sdk_identity.meta_context_of_an_application_with(metadata)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def http_session(timing)
|
|
19
|
+
Connection::HttpSession.to(@stream_uri).tap do |http_session|
|
|
20
|
+
http_session.open_timeout = timing.seconds_to_open_a_stream
|
|
21
|
+
http_session.read_timeout = timing.seconds_of_silence_of_a_dropped_stream
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def stream_request(session_id:, last_event_id:)
|
|
26
|
+
request = Connection::JsonPostRequest.to(
|
|
27
|
+
@stream_uri, @sdk_identity,
|
|
28
|
+
{ serverSdkKey: @server_sdk_key, sessionId: session_id, metaContext: @meta_context }
|
|
29
|
+
)
|
|
30
|
+
request["Accept"] = "text/event-stream"
|
|
31
|
+
request["Last-Event-ID"] = last_event_id unless last_event_id.empty?
|
|
32
|
+
request
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def heartbeat_request(session_id:)
|
|
36
|
+
Connection::JsonPostRequest.to(
|
|
37
|
+
@heartbeat_uri, @sdk_identity, { serverSdkKey: @server_sdk_key, sessionId: session_id }
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Streaming
|
|
6
|
+
StreamTiming = Data.define(
|
|
7
|
+
:seconds_between_heartbeats,
|
|
8
|
+
:seconds_of_silence_of_a_dropped_stream,
|
|
9
|
+
:seconds_to_open_a_stream,
|
|
10
|
+
:pause_before_reconnecting,
|
|
11
|
+
:random_fraction
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
class StreamTiming
|
|
15
|
+
def deadline_of_a_heartbeat
|
|
16
|
+
Connection::RequestDeadline.on_the_clock_of_the_process(
|
|
17
|
+
seconds_to_open_a_stream.to_f + seconds_of_silence_of_a_dropped_stream.to_f
|
|
18
|
+
)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.of_the_protocol
|
|
22
|
+
new(
|
|
23
|
+
seconds_between_heartbeats: 90,
|
|
24
|
+
seconds_of_silence_of_a_dropped_stream: 45,
|
|
25
|
+
seconds_to_open_a_stream: 10,
|
|
26
|
+
pause_before_reconnecting: ->(seconds) { sleep(seconds) },
|
|
27
|
+
random_fraction: -> { Random.rand }
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Streaming
|
|
6
|
+
class StreamingModeConnection
|
|
7
|
+
def self.for_a_client_with(server_sdk_key:, metadata:, connection_options:, sdk_identity:, logger:)
|
|
8
|
+
endpoint = StreamEndpoint.new(
|
|
9
|
+
url: connection_options.url, server_sdk_key: server_sdk_key, metadata: metadata,
|
|
10
|
+
sdk_identity: sdk_identity
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
new(endpoint, logger)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(endpoint, logger)
|
|
17
|
+
@endpoint = endpoint
|
|
18
|
+
@logger = logger
|
|
19
|
+
@log = Logging::ClientLog.new(logger, component: "StreamingTransport")
|
|
20
|
+
@transport = transport_of_a_new_session
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def open(report_unrecoverable_error:, &deliver_payload)
|
|
24
|
+
open_a_stream(report_unrecoverable_error: report_unrecoverable_error, &deliver_payload)
|
|
25
|
+
|
|
26
|
+
:values_awaited
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def restart_in_a_forked_process(report_unrecoverable_error:, &deliver_payload)
|
|
30
|
+
@transport = transport_of_a_new_session
|
|
31
|
+
open_a_stream(report_unrecoverable_error: report_unrecoverable_error, &deliver_payload)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def close
|
|
35
|
+
@transport.close
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def transport_of_a_new_session
|
|
41
|
+
StreamingTransport.new(endpoint: @endpoint, logger: @logger)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def open_a_stream(report_unrecoverable_error:)
|
|
45
|
+
@transport.open(report_unrecoverable_error: report_unrecoverable_error) do |event_data|
|
|
46
|
+
payload = payload_or_nothing_in(event_data)
|
|
47
|
+
yield payload unless payload.nil?
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def payload_or_nothing_in(event_data)
|
|
52
|
+
Connection::ServerSdkPayloadParser.payload_in(event_data)
|
|
53
|
+
rescue Connection::NotAServerSdkPayload => e
|
|
54
|
+
@log.debug { "Skipping a message that is not a server SDK payload: #{e.message}" }
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Streaming
|
|
6
|
+
class StreamingTransport
|
|
7
|
+
SECONDS_CLOSE_WAITS_FOR_THE_THREADS = 5
|
|
8
|
+
|
|
9
|
+
private_constant :SECONDS_CLOSE_WAITS_FOR_THE_THREADS
|
|
10
|
+
|
|
11
|
+
def initialize(endpoint:, logger:, timing: StreamTiming.of_the_protocol)
|
|
12
|
+
log = Logging::ClientLog.new(logger, component: "StreamingTransport")
|
|
13
|
+
@heartbeats = Heartbeats.new(endpoint: endpoint, timing: timing, log: log)
|
|
14
|
+
@stream = ReconnectingStream.new(endpoint: endpoint, timing: timing, heartbeats: @heartbeats, log: log)
|
|
15
|
+
@lock = Mutex.new
|
|
16
|
+
@threads = []
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def open(report_unrecoverable_error:, &deliver_event_data)
|
|
20
|
+
@lock.synchronize do
|
|
21
|
+
heartbeat_thread = StoppableThread.start("configdirector-heartbeat") { @heartbeats.send_on_every_interval }
|
|
22
|
+
stream_thread = StoppableThread.start("configdirector-streaming") do
|
|
23
|
+
@stream.keep_open(report_unrecoverable_error: report_unrecoverable_error, &deliver_event_data)
|
|
24
|
+
ensure
|
|
25
|
+
heartbeat_thread.stop
|
|
26
|
+
end
|
|
27
|
+
@threads = [stream_thread, heartbeat_thread]
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def close
|
|
32
|
+
threads = @lock.synchronize { @threads }
|
|
33
|
+
threads.each(&:stop)
|
|
34
|
+
give_up_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + SECONDS_CLOSE_WAITS_FOR_THE_THREADS
|
|
35
|
+
threads.each do |thread|
|
|
36
|
+
thread.join_within([give_up_at - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
module AggregatedWireEntries
|
|
7
|
+
LARGEST_COUNT_THE_SDK_SERVER_READS = 2_147_483_647
|
|
8
|
+
|
|
9
|
+
private_constant :LARGEST_COUNT_THE_SDK_SERVER_READS
|
|
10
|
+
|
|
11
|
+
def self.count_the_sdk_server_reads(count)
|
|
12
|
+
[count, LARGEST_COUNT_THE_SDK_SERVER_READS].min
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.of(counts_by_wire_event, counted_since:, counted_until:)
|
|
16
|
+
start_time = ReportedTime.of(counted_since || counted_until)
|
|
17
|
+
end_time = ReportedTime.of(counted_until)
|
|
18
|
+
|
|
19
|
+
counts_by_wire_event.map do |wire_event, count|
|
|
20
|
+
{ startTime: start_time, endTime: end_time, count: count_the_sdk_server_reads(count), event: wire_event }
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|