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,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Polling
|
|
6
|
+
PollAnswer = Data.define(
|
|
7
|
+
:kind, :payload_text, :timestamp, :failure_message, :status, :seconds_named_by_retry_after
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
class PollAnswer
|
|
11
|
+
WHAT_FOLLOWS_BY_KIND = {
|
|
12
|
+
payload: :delivery,
|
|
13
|
+
unchanged: :no_delivery,
|
|
14
|
+
unanswered: :error_log,
|
|
15
|
+
timed_out: :error_log,
|
|
16
|
+
refused: :error_log,
|
|
17
|
+
throttled: :error_log,
|
|
18
|
+
unreadable: :error_log,
|
|
19
|
+
refused_unrecoverably: :end_of_polling
|
|
20
|
+
}.freeze
|
|
21
|
+
|
|
22
|
+
SENT_AGAIN_AT_ONCE_BY_KIND = {
|
|
23
|
+
payload: false,
|
|
24
|
+
unchanged: false,
|
|
25
|
+
unanswered: true,
|
|
26
|
+
timed_out: false,
|
|
27
|
+
refused: false,
|
|
28
|
+
throttled: false,
|
|
29
|
+
unreadable: false,
|
|
30
|
+
refused_unrecoverably: false
|
|
31
|
+
}.freeze
|
|
32
|
+
|
|
33
|
+
LONGEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS = 3600
|
|
34
|
+
|
|
35
|
+
private_constant :WHAT_FOLLOWS_BY_KIND, :SENT_AGAIN_AT_ONCE_BY_KIND,
|
|
36
|
+
:LONGEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS
|
|
37
|
+
|
|
38
|
+
def self.payload(payload_text, timestamp:)
|
|
39
|
+
new(
|
|
40
|
+
kind: :payload, payload_text: payload_text, timestamp: timestamp, failure_message: nil, status: nil,
|
|
41
|
+
seconds_named_by_retry_after: nil
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.unchanged
|
|
46
|
+
new(
|
|
47
|
+
kind: :unchanged, payload_text: nil, timestamp: nil, failure_message: nil, status: nil,
|
|
48
|
+
seconds_named_by_retry_after: nil
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.unanswered(cause)
|
|
53
|
+
failed(:unanswered, "Connection failed with error: #{cause}.")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.timed_out(description_of_the_deadline_having_passed)
|
|
57
|
+
failed(:timed_out, "Connection failed with error: #{description_of_the_deadline_having_passed}.")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.refused(status)
|
|
61
|
+
failed(:refused, "Connection failed with status: #{status}", status: status)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.throttled(status, seconds_named_by_retry_after)
|
|
65
|
+
failed(
|
|
66
|
+
:throttled, "Connection failed with status: #{status}",
|
|
67
|
+
status: status, seconds_named_by_retry_after: seconds_named_by_retry_after
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.refused_unrecoverably(status, detail:)
|
|
72
|
+
failed(
|
|
73
|
+
:refused_unrecoverably,
|
|
74
|
+
"Connection failed with status: #{status}#{" (#{detail})" unless detail.empty?}. " \
|
|
75
|
+
"This is an unrecoverable error, retry attempts will be ignored.",
|
|
76
|
+
status: status
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def self.unreadable(cause)
|
|
81
|
+
failed(:unreadable, "Failed to parse the response from the server: #{cause}")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.failed(kind, failure_message, status: nil, seconds_named_by_retry_after: nil)
|
|
85
|
+
new(
|
|
86
|
+
kind: kind, payload_text: nil, timestamp: nil, failure_message: failure_message, status: status,
|
|
87
|
+
seconds_named_by_retry_after: seconds_named_by_retry_after
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
private_class_method :failed
|
|
92
|
+
|
|
93
|
+
def what_follows
|
|
94
|
+
WHAT_FOLLOWS_BY_KIND.fetch(kind)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def sent_again_at_once?
|
|
98
|
+
SENT_AGAIN_AT_ONCE_BY_KIND.fetch(kind)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def seconds_before_the_next_poll(seconds_between_polls)
|
|
102
|
+
seconds_the_server_asked_for = [
|
|
103
|
+
seconds_named_by_retry_after || 0, LONGEST_WAIT_THE_SERVER_CAN_ASK_FOR_SECONDS
|
|
104
|
+
].min
|
|
105
|
+
|
|
106
|
+
[seconds_between_polls, seconds_the_server_asked_for].max
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "timeout"
|
|
5
|
+
|
|
6
|
+
module ConfigDirector
|
|
7
|
+
module Internal
|
|
8
|
+
module Polling
|
|
9
|
+
class PollRequest
|
|
10
|
+
MAXIMUM_BODY_MEBIBYTES = 32
|
|
11
|
+
MAXIMUM_BODY_BYTES = MAXIMUM_BODY_MEBIBYTES * 1024 * 1024
|
|
12
|
+
PAYLOAD_STATUS = 200
|
|
13
|
+
SUCCESS_STATUSES = (200..299)
|
|
14
|
+
TOO_MANY_REQUESTS_STATUS = 429
|
|
15
|
+
CLIENT_ERROR_STATUSES = (400..499)
|
|
16
|
+
|
|
17
|
+
private_constant :MAXIMUM_BODY_MEBIBYTES, :MAXIMUM_BODY_BYTES, :PAYLOAD_STATUS, :SUCCESS_STATUSES,
|
|
18
|
+
:TOO_MANY_REQUESTS_STATUS, :CLIENT_ERROR_STATUSES
|
|
19
|
+
|
|
20
|
+
def initialize(endpoint:, session_id:, last_update_timestamp:, deadline:)
|
|
21
|
+
@endpoint = endpoint
|
|
22
|
+
@session_id = session_id
|
|
23
|
+
@last_update_timestamp = last_update_timestamp
|
|
24
|
+
@deadline = deadline
|
|
25
|
+
@http_session = endpoint.http_session
|
|
26
|
+
@body_text_read = ""
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def answer
|
|
30
|
+
answer_in(response_to_the_post)
|
|
31
|
+
rescue Timeout::Error
|
|
32
|
+
PollAnswer.timed_out(@deadline.description_of_having_passed)
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
PollAnswer.unanswered("#{e.class}: #{e.message}")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def response_to_the_post
|
|
40
|
+
@deadline.ending_the_exchange_when_it_passes(@http_session) do
|
|
41
|
+
@http_session.start { response_read_until_over_the_limit }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def response_read_until_over_the_limit
|
|
46
|
+
@http_session.read_timeout = @deadline.seconds_left
|
|
47
|
+
@http_session.request(json_post) do |response|
|
|
48
|
+
@body_text_read = body_text_read_until_over_the_limit(response)
|
|
49
|
+
break response
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def json_post
|
|
54
|
+
@endpoint.json_post_of_a_poll(session_id: @session_id, last_update_timestamp: @last_update_timestamp)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def body_text_read_until_over_the_limit(response)
|
|
58
|
+
body = String.new
|
|
59
|
+
@http_session.read_timeout = @deadline.seconds_left
|
|
60
|
+
response.read_body do |chunk|
|
|
61
|
+
body << chunk
|
|
62
|
+
break if body.bytesize > MAXIMUM_BODY_BYTES
|
|
63
|
+
|
|
64
|
+
@http_session.read_timeout = @deadline.seconds_left
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
body.force_encoding(Encoding::UTF_8).scrub!
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def answer_in(response)
|
|
71
|
+
status = Integer(response.code, 10)
|
|
72
|
+
|
|
73
|
+
case status
|
|
74
|
+
when PAYLOAD_STATUS then answer_holding_the_payload
|
|
75
|
+
when SUCCESS_STATUSES then PollAnswer.unchanged
|
|
76
|
+
when TOO_MANY_REQUESTS_STATUS then PollAnswer.throttled(status, retry_after_seconds_in(response))
|
|
77
|
+
when CLIENT_ERROR_STATUSES then PollAnswer.refused_unrecoverably(status, detail: refusal_detail)
|
|
78
|
+
else PollAnswer.refused(status)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def answer_holding_the_payload
|
|
83
|
+
if @body_text_read.bytesize > MAXIMUM_BODY_BYTES
|
|
84
|
+
return PollAnswer.unreadable("it is larger than #{MAXIMUM_BODY_MEBIBYTES} MiB")
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
payload = Value::ParsedJson.of(@body_text_read)
|
|
88
|
+
return PollAnswer.unreadable("it is not a JSON object") unless payload.is_a?(Hash)
|
|
89
|
+
|
|
90
|
+
timestamp = payload["timestamp"]
|
|
91
|
+
PollAnswer.payload(@body_text_read, timestamp: timestamp.is_a?(String) ? timestamp : nil)
|
|
92
|
+
rescue JSON::ParserError => e
|
|
93
|
+
PollAnswer.unreadable(e.message)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def retry_after_seconds_in(response)
|
|
97
|
+
Connection::RetryAfterHeader.seconds_to_wait(response["Retry-After"], Time.now)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def refusal_detail
|
|
101
|
+
@endpoint.refusal_detail_in(@body_text_read)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Polling
|
|
6
|
+
PollTiming = Data.define(
|
|
7
|
+
:seconds_between_polls,
|
|
8
|
+
:seconds_a_poll_may_take,
|
|
9
|
+
:pause_before_the_next_poll,
|
|
10
|
+
:monotonic_seconds
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
class PollTiming
|
|
14
|
+
def self.on_the_clock_of_the_process(seconds_between_polls:, seconds_a_poll_may_take:)
|
|
15
|
+
new(
|
|
16
|
+
seconds_between_polls: seconds_between_polls,
|
|
17
|
+
seconds_a_poll_may_take: seconds_a_poll_may_take,
|
|
18
|
+
pause_before_the_next_poll: ->(seconds) { sleep(seconds) },
|
|
19
|
+
monotonic_seconds: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Polling
|
|
8
|
+
class PollingEndpoint
|
|
9
|
+
def initialize(url:, server_sdk_key:, metadata:, sdk_identity:)
|
|
10
|
+
@poll_uri = URI("#{url.delete_suffix("/")}/server/polling/v1")
|
|
11
|
+
@server_sdk_key = server_sdk_key
|
|
12
|
+
@sdk_identity = sdk_identity
|
|
13
|
+
@meta_context = sdk_identity.meta_context_of_an_application_with(metadata)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def http_session
|
|
17
|
+
Connection::HttpSession.to(@poll_uri).tap { |http_session| http_session.ignore_eof = false }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def json_post_of_a_poll(session_id:, last_update_timestamp:)
|
|
21
|
+
Connection::JsonPostRequest.to(
|
|
22
|
+
@poll_uri, @sdk_identity,
|
|
23
|
+
{
|
|
24
|
+
serverSdkKey: @server_sdk_key, sessionId: session_id, metaContext: @meta_context,
|
|
25
|
+
lastUpdateTimestamp: last_update_timestamp
|
|
26
|
+
}.compact
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def refusal_detail_in(answer_body)
|
|
31
|
+
RefusalDetail.of(answer_body, server_sdk_key_to_hide: @server_sdk_key)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Polling
|
|
6
|
+
class PollingModeConnection
|
|
7
|
+
OPENING_BY_WHAT_FOLLOWS_THE_FIRST_POLL = {
|
|
8
|
+
delivery: :values_awaited,
|
|
9
|
+
no_delivery: :values_awaited,
|
|
10
|
+
error_log: :first_attempt_failed,
|
|
11
|
+
end_of_polling: :first_attempt_failed
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
private_constant :OPENING_BY_WHAT_FOLLOWS_THE_FIRST_POLL
|
|
15
|
+
|
|
16
|
+
def self.for_a_client_with(server_sdk_key:, metadata:, connection_options:, sdk_identity:, logger:)
|
|
17
|
+
endpoint = PollingEndpoint.new(
|
|
18
|
+
url: connection_options.url, server_sdk_key: server_sdk_key, metadata: metadata,
|
|
19
|
+
sdk_identity: sdk_identity
|
|
20
|
+
)
|
|
21
|
+
polling_interval = Connection::PollingInterval.new(configured_seconds: connection_options.polling_interval)
|
|
22
|
+
timing = PollTiming.on_the_clock_of_the_process(
|
|
23
|
+
seconds_between_polls: polling_interval.seconds, seconds_a_poll_may_take: connection_options.timeout
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
new(PollingTransport.new(endpoint: endpoint, timing: timing, logger: logger))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def initialize(transport)
|
|
30
|
+
@transport = transport
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def open(report_unrecoverable_error:, &deliver_payload)
|
|
34
|
+
what_follows_the_first_poll = @transport.open(
|
|
35
|
+
report_unrecoverable_error: report_unrecoverable_error
|
|
36
|
+
) { |payload_text| deliver_payload.call(Connection::ServerSdkPayloadParser.payload_in(payload_text)) }
|
|
37
|
+
|
|
38
|
+
OPENING_BY_WHAT_FOLLOWS_THE_FIRST_POLL.fetch(what_follows_the_first_poll)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def restart_in_a_forked_process(report_unrecoverable_error:, &deliver_payload)
|
|
42
|
+
@transport.restart_in_a_forked_process(
|
|
43
|
+
report_unrecoverable_error: report_unrecoverable_error
|
|
44
|
+
) { |payload_text| deliver_payload.call(Connection::ServerSdkPayloadParser.payload_in(payload_text)) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def close
|
|
48
|
+
@transport.close
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Polling
|
|
8
|
+
class PollingSession
|
|
9
|
+
def initialize(endpoint:, timing:, log:, last_update_timestamp: nil)
|
|
10
|
+
@endpoint = endpoint
|
|
11
|
+
@timing = timing
|
|
12
|
+
@log = log
|
|
13
|
+
@session_id = SecureRandom.uuid
|
|
14
|
+
@last_update_timestamp = last_update_timestamp
|
|
15
|
+
@seconds_before_the_next_poll = timing.seconds_between_polls
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def poll(report_unrecoverable_error:, &deliver_payload_text)
|
|
19
|
+
answer = answer_retried_once_when_unanswered
|
|
20
|
+
@seconds_before_the_next_poll = answer.seconds_before_the_next_poll(@timing.seconds_between_polls)
|
|
21
|
+
|
|
22
|
+
what_follows = answer.what_follows
|
|
23
|
+
|
|
24
|
+
case what_follows
|
|
25
|
+
when :delivery then return deliver(answer, &deliver_payload_text)
|
|
26
|
+
when :no_delivery then nil
|
|
27
|
+
when :error_log then log_the_failure_of(answer)
|
|
28
|
+
when :end_of_polling then log_and_report_the_unrecoverable_error_of(answer, report_unrecoverable_error)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
what_follows
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def session_of_a_forked_process
|
|
35
|
+
PollingSession.new(
|
|
36
|
+
endpoint: @endpoint, timing: @timing, log: @log, last_update_timestamp: @last_update_timestamp
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def poll_at_once_and_on_every_interval(report_unrecoverable_error:, &deliver_payload_text)
|
|
41
|
+
what_follows_the_poll = poll(report_unrecoverable_error: report_unrecoverable_error, &deliver_payload_text)
|
|
42
|
+
return if what_follows_the_poll == :end_of_polling
|
|
43
|
+
|
|
44
|
+
poll_on_every_interval(report_unrecoverable_error: report_unrecoverable_error, &deliver_payload_text)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def poll_on_every_interval(report_unrecoverable_error:, &deliver_payload_text)
|
|
48
|
+
loop do
|
|
49
|
+
StoppableThread.stoppable { @timing.pause_before_the_next_poll.call(@seconds_before_the_next_poll) }
|
|
50
|
+
what_follows_the_poll = poll(report_unrecoverable_error: report_unrecoverable_error, &deliver_payload_text)
|
|
51
|
+
break if what_follows_the_poll == :end_of_polling
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def answer_retried_once_when_unanswered
|
|
58
|
+
deadline = Connection::RequestDeadline.new(
|
|
59
|
+
seconds_the_request_may_take: @timing.seconds_a_poll_may_take,
|
|
60
|
+
monotonic_seconds: @timing.monotonic_seconds
|
|
61
|
+
)
|
|
62
|
+
answer = answer_before(deadline)
|
|
63
|
+
return answer unless answer.sent_again_at_once?
|
|
64
|
+
|
|
65
|
+
@log.debug { "The poll got no answer and is sent again at once" }
|
|
66
|
+
answer_before(deadline)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def answer_before(deadline)
|
|
70
|
+
request = PollRequest.new(
|
|
71
|
+
endpoint: @endpoint, session_id: @session_id, last_update_timestamp: @last_update_timestamp,
|
|
72
|
+
deadline: deadline
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
StoppableThread.stoppable { request.answer }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def deliver(answer)
|
|
79
|
+
yield answer.payload_text.to_s
|
|
80
|
+
@last_update_timestamp = answer.timestamp || @last_update_timestamp
|
|
81
|
+
:delivery
|
|
82
|
+
rescue StandardError => e
|
|
83
|
+
log_the_failure_of(PollAnswer.unreadable("#{e.class}: #{e.message.inspect}"))
|
|
84
|
+
:error_log
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def log_the_failure_of(answer)
|
|
88
|
+
@log.error("Error during polling: #{answer.failure_message}")
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def log_and_report_the_unrecoverable_error_of(answer, report_unrecoverable_error)
|
|
92
|
+
error = ConfigDirectorConnectionError.new(answer.failure_message, status: answer.status)
|
|
93
|
+
@log.error(error.message)
|
|
94
|
+
report_unrecoverable_error.call(error)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Polling
|
|
6
|
+
class PollingTransport
|
|
7
|
+
SECONDS_CLOSE_WAITS_FOR_THE_THREAD = 5
|
|
8
|
+
|
|
9
|
+
private_constant :SECONDS_CLOSE_WAITS_FOR_THE_THREAD
|
|
10
|
+
|
|
11
|
+
def initialize(endpoint:, timing:, logger:)
|
|
12
|
+
@session = PollingSession.new(
|
|
13
|
+
endpoint: endpoint, timing: timing, log: Logging::ClientLog.new(logger, component: "PollingTransport")
|
|
14
|
+
)
|
|
15
|
+
@lock = Mutex.new
|
|
16
|
+
@closed = false
|
|
17
|
+
@polling_thread = nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def open(report_unrecoverable_error:, &deliver_payload_text)
|
|
21
|
+
what_follows_the_first_poll = @session.poll(
|
|
22
|
+
report_unrecoverable_error: report_unrecoverable_error, &deliver_payload_text
|
|
23
|
+
)
|
|
24
|
+
unless what_follows_the_first_poll == :end_of_polling
|
|
25
|
+
poll_on_every_interval_unless_closed(
|
|
26
|
+
report_unrecoverable_error: report_unrecoverable_error, &deliver_payload_text
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
what_follows_the_first_poll
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def restart_in_a_forked_process(report_unrecoverable_error:, &deliver_payload_text)
|
|
34
|
+
@session = @session.session_of_a_forked_process
|
|
35
|
+
start_the_polling_thread_unless_closed do
|
|
36
|
+
@session.poll_at_once_and_on_every_interval(
|
|
37
|
+
report_unrecoverable_error: report_unrecoverable_error, &deliver_payload_text
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def close
|
|
43
|
+
polling_thread = @lock.synchronize do
|
|
44
|
+
@closed = true
|
|
45
|
+
@polling_thread
|
|
46
|
+
end
|
|
47
|
+
return if polling_thread.nil?
|
|
48
|
+
|
|
49
|
+
polling_thread.stop
|
|
50
|
+
polling_thread.join_within(SECONDS_CLOSE_WAITS_FOR_THE_THREAD)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def poll_on_every_interval_unless_closed(report_unrecoverable_error:, &deliver_payload_text)
|
|
56
|
+
start_the_polling_thread_unless_closed do
|
|
57
|
+
@session.poll_on_every_interval(
|
|
58
|
+
report_unrecoverable_error: report_unrecoverable_error, &deliver_payload_text
|
|
59
|
+
)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def start_the_polling_thread_unless_closed(&polling)
|
|
64
|
+
@lock.synchronize do
|
|
65
|
+
return if @closed
|
|
66
|
+
|
|
67
|
+
@polling_thread&.stop
|
|
68
|
+
@polling_thread = StoppableThread.start("configdirector-polling", &polling)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Polling
|
|
8
|
+
module RefusalDetail
|
|
9
|
+
MAXIMUM_BYTES = 1_024
|
|
10
|
+
|
|
11
|
+
private_constant :MAXIMUM_BYTES
|
|
12
|
+
|
|
13
|
+
def self.of(answer_body, server_sdk_key_to_hide:)
|
|
14
|
+
detail = written_forms_of(server_sdk_key_to_hide).reduce(answer_body) do |text, written_form|
|
|
15
|
+
text.gsub(written_form, "[the server SDK key]")
|
|
16
|
+
end.strip
|
|
17
|
+
return detail unless detail.bytesize > MAXIMUM_BYTES
|
|
18
|
+
|
|
19
|
+
"#{detail.byteslice(0, MAXIMUM_BYTES).to_s.scrub("").strip}…"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.written_forms_of(server_sdk_key)
|
|
23
|
+
key_without_the_space_around_it = server_sdk_key.strip
|
|
24
|
+
[
|
|
25
|
+
server_sdk_key,
|
|
26
|
+
key_without_the_space_around_it,
|
|
27
|
+
JSON.generate(key_without_the_space_around_it)[1...-1].to_s,
|
|
28
|
+
JSON.generate(key_without_the_space_around_it, script_safe: true)[1...-1].to_s
|
|
29
|
+
]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private_class_method :written_forms_of
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module ServerSentEvents
|
|
6
|
+
class EventStreamParser
|
|
7
|
+
MAXIMUM_LINE_BYTES = 1_048_576
|
|
8
|
+
MAXIMUM_EVENT_DATA_BYTES = 16_777_216
|
|
9
|
+
LINE_END = /[\r\n]/
|
|
10
|
+
BYTE_ORDER_MARK = "\uFEFF"
|
|
11
|
+
ASCII_DIGITS = /\A[0-9]+\z/
|
|
12
|
+
|
|
13
|
+
private_constant :MAXIMUM_LINE_BYTES, :MAXIMUM_EVENT_DATA_BYTES, :LINE_END, :BYTE_ORDER_MARK, :ASCII_DIGITS
|
|
14
|
+
|
|
15
|
+
attr_reader :last_event_id, :retry_milliseconds
|
|
16
|
+
|
|
17
|
+
def initialize(last_event_id:, &on_event)
|
|
18
|
+
@last_event_id = last_event_id
|
|
19
|
+
@on_event = on_event
|
|
20
|
+
@at_the_start_of_the_stream = true
|
|
21
|
+
@unfinished_line = String.new(encoding: Encoding::BINARY)
|
|
22
|
+
@last_chunk_ended_on_a_carriage_return = false
|
|
23
|
+
@event_type = ""
|
|
24
|
+
@event_data = String.new(encoding: Encoding::UTF_8)
|
|
25
|
+
@retry_milliseconds = nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def feed(chunk)
|
|
29
|
+
return if chunk.empty?
|
|
30
|
+
|
|
31
|
+
bytes = chunk.b
|
|
32
|
+
line_start = @last_chunk_ended_on_a_carriage_return && bytes.start_with?("\n") ? 1 : 0
|
|
33
|
+
@last_chunk_ended_on_a_carriage_return = bytes.end_with?("\r")
|
|
34
|
+
while (line_end = bytes.byteindex(LINE_END, line_start))
|
|
35
|
+
finish_line(bytes.byteslice(line_start, line_end - line_start).to_s)
|
|
36
|
+
line_start = line_end + (bytes.byteslice(line_end, 2) == "\r\n" ? 2 : 1)
|
|
37
|
+
end
|
|
38
|
+
extend_unfinished_line(bytes.byteslice(line_start..).to_s)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def extend_unfinished_line(fragment)
|
|
44
|
+
if @unfinished_line.bytesize + fragment.bytesize > MAXIMUM_LINE_BYTES
|
|
45
|
+
raise EventStreamLimitExceeded, "The SDK server sent a line longer than 1 MiB."
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
@unfinished_line << fragment
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def finish_line(fragment)
|
|
52
|
+
extend_unfinished_line(fragment)
|
|
53
|
+
line = @unfinished_line.force_encoding(Encoding::UTF_8).scrub!
|
|
54
|
+
@unfinished_line = String.new(encoding: Encoding::BINARY)
|
|
55
|
+
line = line.delete_prefix(BYTE_ORDER_MARK) if @at_the_start_of_the_stream
|
|
56
|
+
@at_the_start_of_the_stream = false
|
|
57
|
+
read_line(line)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def read_line(line)
|
|
61
|
+
return dispatch_event if line.empty?
|
|
62
|
+
|
|
63
|
+
name, _colon, value = line.partition(":")
|
|
64
|
+
read_field(name, value.delete_prefix(" "))
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def read_field(name, value)
|
|
68
|
+
case name
|
|
69
|
+
when "event" then @event_type = value
|
|
70
|
+
when "data" then append_data(value)
|
|
71
|
+
when "id" then @last_event_id = value unless value.include?("\0")
|
|
72
|
+
when "retry" then @retry_milliseconds = Integer(value, 10) if value.match?(ASCII_DIGITS)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def append_data(value)
|
|
77
|
+
if @event_data.bytesize + value.bytesize > MAXIMUM_EVENT_DATA_BYTES
|
|
78
|
+
raise EventStreamLimitExceeded, "The SDK server sent an event larger than 16 MiB."
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
@event_data << value << "\n"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def dispatch_event
|
|
85
|
+
data = @event_data
|
|
86
|
+
data.delete_suffix!("\n")
|
|
87
|
+
event = ServerSentEvent.new(type: @event_type, data: data)
|
|
88
|
+
@event_type = ""
|
|
89
|
+
@event_data = String.new(encoding: Encoding::UTF_8)
|
|
90
|
+
@on_event.call(event) unless event.data.empty?
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|