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,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
module Rapidhash
|
|
7
|
+
LOW_64_BITS = 0xFFFF_FFFF_FFFF_FFFF
|
|
8
|
+
|
|
9
|
+
SECRET = [
|
|
10
|
+
0x2D35_8DCC_AA6C_78A5,
|
|
11
|
+
0x8BB8_4B93_962E_ACC9,
|
|
12
|
+
0x4B33_A62E_D433_D4A3,
|
|
13
|
+
0x4D5A_2DA5_1DE1_AA47,
|
|
14
|
+
0xA076_1D64_78BD_642F,
|
|
15
|
+
0xE703_7ED1_A0B4_28DB,
|
|
16
|
+
0x90ED_1765_281C_388C,
|
|
17
|
+
0xAAAA_AAAA_AAAA_AAAA
|
|
18
|
+
].freeze
|
|
19
|
+
|
|
20
|
+
SHORT_MESSAGE_LENGTH = 16
|
|
21
|
+
BLOCK_LENGTH = 112
|
|
22
|
+
LANE_COUNT = 7
|
|
23
|
+
REMAINDER_PAIR_SECRETS = [2, 2, 1, 1, 2, 1].freeze
|
|
24
|
+
|
|
25
|
+
def self.of(message, seed)
|
|
26
|
+
length = message.bytesize
|
|
27
|
+
seed ^= mix(seed ^ SECRET[2], SECRET[1])
|
|
28
|
+
first_word, second_word, seed, last_block_length =
|
|
29
|
+
if length <= SHORT_MESSAGE_LENGTH
|
|
30
|
+
short_message_words(message, length, seed)
|
|
31
|
+
else
|
|
32
|
+
long_message_words(message, length, seed)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
finish(first_word ^ SECRET[1], second_word ^ seed, last_block_length)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.short_message_words(message, length, seed)
|
|
39
|
+
first_word, second_word =
|
|
40
|
+
if length >= 8 then [read64(message, 0), read64(message, length - 8)]
|
|
41
|
+
elsif length >= 4 then [read32(message, 0), read32(message, length - 4)]
|
|
42
|
+
elsif length.positive? then tiny_message_words(message, length)
|
|
43
|
+
else [0, 0]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
[first_word, second_word, length >= 4 ? seed ^ length : seed, length]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.tiny_message_words(message, length)
|
|
50
|
+
first_byte = read8(message, 0)
|
|
51
|
+
first_word = read8(message, length - 1) | (((first_byte << 5) & 0xFF) << 40) | ((first_byte >> 3) << 48)
|
|
52
|
+
|
|
53
|
+
[first_word, read8(message, length >> 1)]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.long_message_words(message, length, seed)
|
|
57
|
+
remainder_offset, seed = blocks(message, length, seed)
|
|
58
|
+
seed = remainder_pairs(message, remainder_offset, length, seed)
|
|
59
|
+
last_block_length = length - remainder_offset
|
|
60
|
+
|
|
61
|
+
[read64(message, length - 16) ^ last_block_length, read64(message, length - 8), seed, last_block_length]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.blocks(message, length, seed)
|
|
65
|
+
return [0, seed] if length <= BLOCK_LENGTH
|
|
66
|
+
|
|
67
|
+
lanes = Array.new(LANE_COUNT, seed)
|
|
68
|
+
offset = 0
|
|
69
|
+
loop do
|
|
70
|
+
lanes = mix_block(message, offset, lanes)
|
|
71
|
+
offset += BLOCK_LENGTH
|
|
72
|
+
break if length - offset <= BLOCK_LENGTH
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
[offset, lanes.reduce(0) { |folded, lane| folded ^ lane }]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def self.mix_block(message, offset, lanes)
|
|
79
|
+
lanes.each_with_index.map do |lane_seed, lane|
|
|
80
|
+
lane_offset = offset + (lane * 16)
|
|
81
|
+
mix(read64(message, lane_offset) ^ SECRET[lane], read64(message, lane_offset + 8) ^ lane_seed)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.remainder_pairs(message, offset, length, seed)
|
|
86
|
+
REMAINDER_PAIR_SECRETS.each_with_index do |secret, pair|
|
|
87
|
+
pair_offset = offset + (pair * 16)
|
|
88
|
+
break if length - pair_offset <= 16
|
|
89
|
+
|
|
90
|
+
seed = mix(read64(message, pair_offset) ^ SECRET[secret], read64(message, pair_offset + 8) ^ seed)
|
|
91
|
+
end
|
|
92
|
+
seed
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def self.mix(multiplicand, multiplier)
|
|
96
|
+
product = multiplicand * multiplier
|
|
97
|
+
(product & LOW_64_BITS) ^ (product >> 64)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def self.finish(first_word, second_word, last_block_length)
|
|
101
|
+
product = first_word * second_word
|
|
102
|
+
mix((product & LOW_64_BITS) ^ SECRET[7], (product >> 64) ^ SECRET[1] ^ last_block_length)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.read64(message, offset)
|
|
106
|
+
message.unpack1("Q<", offset: offset).to_i
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def self.read32(message, offset)
|
|
110
|
+
message.unpack1("L<", offset: offset).to_i
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def self.read8(message, offset)
|
|
114
|
+
message.unpack1("C", offset: offset).to_i
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
private_constant :LOW_64_BITS, :SECRET, :SHORT_MESSAGE_LENGTH, :BLOCK_LENGTH, :LANE_COUNT,
|
|
118
|
+
:REMAINDER_PAIR_SECRETS
|
|
119
|
+
private_class_method :short_message_words, :tiny_message_words, :long_message_words, :blocks, :mix_block,
|
|
120
|
+
:remainder_pairs, :mix, :finish, :read64, :read32, :read8
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
Rule = Data.define(:id, :order, :type, :target, :value, :value_id, :variation_id, :conditions, :shares)
|
|
7
|
+
|
|
8
|
+
class Rule
|
|
9
|
+
def serve(config_id, context, segments)
|
|
10
|
+
case type
|
|
11
|
+
when "conditional" then serve_when_every_condition_holds(config_id, context, segments)
|
|
12
|
+
when "percentage" then serve_from_rollout(config_id, context)
|
|
13
|
+
else :not_matched
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def uses_any_segment_of?(segment_ids)
|
|
18
|
+
type == "conditional" && conditions.any? { |condition| condition.uses_any_segment_of?(segment_ids) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def serve_when_every_condition_holds(config_id, context, segments)
|
|
24
|
+
return :not_matched unless conditions.all? { |condition| condition.holds_for?(context, segments) }
|
|
25
|
+
|
|
26
|
+
case target
|
|
27
|
+
when "value" then serve_value
|
|
28
|
+
when "percentage" then serve_from_rollout(config_id, context)
|
|
29
|
+
else :not_matched
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def serve_value
|
|
34
|
+
return :not_matched if value.nil?
|
|
35
|
+
|
|
36
|
+
ServedValue.new(
|
|
37
|
+
value: value, value_id: value_id, variation_id: variation_id, rollout_share: nil, served_by: :matching_rule
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def serve_from_rollout(config_id, context)
|
|
42
|
+
rollout_shares = shares
|
|
43
|
+
return :not_matched if rollout_shares.nil?
|
|
44
|
+
|
|
45
|
+
identifier = context.identifier
|
|
46
|
+
return :rollout_skipped_no_identifier if identifier.nil?
|
|
47
|
+
|
|
48
|
+
share = share_covering(rollout_shares, AssignedPercentage.of(config_id, identifier, context.effective_kind))
|
|
49
|
+
share_value = share&.value
|
|
50
|
+
share.nil? || share_value.nil? ? :not_matched : serve_share(share, share_value)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def share_covering(rollout_shares, assigned_percentage)
|
|
54
|
+
share_end = 0.0
|
|
55
|
+
rollout_shares.find do |share|
|
|
56
|
+
share_end += share.percentage
|
|
57
|
+
assigned_percentage < share_end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def serve_share(share, share_value)
|
|
62
|
+
ServedValue.new(
|
|
63
|
+
value: share_value, value_id: share.value_id, variation_id: share.variation_id,
|
|
64
|
+
rollout_share: RolloutShare.new(rule_id: id, share_id: share.id), served_by: :matching_rule
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
module ScalarText
|
|
7
|
+
LAST_POINT_POSITION_SPELLED_OUT = 21
|
|
8
|
+
FIRST_POINT_POSITION_SPELLED_OUT = -5
|
|
9
|
+
|
|
10
|
+
def self.of(value)
|
|
11
|
+
case value
|
|
12
|
+
when String then value
|
|
13
|
+
when true then "true"
|
|
14
|
+
when false then "false"
|
|
15
|
+
when Integer then value.to_s
|
|
16
|
+
when Float then of_float(value)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.of_float(number)
|
|
21
|
+
if number.nan? then "NaN"
|
|
22
|
+
elsif number.infinite? then number.positive? ? "Infinity" : "-Infinity"
|
|
23
|
+
elsif number.zero? then "0"
|
|
24
|
+
elsif number.negative? then "-#{of_positive_float(-number)}"
|
|
25
|
+
else of_positive_float(number)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.of_positive_float(number)
|
|
30
|
+
digits, point_position = shortest_digits_and_point_position(number)
|
|
31
|
+
|
|
32
|
+
if point_position.between?(digits.length, LAST_POINT_POSITION_SPELLED_OUT)
|
|
33
|
+
digits + ("0" * (point_position - digits.length))
|
|
34
|
+
elsif point_position.between?(1, LAST_POINT_POSITION_SPELLED_OUT)
|
|
35
|
+
"#{digits[0, point_position]}.#{digits[point_position..]}"
|
|
36
|
+
elsif point_position.between?(FIRST_POINT_POSITION_SPELLED_OUT, 0)
|
|
37
|
+
"0.#{"0" * -point_position}#{digits}"
|
|
38
|
+
else
|
|
39
|
+
exponent_form(digits, point_position - 1)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.shortest_digits_and_point_position(number)
|
|
44
|
+
mantissa, exponent = number.to_s.split("e")
|
|
45
|
+
whole, fraction = mantissa.to_s.split(".")
|
|
46
|
+
digits = "#{whole}#{fraction}"
|
|
47
|
+
leading_zeros = leading_zeros_of(digits)
|
|
48
|
+
trailing_zeros = leading_zeros_of(digits.reverse)
|
|
49
|
+
significant_digits = digits.delete_prefix(leading_zeros).delete_suffix(trailing_zeros)
|
|
50
|
+
|
|
51
|
+
[significant_digits, whole.to_s.length + exponent.to_i - leading_zeros.length]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.leading_zeros_of(text)
|
|
55
|
+
text.each_char.take_while { |character| character == "0" }.join
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.exponent_form(digits, exponent)
|
|
59
|
+
mantissa = digits.length == 1 ? digits : "#{digits[0]}.#{digits[1..]}"
|
|
60
|
+
|
|
61
|
+
"#{mantissa}e#{exponent.negative? ? "-" : "+"}#{exponent.abs}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private_constant :LAST_POINT_POSITION_SPELLED_OUT, :FIRST_POINT_POSITION_SPELLED_OUT
|
|
65
|
+
private_class_method :of_float, :of_positive_float, :shortest_digits_and_point_position, :leading_zeros_of,
|
|
66
|
+
:exponent_form
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
Segment = Data.define(:condition_groups)
|
|
7
|
+
|
|
8
|
+
class Segment
|
|
9
|
+
def contains?(context)
|
|
10
|
+
condition_groups.any? do |condition_group|
|
|
11
|
+
condition_group.all? { |condition| condition.holds_for?(context, {}) }
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
SegmentCondition = Data.define(:operator, :segment_id)
|
|
7
|
+
|
|
8
|
+
class SegmentCondition
|
|
9
|
+
def holds_for?(context, segments)
|
|
10
|
+
segment = segments[segment_id]
|
|
11
|
+
return false if segment.nil?
|
|
12
|
+
|
|
13
|
+
case operator.downcase
|
|
14
|
+
when "in" then segment.contains?(context)
|
|
15
|
+
when "not in" then !segment.contains?(context)
|
|
16
|
+
else false
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def uses_any_segment_of?(segment_ids)
|
|
21
|
+
segment_ids.include?(segment_id)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
module SemverComparison
|
|
7
|
+
VERSION_IN_TEXT = /(?:\A|[^0-9])([0-9]{1,16})(?:\.([0-9]{1,16}))?(?:\.([0-9]{1,16}))?(?:\z|[^0-9])/
|
|
8
|
+
|
|
9
|
+
NEVER_MATCHES = ->(_version, _targets) { false }.freeze
|
|
10
|
+
|
|
11
|
+
OPERATORS = {
|
|
12
|
+
"=" => ->(version, targets) { [0].include?(ordering(version, targets.first)) },
|
|
13
|
+
"<" => ->(version, targets) { [-1].include?(ordering(version, targets.first)) },
|
|
14
|
+
"<=" => ->(version, targets) { [-1, 0].include?(ordering(version, targets.first)) },
|
|
15
|
+
">" => ->(version, targets) { [1].include?(ordering(version, targets.first)) },
|
|
16
|
+
">=" => ->(version, targets) { [0, 1].include?(ordering(version, targets.first)) },
|
|
17
|
+
"is one of" => ->(version, targets) { equals_any?(version, targets) },
|
|
18
|
+
"is not one of" => ->(version, targets) { !equals_any?(version, targets) }
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
def self.matches?(text, operator, targets)
|
|
22
|
+
OPERATORS.fetch(operator, NEVER_MATCHES).call(version_in(text), targets)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.version_in(text)
|
|
26
|
+
match = VERSION_IN_TEXT.match(text)
|
|
27
|
+
return nil if match.nil?
|
|
28
|
+
|
|
29
|
+
major, minor, patch = match.captures
|
|
30
|
+
[major.to_i, minor.to_i, patch.to_i]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.ordering(version, target_text)
|
|
34
|
+
target = target_text.nil? ? nil : version_in(target_text)
|
|
35
|
+
return nil if version.nil? || target.nil?
|
|
36
|
+
|
|
37
|
+
version <=> target
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.equals_any?(version, targets)
|
|
41
|
+
targets.any? { |target| ordering(version, target)&.zero? }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private_constant :VERSION_IN_TEXT, :NEVER_MATCHES, :OPERATORS
|
|
45
|
+
private_class_method :version_in, :ordering, :equals_any?
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
TargetingRules = Data.define(:default_value, :default_value_id, :default_variation_id, :rules)
|
|
7
|
+
|
|
8
|
+
class TargetingRules
|
|
9
|
+
def default_targeting_rule_value(a_rollout_was_skipped:)
|
|
10
|
+
ServedValue.new(
|
|
11
|
+
value: default_value, value_id: default_value_id, variation_id: default_variation_id, rollout_share: nil,
|
|
12
|
+
served_by: a_rollout_was_skipped ? :default_targeting_rule_after_skipped_rollout : :default_targeting_rule
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
module TextComparison
|
|
7
|
+
EQUALS_THE_FIRST_TARGET = ->(text, targets) { !targets.empty? && text == targets.first }.freeze
|
|
8
|
+
DIFFERS_FROM_THE_FIRST_TARGET = ->(text, targets) { !targets.empty? && text != targets.first }.freeze
|
|
9
|
+
NEVER_MATCHES = ->(_text, _targets) { false }.freeze
|
|
10
|
+
|
|
11
|
+
OPERATORS = {
|
|
12
|
+
"=" => EQUALS_THE_FIRST_TARGET,
|
|
13
|
+
"equals" => EQUALS_THE_FIRST_TARGET,
|
|
14
|
+
"!=" => DIFFERS_FROM_THE_FIRST_TARGET,
|
|
15
|
+
"does not equal" => DIFFERS_FROM_THE_FIRST_TARGET,
|
|
16
|
+
"is one of" => ->(text, targets) { targets.include?(text) },
|
|
17
|
+
"is not one of" => ->(text, targets) { !targets.include?(text) },
|
|
18
|
+
"starts with any of" => ->(text, targets) { text.start_with?(*targets) },
|
|
19
|
+
"does not start with any of" => ->(text, targets) { !text.start_with?(*targets) },
|
|
20
|
+
"ends with any of" => ->(text, targets) { text.end_with?(*targets) },
|
|
21
|
+
"does not end with any of" => ->(text, targets) { !text.end_with?(*targets) }
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
private_constant :EQUALS_THE_FIRST_TARGET, :DIFFERS_FROM_THE_FIRST_TARGET, :NEVER_MATCHES, :OPERATORS
|
|
25
|
+
|
|
26
|
+
def self.matches?(text, operator, targets)
|
|
27
|
+
OPERATORS.fetch(operator, NEVER_MATCHES).call(text, targets)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Evaluation
|
|
6
|
+
module TraitPointer
|
|
7
|
+
INVALID_ESCAPE = /~(?:[^01]|\z)/
|
|
8
|
+
ESCAPE = /~[01]/
|
|
9
|
+
DECODED_ESCAPES = { "~1" => "/", "~0" => "~" }.freeze
|
|
10
|
+
ARRAY_INDEX = /\A(?:0|[1-9][0-9]*)\z/
|
|
11
|
+
|
|
12
|
+
def self.value_at(pointer, traits)
|
|
13
|
+
return nil unless pointer.start_with?("/")
|
|
14
|
+
|
|
15
|
+
encoded_tokens = pointer.split("/", -1).drop(1)
|
|
16
|
+
return nil if encoded_tokens.any? { |encoded_token| encoded_token.match?(INVALID_ESCAPE) }
|
|
17
|
+
|
|
18
|
+
encoded_tokens.reduce(traits) do |container, encoded_token|
|
|
19
|
+
member_of(container, encoded_token.gsub(ESCAPE, DECODED_ESCAPES))
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.member_of(container, token)
|
|
24
|
+
case container
|
|
25
|
+
when Hash then container.fetch(token, nil)
|
|
26
|
+
when Array then element_of(container, token)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.element_of(elements, token)
|
|
31
|
+
return nil unless token.match?(ARRAY_INDEX)
|
|
32
|
+
|
|
33
|
+
index = Integer(token, 10)
|
|
34
|
+
index < elements.length ? elements[index] : nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private_constant :INVALID_ESCAPE, :ESCAPE, :DECODED_ESCAPES, :ARRAY_INDEX
|
|
38
|
+
private_class_method :member_of, :element_of
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "evaluation/scalar_text"
|
|
4
|
+
require_relative "evaluation/text_comparison"
|
|
5
|
+
require_relative "evaluation/number_comparison"
|
|
6
|
+
require_relative "evaluation/semver_comparison"
|
|
7
|
+
require_relative "evaluation/datetime_comparison"
|
|
8
|
+
require_relative "evaluation/array_comparison"
|
|
9
|
+
require_relative "evaluation/trait_pointer"
|
|
10
|
+
require_relative "evaluation/rapidhash"
|
|
11
|
+
require_relative "evaluation/evaluation_context"
|
|
12
|
+
require_relative "evaluation/assigned_percentage"
|
|
13
|
+
require_relative "evaluation/attribute_condition"
|
|
14
|
+
require_relative "evaluation/segment"
|
|
15
|
+
require_relative "evaluation/segment_condition"
|
|
16
|
+
require_relative "evaluation/share"
|
|
17
|
+
require_relative "evaluation/rollout_share"
|
|
18
|
+
require_relative "evaluation/served_value"
|
|
19
|
+
require_relative "evaluation/targeting_rules"
|
|
20
|
+
require_relative "evaluation/rule"
|
|
21
|
+
require_relative "evaluation/config_evaluator"
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Identity
|
|
6
|
+
class SdkIdentity
|
|
7
|
+
def self.of_the_server_sdk
|
|
8
|
+
new("ruby-server-sdk", VERSION)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.of_the_wrapper(wrapper)
|
|
12
|
+
unless wrapper.is_a?(Wrapper)
|
|
13
|
+
raise ArgumentError, "An SDK identity is made for the SDK itself or for a wrapper the SDK lists."
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
new(wrapper.sdk_name, wrapper.version_of_its_gem)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private_class_method :new
|
|
20
|
+
|
|
21
|
+
def initialize(sdk_name, sdk_version)
|
|
22
|
+
@sdk_name = sdk_name
|
|
23
|
+
@sdk_version = sdk_version
|
|
24
|
+
freeze
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def user_agent
|
|
28
|
+
"#{@sdk_name}/#{@sdk_version}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def meta_context_of_an_application_with(metadata)
|
|
32
|
+
{
|
|
33
|
+
sdkName: @sdk_name, sdkVersion: @sdk_version, appName: metadata.app_name, appVersion: metadata.app_version
|
|
34
|
+
}.compact
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Identity
|
|
6
|
+
class Wrapper
|
|
7
|
+
VERSION_WITHOUT_THE_GEM_OF_THE_WRAPPER = "0.0.0-dev"
|
|
8
|
+
|
|
9
|
+
private_constant :VERSION_WITHOUT_THE_GEM_OF_THE_WRAPPER
|
|
10
|
+
|
|
11
|
+
attr_reader :sdk_name
|
|
12
|
+
|
|
13
|
+
def initialize(sdk_name, name_of_the_version_constant_of_its_gem)
|
|
14
|
+
@sdk_name = sdk_name
|
|
15
|
+
@name_of_the_version_constant_of_its_gem = name_of_the_version_constant_of_its_gem
|
|
16
|
+
freeze
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
OPENFEATURE_SERVER_PROVIDER = new("ruby-openfeature-server-provider", "ConfigDirector::OpenFeature::VERSION")
|
|
20
|
+
|
|
21
|
+
private_class_method :new
|
|
22
|
+
|
|
23
|
+
def create_client(
|
|
24
|
+
server_sdk_key = nil, metadata: nil, connection: nil, logger: nil, log_level: nil, telemetry: nil, hooks: nil
|
|
25
|
+
)
|
|
26
|
+
Client::ClientFactory.client_identified_as(
|
|
27
|
+
SdkIdentity.of_the_wrapper(self), server_sdk_key,
|
|
28
|
+
metadata: metadata, connection: connection, logger: logger, log_level: log_level, telemetry: telemetry,
|
|
29
|
+
hooks: hooks
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def version_of_its_gem
|
|
34
|
+
return VERSION_WITHOUT_THE_GEM_OF_THE_WRAPPER unless gem_loaded?
|
|
35
|
+
|
|
36
|
+
version = Object.const_get(@name_of_the_version_constant_of_its_gem)
|
|
37
|
+
version.is_a?(String) ? version : VERSION_WITHOUT_THE_GEM_OF_THE_WRAPPER
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def gem_loaded?
|
|
43
|
+
Object.const_defined?(@name_of_the_version_constant_of_its_gem)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Logging
|
|
6
|
+
class ClientLog
|
|
7
|
+
def initialize(logger, component: "ConfigDirectorClient")
|
|
8
|
+
@logger = logger
|
|
9
|
+
@line_prefix = "[#{component}]"
|
|
10
|
+
@writes_debug_lines = logger.respond_to?(:debug?) ? logger.method(:debug?) : -> { true }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def debug
|
|
14
|
+
@logger.debug("#{@line_prefix} #{yield}") if @writes_debug_lines.call
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def info(line)
|
|
18
|
+
@logger.info("#{@line_prefix} #{line}")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def warn(line)
|
|
22
|
+
@logger.warn("#{@line_prefix} #{line}")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def error(line)
|
|
26
|
+
@logger.error("#{@line_prefix} #{line}")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def ignoring_errors_from(source)
|
|
30
|
+
yield
|
|
31
|
+
rescue StandardError => e
|
|
32
|
+
error("#{source} raised an error, which was ignored: #{e.class}: #{e.message.inspect}")
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "logger"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Logging
|
|
8
|
+
module DefaultLogger
|
|
9
|
+
LINE_PREFIX = "[ConfigDirector:ruby-server-sdk]"
|
|
10
|
+
LINE_FORMAT = ->(severity, _time, _program_name, message) { "#{LINE_PREFIX} #{severity}: #{message}\n" }.freeze
|
|
11
|
+
LOGGER_LEVELS = (Logger::DEBUG..Logger::UNKNOWN)
|
|
12
|
+
|
|
13
|
+
def self.writing_from(log_level)
|
|
14
|
+
lowest_level_written = log_level.nil? ? Logger::WARN : log_level
|
|
15
|
+
raise invalid(log_level) if lowest_level_written.is_a?(Integer) && !LOGGER_LEVELS.cover?(lowest_level_written)
|
|
16
|
+
|
|
17
|
+
Logger.new($stderr, level: lowest_level_written, formatter: LINE_FORMAT)
|
|
18
|
+
rescue ArgumentError
|
|
19
|
+
raise invalid(log_level)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.invalid(log_level)
|
|
23
|
+
ConfigDirectorValidationError.new(
|
|
24
|
+
"Invalid log level #{log_level.inspect}. The log level must be one that Ruby's Logger takes, " \
|
|
25
|
+
"such as :debug, :info, :warn or :error."
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private_constant :LINE_PREFIX, :LINE_FORMAT, :LOGGER_LEVELS
|
|
30
|
+
private_class_method :invalid
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|