From e1da666f0d94652f980dc3a3e7b670bf47033c4c Mon Sep 17 00:00:00 2001 From: Mauro Sanz Date: Fri, 16 Jul 2021 17:11:10 -0300 Subject: [PATCH] add configs and update api clients --- .../cache/fetchers/segment_fetcher.rb | 11 +++++--- .../cache/fetchers/split_fetcher.rb | 8 +++--- lib/splitclient-rb/engine/api/segments.rb | 11 +++++--- lib/splitclient-rb/engine/api/splits.rb | 8 +++--- lib/splitclient-rb/engine/synchronizer.rb | 10 ++++---- lib/splitclient-rb/split_config.rb | 14 +++++++++++ spec/engine/api/segments_spec.rb | 25 ++++++++++++++++++- spec/engine/api/splits_spec.rb | 25 +++++++++++++++++-- spec/splitclient/split_config_spec.rb | 2 ++ 9 files changed, 91 insertions(+), 23 deletions(-) diff --git a/lib/splitclient-rb/cache/fetchers/segment_fetcher.rb b/lib/splitclient-rb/cache/fetchers/segment_fetcher.rb index 85ed13ce..efd24bd2 100644 --- a/lib/splitclient-rb/cache/fetchers/segment_fetcher.rb +++ b/lib/splitclient-rb/cache/fetchers/segment_fetcher.rb @@ -30,16 +30,19 @@ def call def fetch_segments_if_not_exists(names, cache_control_headers = false) names.each do |name| change_number = @segments_repository.get_change_number(name) - - fetch_segment(name, cache_control_headers) if change_number == -1 + + if change_number == -1 + fetch_options = { cache_control_headers: cache_control_headers, till: nil } + fetch_segment(name, fetch_options) if change_number == -1 + end end rescue StandardError => error @config.log_found_exception(__method__.to_s, error) end - def fetch_segment(name, cache_control_headers = false) + def fetch_segment(name, fetch_options = { cache_control_headers: false, till: nil }) @semaphore.synchronize do - segments_api.fetch_segments_by_names([name], cache_control_headers) + segments_api.fetch_segments_by_names([name], fetch_options) end rescue StandardError => error @config.log_found_exception(__method__.to_s, error) diff --git a/lib/splitclient-rb/cache/fetchers/split_fetcher.rb b/lib/splitclient-rb/cache/fetchers/split_fetcher.rb index 91cb311b..51534bab 100644 --- a/lib/splitclient-rb/cache/fetchers/split_fetcher.rb +++ b/lib/splitclient-rb/cache/fetchers/split_fetcher.rb @@ -27,9 +27,9 @@ def call end end - def fetch_splits(cache_control_headers = false) + def fetch_splits(fetch_options = { cache_control_headers: false, till: nil }) @semaphore.synchronize do - data = splits_since(@splits_repository.get_change_number, cache_control_headers) + data = splits_since(@splits_repository.get_change_number, fetch_options) data[:splits] && data[:splits].each do |split| add_split_unless_archived(split) @@ -68,8 +68,8 @@ def splits_thread end end - def splits_since(since, cache_control_headers = false) - splits_api.since(since, cache_control_headers) + def splits_since(since, fetch_options = { cache_control_headers: false, till: nil }) + splits_api.since(since, fetch_options) end def add_split_unless_archived(split) diff --git a/lib/splitclient-rb/engine/api/segments.rb b/lib/splitclient-rb/engine/api/segments.rb index ae27453d..0c16adc7 100644 --- a/lib/splitclient-rb/engine/api/segments.rb +++ b/lib/splitclient-rb/engine/api/segments.rb @@ -11,13 +11,13 @@ def initialize(api_key, segments_repository, config, telemetry_runtime_producer) @telemetry_runtime_producer = telemetry_runtime_producer end - def fetch_segments_by_names(names, cache_control_headers = false) + def fetch_segments_by_names(names, fetch_options = { cache_control_headers: false, till: nil }) return if names.nil? || names.empty? names.each do |name| since = @segments_repository.get_change_number(name) loop do - segment = fetch_segment_changes(name, since, cache_control_headers) + segment = fetch_segment_changes(name, since, fetch_options) @segments_repository.add_to_segment(segment) @config.split_logger.log_if_debug("Segment #{name} fetched before: #{since}, \ @@ -32,9 +32,12 @@ def fetch_segments_by_names(names, cache_control_headers = false) private - def fetch_segment_changes(name, since, cache_control_headers = false) + def fetch_segment_changes(name, since, fetch_options = { cache_control_headers: false, till: nil }) start = Time.now - response = get_api("#{@config.base_uri}/segmentChanges/#{name}", @api_key, { since: since }, cache_control_headers) + + params = { since: since } + params[:till] = fetch_options[:till] unless fetch_options[:till].nil? + response = get_api("#{@config.base_uri}/segmentChanges/#{name}", @api_key, params, fetch_options[:cache_control_headers]) if response.success? segment = JSON.parse(response.body, symbolize_names: true) diff --git a/lib/splitclient-rb/engine/api/splits.rb b/lib/splitclient-rb/engine/api/splits.rb index 3d0c1197..99bbe5ce 100644 --- a/lib/splitclient-rb/engine/api/splits.rb +++ b/lib/splitclient-rb/engine/api/splits.rb @@ -10,10 +10,12 @@ def initialize(api_key, config, telemetry_runtime_producer) @telemetry_runtime_producer = telemetry_runtime_producer end - def since(since, cache_control_headers = false) + def since(since, fetch_options = { cache_control_headers: false, till: nil }) start = Time.now - - response = get_api("#{@config.base_uri}/splitChanges", @api_key, { since: since }, cache_control_headers) + + params = { since: since } + params[:till] = fetch_options[:till] unless fetch_options[:till].nil? + response = get_api("#{@config.base_uri}/splitChanges", @api_key, params, fetch_options[:cache_control_headers]) if response.success? result = splits_with_segment_names(response.body) diff --git a/lib/splitclient-rb/engine/synchronizer.rb b/lib/splitclient-rb/engine/synchronizer.rb index 069521d7..5f23b4cb 100644 --- a/lib/splitclient-rb/engine/synchronizer.rb +++ b/lib/splitclient-rb/engine/synchronizer.rb @@ -6,8 +6,6 @@ class Synchronizer include SplitIoClient::Cache::Fetchers include SplitIoClient::Cache::Senders - FORCE_CACHE_CONTROL_HEADERS = true - def initialize( repositories, api_key, @@ -55,12 +53,14 @@ def stop_periodic_fetch end def fetch_splits - segment_names = @split_fetcher.fetch_splits(FORCE_CACHE_CONTROL_HEADERS) - @segment_fetcher.fetch_segments_if_not_exists(segment_names, FORCE_CACHE_CONTROL_HEADERS) unless segment_names.empty? + fetch_options = { cache_control_headers: true, till: nil } + segment_names = @split_fetcher.fetch_splits(fetch_options) + @segment_fetcher.fetch_segments_if_not_exists(segment_names, true) unless segment_names.empty? end def fetch_segment(name) - @segment_fetcher.fetch_segment(name, FORCE_CACHE_CONTROL_HEADERS) + fetch_options = { cache_control_headers: true, till: nil } + @segment_fetcher.fetch_segment(name, fetch_options) end private diff --git a/lib/splitclient-rb/split_config.rb b/lib/splitclient-rb/split_config.rb index 468c3b0a..7886e99d 100644 --- a/lib/splitclient-rb/split_config.rb +++ b/lib/splitclient-rb/split_config.rb @@ -113,6 +113,9 @@ def initialize(opts = {}) @sdk_start_time = Time.now + @on_demand_fetch_retry_delay_ms = SplitConfig.default_on_demand_fetch_retry_delay_ms + @on_demand_fetch_max_retries = SplitConfig.default_on_demand_fetch_max_retries + startup_log end @@ -278,6 +281,17 @@ def initialize(opts = {}) attr_accessor :sdk_start_time + attr_accessor :on_demand_fetch_retry_delay_ms + attr_accessor :on_demand_fetch_max_retries + + def self.default_on_demand_fetch_retry_delay_ms + 50 + end + + def self.default_on_demand_fetch_max_retries + 10 + end + def self.default_impressions_mode :optimized end diff --git a/spec/engine/api/segments_spec.rb b/spec/engine/api/segments_spec.rb index af112ae1..755a2890 100644 --- a/spec/engine/api/segments_spec.rb +++ b/spec/engine/api/segments_spec.rb @@ -43,6 +43,28 @@ expect(log.string).to include ':added=>["max", "dan"]' end + it 'returns fetch_segments - with till param' do + stub_request(:get, 'https://sdk.split.io/api/segmentChanges/employees?since=-1&till=222334') + .with(headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip', + 'Authorization' => 'Bearer', + 'Connection' => 'keep-alive', + 'Keep-Alive' => '30', + 'Splitsdkversion' => "#{config.language}-#{config.version}" + }) + .to_return(status: 200, body: segments) + + fetch_options = { cache_control_headers: false, till: 222_334 } + returned_segment = segments_api.send(:fetch_segment_changes, 'employees', -1, fetch_options) + + expect(returned_segment[:name]).to eq 'employees' + + expect(log.string).to include "'employees' segment retrieved." + expect(log.string).to include "'employees' 2 added keys" + expect(log.string).to include ':added=>["max", "dan"]' + end + it 'returns fetch_segments - checking headers when cache_control_headers is true' do stub_request(:get, 'https://sdk.split.io/api/segmentChanges/employees?since=-1') .with(headers: { @@ -56,7 +78,8 @@ }) .to_return(status: 200, body: segments) - returned_segment = segments_api.send(:fetch_segment_changes, 'employees', -1, true) + fetch_options = { cache_control_headers: true, till: nil } + returned_segment = segments_api.send(:fetch_segment_changes, 'employees', -1, fetch_options) expect(returned_segment[:name]).to eq 'employees' diff --git a/spec/engine/api/splits_spec.rb b/spec/engine/api/splits_spec.rb index 374fda99..bc2fcb03 100644 --- a/spec/engine/api/splits_spec.rb +++ b/spec/engine/api/splits_spec.rb @@ -47,7 +47,27 @@ expect(log.string).to include returned_splits.to_s end - it 'returns the splits - checking headers when cache_control_headers is true ' do + it 'returns the splits - with till param' do + stub_request(:get, 'https://sdk.split.io/api/splitChanges?since=-1&till=123123') + .with(headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip', + 'Authorization' => 'Bearer', + 'Connection' => 'keep-alive', + 'Keep-Alive' => '30', + 'Splitsdkversion' => "#{config.language}-#{config.version}" + }) + .to_return(status: 200, body: splits) + + fetch_options = { cache_control_headers: false, till: 123_123 } + returned_splits = splits_api.since(-1, fetch_options) + expect(returned_splits[:segment_names]).to eq(Set.new(%w[demo employees])) + + expect(log.string).to include '2 splits retrieved. since=-1' + expect(log.string).to include returned_splits.to_s + end + + it 'returns the splits - checking headers when cache_control_headers is true' do stub_request(:get, 'https://sdk.split.io/api/splitChanges?since=-1') .with(headers: { 'Accept' => '*/*', @@ -60,7 +80,8 @@ }) .to_return(status: 200, body: splits) - returned_splits = splits_api.since(-1, true) + fetch_options = { cache_control_headers: true, till: nil } + returned_splits = splits_api.since(-1, fetch_options) expect(returned_splits[:segment_names]).to eq(Set.new(%w[demo employees])) expect(log.string).to include '2 splits retrieved. since=-1' diff --git a/spec/splitclient/split_config_spec.rb b/spec/splitclient/split_config_spec.rb index 8cb8ff04..97b675f2 100644 --- a/spec/splitclient/split_config_spec.rb +++ b/spec/splitclient/split_config_spec.rb @@ -31,6 +31,8 @@ expect(configs.ip_addresses_enabled).to eq default_ip expect(configs.machine_name).to eq SplitIoClient::SplitConfig.machine_hostname(default_ip, nil, :redis) expect(configs.machine_ip).to eq SplitIoClient::SplitConfig.machine_ip(default_ip, nil, :redis) + expect(configs.on_demand_fetch_retry_delay_ms).to eq SplitIoClient::SplitConfig.default_on_demand_fetch_retry_delay_ms + expect(configs.on_demand_fetch_max_retries).to eq SplitIoClient::SplitConfig.default_on_demand_fetch_max_retries end it 'stores and retrieves correctly the customized values' do