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,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class ClientConnection
|
|
7
|
+
CONNECTION_AT_A_LATER_INIT_BY_MODE = {
|
|
8
|
+
"streaming" => :left_open,
|
|
9
|
+
"polling" => :left_open,
|
|
10
|
+
"offline" => :opened_again,
|
|
11
|
+
"test" => :opened_again
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
WHAT_FOLLOWS_AN_INIT_WITHOUT_VALUES_BY_MODE = {
|
|
15
|
+
"streaming" => "The client will continue to retry since there were no fatal errors detected. " \
|
|
16
|
+
"Configs will return the in-code default value until the connection succeeds.",
|
|
17
|
+
"polling" => "Since the client was configured without streaming, configs may not update and always " \
|
|
18
|
+
"return the in-code default value.",
|
|
19
|
+
"offline" => "The offline values were not delivered, so configs return the in-code default value.",
|
|
20
|
+
"test" => "Configs return the in-code default value until values arrive."
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
private_constant :CONNECTION_AT_A_LATER_INIT_BY_MODE, :WHAT_FOLLOWS_AN_INIT_WITHOUT_VALUES_BY_MODE
|
|
24
|
+
|
|
25
|
+
def initialize(connection:, telemetry:, mode:, serving_state:, intake:, log:, lifecycle:)
|
|
26
|
+
@connection = connection
|
|
27
|
+
@telemetry = telemetry
|
|
28
|
+
@mode = mode
|
|
29
|
+
@lifecycle = lifecycle
|
|
30
|
+
@connection_opened = false
|
|
31
|
+
@serving_state = serving_state
|
|
32
|
+
@log = log
|
|
33
|
+
@report_unrecoverable_error = ->(connection_error) { intake.take_in_unrecoverable_error(connection_error) }
|
|
34
|
+
@deliver_payload = ->(payload) { intake.take_in(payload) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def open
|
|
38
|
+
@telemetry.start_reporting
|
|
39
|
+
case connection_at_this_init
|
|
40
|
+
when :left_open then :opened_before
|
|
41
|
+
when :opened_for_the_first_time, :opened_again then open_the_connection
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def close
|
|
46
|
+
LiveClientConnections::OF_THE_PROCESS.remove(self)
|
|
47
|
+
@connection.close
|
|
48
|
+
@telemetry.close
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def warn_that_no_values_arrived_within(timeout)
|
|
52
|
+
@log.warn(
|
|
53
|
+
"Timed out waiting for initialization after #{format("%g", timeout)}s. " \
|
|
54
|
+
"#{WHAT_FOLLOWS_AN_INIT_WITHOUT_VALUES_BY_MODE.fetch(@mode)}"
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def choose_the_reporter_of_what_the_parent_process_collected(forked_through)
|
|
59
|
+
@telemetry.choose_the_reporter_of_what_the_parent_process_collected(forked_through)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def restart_in_a_forked_process
|
|
63
|
+
@lifecycle.synchronize do
|
|
64
|
+
case what_follows_a_fork(connection_opened: @connection_opened)
|
|
65
|
+
when :nothing_to_restart then nil
|
|
66
|
+
when :restart_the_connection then restart_the_telemetry_and_the_connection
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
rescue StandardError => e
|
|
70
|
+
@log.error(
|
|
71
|
+
"Failed to restart the connection in forked process #{Process.pid}: #{e.class}: #{e.message.inspect}"
|
|
72
|
+
)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def connection_at_this_init
|
|
78
|
+
return :opened_for_the_first_time unless @connection_opened
|
|
79
|
+
|
|
80
|
+
CONNECTION_AT_A_LATER_INIT_BY_MODE.fetch(@mode)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def open_the_connection
|
|
84
|
+
@serving_state.expect_values_from_the_connection
|
|
85
|
+
Process.singleton_class.prepend(RestartInForkedProcesses)
|
|
86
|
+
case what_follows_a_fork(connection_opened: true)
|
|
87
|
+
when :nothing_to_restart then nil
|
|
88
|
+
when :restart_the_connection then LiveClientConnections::OF_THE_PROCESS.add(self)
|
|
89
|
+
end
|
|
90
|
+
@connection_opened = true
|
|
91
|
+
|
|
92
|
+
@connection.open(report_unrecoverable_error: @report_unrecoverable_error, &@deliver_payload)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def what_follows_a_fork(connection_opened:)
|
|
96
|
+
WhatFollowsAFork.for_a_client_with(
|
|
97
|
+
mode: @mode, connection_opened: connection_opened, closed: @serving_state.closed?,
|
|
98
|
+
connection_failed_unrecoverably: @serving_state.connection_failed_unrecoverably?
|
|
99
|
+
)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def restart_the_telemetry
|
|
103
|
+
@telemetry.restart_in_a_forked_process
|
|
104
|
+
rescue StandardError => e
|
|
105
|
+
@log.error("Failed to restart telemetry in forked process #{Process.pid}: #{e.class}: #{e.message.inspect}")
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def restart_the_telemetry_and_the_connection
|
|
109
|
+
restart_the_telemetry
|
|
110
|
+
@log.debug { "Restarting the connection in forked process #{Process.pid}" }
|
|
111
|
+
@serving_state.leave_the_delivery_of_the_first_values_to_the_parent_process
|
|
112
|
+
@connection.restart_in_a_forked_process(
|
|
113
|
+
report_unrecoverable_error: @report_unrecoverable_error, &@deliver_payload
|
|
114
|
+
)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
module ClientFactory
|
|
7
|
+
CONNECTION_CLASS_BY_MODE = {
|
|
8
|
+
"streaming" => Streaming::StreamingModeConnection,
|
|
9
|
+
"polling" => Polling::PollingModeConnection,
|
|
10
|
+
"offline" => Connection::OfflineModeConnection
|
|
11
|
+
}.freeze
|
|
12
|
+
|
|
13
|
+
TELEMETRY_CLASS_BY_MODE = {
|
|
14
|
+
"streaming" => Telemetry::ReportedTelemetry,
|
|
15
|
+
"polling" => Telemetry::ReportedTelemetry,
|
|
16
|
+
"offline" => Telemetry::DiscardedTelemetry
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
private_constant :CONNECTION_CLASS_BY_MODE, :TELEMETRY_CLASS_BY_MODE
|
|
20
|
+
|
|
21
|
+
def self.client_identified_as(
|
|
22
|
+
sdk_identity, server_sdk_key, metadata:, connection:, logger:, log_level:, telemetry:, hooks:
|
|
23
|
+
)
|
|
24
|
+
metadata, connection, telemetry, hooks = options_or_their_defaults(metadata, connection, telemetry, hooks)
|
|
25
|
+
ClientOptionsValidation.reject_unusable(
|
|
26
|
+
server_sdk_key: server_sdk_key, metadata: metadata, connection: connection, telemetry: telemetry,
|
|
27
|
+
hooks: hooks, logger: logger
|
|
28
|
+
)
|
|
29
|
+
logger ||= Logging::DefaultLogger.writing_from(log_level)
|
|
30
|
+
|
|
31
|
+
client_with_usable_options(
|
|
32
|
+
sdk_identity: sdk_identity, server_sdk_key: server_sdk_key.to_s, metadata: metadata,
|
|
33
|
+
connection_options: connection, telemetry_options: telemetry, logger: logger, hooks: hooks
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.test_client_serving(values, timeout:, logger:)
|
|
38
|
+
timeout = ConnectionOptions.new.timeout if timeout.nil?
|
|
39
|
+
ConnectionOptionsValidation.reject_a_timeout_that_is_not_positive(timeout)
|
|
40
|
+
ClientOptionsValidation.reject_an_unusable_logger(logger)
|
|
41
|
+
logger ||= Logging::DefaultLogger.writing_from(nil)
|
|
42
|
+
connection = Connection::InMemoryConnection.new(values, timeout_seconds: timeout, logger: logger)
|
|
43
|
+
telemetry = Telemetry::TestClientTelemetry.new
|
|
44
|
+
|
|
45
|
+
Testing::TestClient.new(
|
|
46
|
+
client_over_an_in_memory_connection(connection, telemetry, timeout, logger), connection,
|
|
47
|
+
telemetry.tracked_events
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.client_with_usable_options(
|
|
52
|
+
sdk_identity:, server_sdk_key:, metadata:, connection_options:, telemetry_options:, logger:, hooks:
|
|
53
|
+
)
|
|
54
|
+
warn_of_a_polling_interval_below_the_minimum(connection_options, logger)
|
|
55
|
+
connection = CONNECTION_CLASS_BY_MODE.fetch(connection_options.mode).for_a_client_with(
|
|
56
|
+
server_sdk_key: server_sdk_key, metadata: metadata, connection_options: connection_options,
|
|
57
|
+
sdk_identity: sdk_identity, logger: logger
|
|
58
|
+
)
|
|
59
|
+
telemetry = TELEMETRY_CLASS_BY_MODE.fetch(connection_options.mode).for_a_client_with(
|
|
60
|
+
server_sdk_key: server_sdk_key, metadata: metadata, connection_options: connection_options,
|
|
61
|
+
telemetry_options: telemetry_options, sdk_identity: sdk_identity, logger: logger
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
client_over(connection, telemetry, connection_options, metadata: metadata, logger: logger, hooks: hooks)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.options_or_their_defaults(metadata, connection, telemetry, hooks)
|
|
68
|
+
[
|
|
69
|
+
metadata || Metadata.new, connection || ConnectionOptions.new, telemetry || TelemetryOptions.new,
|
|
70
|
+
hooks || ClientHooks.new
|
|
71
|
+
]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.client_over(connection, telemetry, connection_options, metadata:, logger:, hooks:)
|
|
75
|
+
ConfigDirectorClient.new(
|
|
76
|
+
connection: connection, telemetry: telemetry, mode: connection_options.mode,
|
|
77
|
+
timeout: connection_options.timeout, metadata: metadata, logger: logger, hooks: hooks
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.client_over_an_in_memory_connection(connection, telemetry, timeout, logger)
|
|
82
|
+
ConfigDirectorClient.new(
|
|
83
|
+
connection: connection, telemetry: telemetry, mode: "test", timeout: timeout, metadata: Metadata.new,
|
|
84
|
+
logger: logger, hooks: ClientHooks.new
|
|
85
|
+
)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def self.warn_of_a_polling_interval_below_the_minimum(connection, logger)
|
|
89
|
+
polling_interval = Connection::PollingInterval.new(configured_seconds: connection.polling_interval)
|
|
90
|
+
return unless connection.mode == "polling" && polling_interval.raised_to_the_minimum?
|
|
91
|
+
|
|
92
|
+
Logging::ClientLog.new(logger).warn(
|
|
93
|
+
"polling_interval of #{polling_interval.configured_seconds} seconds is below the " \
|
|
94
|
+
"minimum of #{Connection::PollingInterval::MINIMUM_SECONDS} seconds. " \
|
|
95
|
+
"Using #{polling_interval.seconds} seconds."
|
|
96
|
+
)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private_class_method :client_with_usable_options, :options_or_their_defaults, :client_over,
|
|
100
|
+
:client_over_an_in_memory_connection, :warn_of_a_polling_interval_below_the_minimum
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
module ClientOptionsValidation
|
|
7
|
+
EVENT_QUEUE_LIMITS = (100..100_000)
|
|
8
|
+
LOGGER_METHODS = %i[debug info warn error].freeze
|
|
9
|
+
|
|
10
|
+
def self.reject_unusable(server_sdk_key:, metadata:, connection:, telemetry:, hooks:, logger:)
|
|
11
|
+
reject_an_option_of_another_class("metadata", metadata, Metadata)
|
|
12
|
+
reject_an_option_of_another_class("connection", connection, ConnectionOptions)
|
|
13
|
+
reject_an_option_of_another_class("telemetry", telemetry, TelemetryOptions)
|
|
14
|
+
reject_an_option_of_another_class("hooks", hooks, ClientHooks)
|
|
15
|
+
reject_an_unusable_logger(logger)
|
|
16
|
+
ConnectionOptionsValidation.reject_unusable(connection, server_sdk_key)
|
|
17
|
+
reject_unusable_telemetry(telemetry)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.reject_an_option_of_another_class(option_name, option, option_class)
|
|
21
|
+
return if option.is_a?(option_class)
|
|
22
|
+
|
|
23
|
+
raise ConfigDirectorValidationError,
|
|
24
|
+
"Invalid '#{option_name}' option of class #{option.class}. It must be a #{option_class}."
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.reject_an_unusable_logger(logger)
|
|
28
|
+
return if logger.nil? || LOGGER_METHODS.all? { |method_name| logger.respond_to?(method_name) }
|
|
29
|
+
|
|
30
|
+
raise ConfigDirectorValidationError,
|
|
31
|
+
"Invalid logger of class #{logger.class}. A logger must respond to debug, info, warn and error."
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.reject_unusable_telemetry(telemetry)
|
|
35
|
+
event_queue_limit = telemetry.event_queue_limit
|
|
36
|
+
unless event_queue_limit.is_a?(Integer) && EVENT_QUEUE_LIMITS.cover?(event_queue_limit)
|
|
37
|
+
raise ConfigDirectorValidationError,
|
|
38
|
+
"Invalid telemetry event queue limit '#{event_queue_limit}'. It must be a whole number " \
|
|
39
|
+
"between #{EVENT_QUEUE_LIMITS.min} and #{EVENT_QUEUE_LIMITS.max}."
|
|
40
|
+
end
|
|
41
|
+
return if NumberOfSeconds.finite_and_positive?(telemetry.flush_interval)
|
|
42
|
+
|
|
43
|
+
raise ConfigDirectorValidationError,
|
|
44
|
+
"Invalid telemetry flush interval '#{telemetry.flush_interval}'. " \
|
|
45
|
+
"It must be a positive number of seconds."
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private_constant :EVENT_QUEUE_LIMITS, :LOGGER_METHODS
|
|
49
|
+
private_class_method :reject_an_option_of_another_class, :reject_unusable_telemetry
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
ConfigRead = Data.define(:config_key, :in_code_default_value, :context)
|
|
7
|
+
|
|
8
|
+
class ConfigRead
|
|
9
|
+
def evaluated_to_the_in_code_default_value(reason, config_state)
|
|
10
|
+
EvaluatedRead.new(
|
|
11
|
+
config_read: self, config_state: config_state,
|
|
12
|
+
evaluation: ConfigEvaluation.new(
|
|
13
|
+
key: config_key, value: in_code_default_value.value, default_value: true, reason: reason,
|
|
14
|
+
context: context
|
|
15
|
+
)
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def evaluated_to(typed_value, config_state, reason)
|
|
20
|
+
EvaluatedRead.new(
|
|
21
|
+
config_read: self, config_state: config_state,
|
|
22
|
+
evaluation: ConfigEvaluation.new(
|
|
23
|
+
key: config_key, value: typed_value.value, default_value: false, reason: reason,
|
|
24
|
+
value_id: config_state.value_id, variation: config_state.variation, context: context
|
|
25
|
+
)
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class ConfigReader
|
|
7
|
+
def initialize(serving_state:, event_handlers:, telemetry:, log:)
|
|
8
|
+
@serving_state = serving_state
|
|
9
|
+
@event_handlers = event_handlers
|
|
10
|
+
@telemetry = telemetry
|
|
11
|
+
@log = log
|
|
12
|
+
@context_admission = ContextAdmission.new(log, telemetry)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def config_read_of(config_key, default_value, context)
|
|
16
|
+
ConfigRead.new(
|
|
17
|
+
config_key: config_key,
|
|
18
|
+
in_code_default_value: Value::InCodeDefaultValue.from(default_value),
|
|
19
|
+
context: @context_admission.admitted_context_or_none(context)
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def evaluation_of(config_key, default_value, context)
|
|
24
|
+
read(config_read_of(config_key, default_value, context))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def read(config_read)
|
|
28
|
+
evaluated_read = @serving_state.served_configs&.evaluated_read(config_read) ||
|
|
29
|
+
config_read.evaluated_to_the_in_code_default_value("client-not-ready", nil)
|
|
30
|
+
@telemetry.record_evaluation(evaluated_read)
|
|
31
|
+
evaluation = evaluated_read.evaluation
|
|
32
|
+
@log.debug { "Evaluated #{evaluation.key.inspect} to #{evaluation.value.inspect} (#{evaluation.reason})" }
|
|
33
|
+
@event_handlers.deliver_config_evaluated(evaluation)
|
|
34
|
+
evaluation
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def states(context, config_keys)
|
|
38
|
+
reject_config_keys_that_are_not_an_array(config_keys)
|
|
39
|
+
admitted_context = @context_admission.admitted_context_or_none(context)
|
|
40
|
+
|
|
41
|
+
@serving_state.served_configs&.states(admitted_context, config_keys) || {}
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def reject_config_keys_that_are_not_an_array(config_keys)
|
|
47
|
+
return if config_keys.nil? || config_keys.is_a?(Array)
|
|
48
|
+
|
|
49
|
+
raise ConfigDirectorValidationError,
|
|
50
|
+
"Invalid config keys of class #{config_keys.class}. The config keys must be an Array of Strings."
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class ConnectionIntake
|
|
7
|
+
def initialize(serving_state:, event_handlers:, watches:, config_reader:, log:)
|
|
8
|
+
@serving_state = serving_state
|
|
9
|
+
@event_handlers = event_handlers
|
|
10
|
+
@watches = watches
|
|
11
|
+
@config_reader = config_reader
|
|
12
|
+
@log = log
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def take_in(payload)
|
|
16
|
+
warn_of_the_definitions_skipped_in(payload)
|
|
17
|
+
arrival, update = @serving_state.serve(payload)
|
|
18
|
+
|
|
19
|
+
case arrival
|
|
20
|
+
when :after_close then nil
|
|
21
|
+
when :newer_values then deliver_the_update(update)
|
|
22
|
+
when :first_values then deliver_the_first_values(update)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def take_in_unrecoverable_error(connection_error)
|
|
27
|
+
@serving_state.expect_no_values_from_the_connection
|
|
28
|
+
@event_handlers.deliver_connection_error(connection_error)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def warn_of_the_definitions_skipped_in(payload)
|
|
34
|
+
payload.unreadable_configs.each do |config_key, cause|
|
|
35
|
+
@log.warn("Skipping the config '#{config_key}', its definition could not be read: #{cause}")
|
|
36
|
+
end
|
|
37
|
+
payload.unreadable_segments.each do |segment_id, cause|
|
|
38
|
+
@log.warn("Skipping the segment '#{segment_id}', its definition could not be read: #{cause}")
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def deliver_the_first_values(update)
|
|
43
|
+
deliver_the_update(update)
|
|
44
|
+
@event_handlers.deliver_client_ready
|
|
45
|
+
@log.debug { "Received initial payload from the server, client is ready" }
|
|
46
|
+
ensure
|
|
47
|
+
@serving_state.end_the_delivery_of_the_first_values
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def deliver_the_update(update)
|
|
51
|
+
@event_handlers.deliver_configs_updated(update)
|
|
52
|
+
@watches.deliver_on(update.keys + update.removed_keys) do |config_read|
|
|
53
|
+
@config_reader.read(config_read).value
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Client
|
|
8
|
+
module ConnectionOptionsValidation
|
|
9
|
+
SOURCE_OF_VALUES_BY_MODE = {
|
|
10
|
+
"streaming" => :configdirector,
|
|
11
|
+
"polling" => :configdirector,
|
|
12
|
+
"offline" => :offline_values
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
private_constant :SOURCE_OF_VALUES_BY_MODE
|
|
16
|
+
|
|
17
|
+
def self.reject_unusable(connection, server_sdk_key)
|
|
18
|
+
reject_an_unknown_mode(connection.mode)
|
|
19
|
+
case SOURCE_OF_VALUES_BY_MODE.fetch(connection.mode)
|
|
20
|
+
when :configdirector then reject_an_unusable_connection_to_configdirector(connection, server_sdk_key)
|
|
21
|
+
when :offline_values then reject_an_offline_timeout_that_is_not_a_number(connection.timeout)
|
|
22
|
+
end
|
|
23
|
+
return if NumberOfSeconds.finite?(connection.polling_interval)
|
|
24
|
+
|
|
25
|
+
raise ConfigDirectorValidationError,
|
|
26
|
+
"Invalid polling interval '#{connection.polling_interval}'. " \
|
|
27
|
+
"The polling interval must be a number of seconds."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.reject_an_unknown_mode(mode)
|
|
31
|
+
return if SOURCE_OF_VALUES_BY_MODE.key?(mode)
|
|
32
|
+
|
|
33
|
+
raise ConfigDirectorValidationError,
|
|
34
|
+
"Unknown connection mode #{mode.inspect}. " \
|
|
35
|
+
"Expected one of: #{SOURCE_OF_VALUES_BY_MODE.keys.map(&:inspect).join(", ")}."
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.reject_an_unusable_connection_to_configdirector(connection, server_sdk_key)
|
|
39
|
+
reject_an_unusable_server_sdk_key(server_sdk_key)
|
|
40
|
+
reject_offline_values_given_to(connection)
|
|
41
|
+
reject_an_unusable_url(connection.url)
|
|
42
|
+
reject_a_timeout_that_is_not_positive(connection.timeout)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.reject_a_timeout_that_is_not_positive(timeout)
|
|
46
|
+
return if NumberOfSeconds.finite_and_positive?(timeout)
|
|
47
|
+
|
|
48
|
+
raise ConfigDirectorValidationError,
|
|
49
|
+
"Invalid timeout '#{timeout}'. The timeout must be a positive number of seconds."
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.reject_an_offline_timeout_that_is_not_a_number(timeout)
|
|
53
|
+
return if NumberOfSeconds.finite?(timeout)
|
|
54
|
+
|
|
55
|
+
raise ConfigDirectorValidationError, "Invalid timeout '#{timeout}'. The timeout must be a number of seconds."
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.reject_an_unusable_server_sdk_key(server_sdk_key)
|
|
59
|
+
return if server_sdk_key.is_a?(String) && !server_sdk_key.strip.empty?
|
|
60
|
+
|
|
61
|
+
raise ConfigDirectorValidationError,
|
|
62
|
+
"No server SDK key was provided, the client cannot be instantiated without a valid server SDK key"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.reject_offline_values_given_to(connection)
|
|
66
|
+
return if connection.file.nil? && connection.values.nil?
|
|
67
|
+
|
|
68
|
+
raise ConfigDirectorValidationError,
|
|
69
|
+
"The connection options 'file' and 'values' apply only when the connection mode is 'offline', " \
|
|
70
|
+
"but the mode is '#{connection.mode}'."
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.reject_an_unusable_url(url)
|
|
74
|
+
return if http_url?(url)
|
|
75
|
+
|
|
76
|
+
raise ConfigDirectorValidationError, "Invalid base URL '#{url}'. The base URL must be an http or https URL."
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.http_url?(url)
|
|
80
|
+
return false unless url.is_a?(String)
|
|
81
|
+
|
|
82
|
+
uri = URI.parse(url)
|
|
83
|
+
uri.is_a?(URI::HTTP) && !uri.host.to_s.empty?
|
|
84
|
+
rescue URI::InvalidURIError
|
|
85
|
+
false
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private_class_method :reject_an_unknown_mode, :reject_an_unusable_connection_to_configdirector,
|
|
89
|
+
:reject_an_offline_timeout_that_is_not_a_number, :reject_an_unusable_server_sdk_key,
|
|
90
|
+
:reject_offline_values_given_to, :reject_an_unusable_url, :http_url?
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
class ContextAdmission
|
|
7
|
+
LOG_AN_INVALID_KIND = {
|
|
8
|
+
new_kind_sample: lambda do |log, kind_sample|
|
|
9
|
+
log.error(
|
|
10
|
+
"Ignored a context with the invalid kind '#{kind_sample}' and evaluated as if no context was " \
|
|
11
|
+
"supplied. A context kind has 1 to 64 characters, using only lowercase letters, digits, '-' and '_'."
|
|
12
|
+
)
|
|
13
|
+
end,
|
|
14
|
+
known_kind_sample: ->(_log, _kind_sample) {},
|
|
15
|
+
first_beyond_sample_limit: lambda do |log, _kind_sample|
|
|
16
|
+
log.error(
|
|
17
|
+
"Ignored a context with another invalid kind and evaluated as if no context was supplied. " \
|
|
18
|
+
"#{Telemetry::InvalidContextSubmissions::DISTINCT_KIND_SAMPLE_LIMIT} different invalid kinds were " \
|
|
19
|
+
"logged recently, so further invalid kinds are not logged individually."
|
|
20
|
+
)
|
|
21
|
+
end,
|
|
22
|
+
beyond_sample_limit: ->(_log, _kind_sample) {}
|
|
23
|
+
}.freeze
|
|
24
|
+
|
|
25
|
+
private_constant :LOG_AN_INVALID_KIND
|
|
26
|
+
|
|
27
|
+
def initialize(log, telemetry)
|
|
28
|
+
@log = log
|
|
29
|
+
@telemetry = telemetry
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def admitted_context_or_none(context)
|
|
33
|
+
return nil if context.nil?
|
|
34
|
+
|
|
35
|
+
unless context.is_a?(Context)
|
|
36
|
+
raise ConfigDirectorValidationError,
|
|
37
|
+
"Invalid context of class #{context.class}. A context must be a ConfigDirector::Context."
|
|
38
|
+
end
|
|
39
|
+
return context if context.kind.nil? || ContextKind.valid?(context.kind)
|
|
40
|
+
|
|
41
|
+
kind_sample = ContextKind.sample_of(context.kind)
|
|
42
|
+
LOG_AN_INVALID_KIND.fetch(@telemetry.count_invalid_context(kind_sample)).call(@log, kind_sample)
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
module ContextAttributes
|
|
7
|
+
MOST_LEVELS_OF_TRAITS = 100
|
|
8
|
+
|
|
9
|
+
private_constant :MOST_LEVELS_OF_TRAITS
|
|
10
|
+
|
|
11
|
+
def self.identifier_text(id)
|
|
12
|
+
case id
|
|
13
|
+
when nil then nil
|
|
14
|
+
when String then UnchangeableCopy.of(id)
|
|
15
|
+
when Integer then id.to_s.freeze
|
|
16
|
+
else raise ConfigDirectorValidationError, "Invalid context id of class #{id.class}. " \
|
|
17
|
+
"A context's id must be a String or an Integer."
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.name_text(name)
|
|
22
|
+
return UnchangeableCopy.of(name) if name.nil? || name.is_a?(String)
|
|
23
|
+
|
|
24
|
+
raise ConfigDirectorValidationError,
|
|
25
|
+
"Invalid context name of class #{name.class}. A context's name must be a String."
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.traits_with_symbols_as_text(traits)
|
|
29
|
+
return with_symbols_as_text(traits, 1) if traits.nil? || traits.is_a?(Hash)
|
|
30
|
+
|
|
31
|
+
raise ConfigDirectorValidationError,
|
|
32
|
+
"Invalid context traits of class #{traits.class}. A context's traits must be a Hash."
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.anonymous_flag(anonymous)
|
|
36
|
+
return anonymous if [true, false].include?(anonymous)
|
|
37
|
+
|
|
38
|
+
raise ConfigDirectorValidationError,
|
|
39
|
+
"Invalid anonymous flag of class #{anonymous.class}. A context's anonymous flag must be true or false."
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.with_symbols_as_text(trait, level)
|
|
43
|
+
case trait
|
|
44
|
+
when Hash, Array then container_with_symbols_as_text(trait, level)
|
|
45
|
+
else UnchangeableCopy.of(text_of_a_symbol(trait))
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.container_with_symbols_as_text(container, level)
|
|
50
|
+
raise traits_nested_too_deep if level > MOST_LEVELS_OF_TRAITS
|
|
51
|
+
|
|
52
|
+
level_of_its_contents = level + 1
|
|
53
|
+
case container
|
|
54
|
+
when Hash
|
|
55
|
+
container.to_h do |key, value|
|
|
56
|
+
[text_of_a_symbol(key), with_symbols_as_text(value, level_of_its_contents)]
|
|
57
|
+
end.freeze
|
|
58
|
+
else container.map { |element| with_symbols_as_text(element, level_of_its_contents) }.freeze
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.traits_nested_too_deep
|
|
63
|
+
ConfigDirectorValidationError.new(
|
|
64
|
+
"Invalid context traits nested deeper than #{MOST_LEVELS_OF_TRAITS} levels. A context's traits must be " \
|
|
65
|
+
"nested #{MOST_LEVELS_OF_TRAITS} levels deep or less, so a Hash or an Array among them cannot hold itself."
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def self.text_of_a_symbol(key_or_value)
|
|
70
|
+
key_or_value.is_a?(Symbol) ? key_or_value.name : key_or_value
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private_class_method :with_symbols_as_text, :container_with_symbols_as_text, :traits_nested_too_deep,
|
|
74
|
+
:text_of_a_symbol
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Client
|
|
6
|
+
module ContextKind
|
|
7
|
+
VALID_KIND = /\A[a-z0-9_-]{1,64}\z/
|
|
8
|
+
SAMPLE_MAXIMUM_CHARACTERS = 128
|
|
9
|
+
|
|
10
|
+
def self.valid?(kind)
|
|
11
|
+
kind.is_a?(String) && kind.b.match?(VALID_KIND)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.sample_of(kind)
|
|
15
|
+
kind.to_s[0, SAMPLE_MAXIMUM_CHARACTERS].to_s.encode(Encoding::UTF_8, invalid: :replace, undef: :replace).scrub
|
|
16
|
+
rescue StandardError
|
|
17
|
+
kind.class.to_s
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private_constant :VALID_KIND, :SAMPLE_MAXIMUM_CHARACTERS
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|