@@ -5,28 +5,68 @@ module Api
55 # Retrieves split definitions from the Split Backend
66 class Splits < Client
77
8+ PROXY_CHECK_INTERVAL_SECONDS = 24 * 60 * 60
9+ SPEC_1_1 = "1.1"
10+
811 def initialize ( api_key , config , telemetry_runtime_producer )
912 super ( config )
1013 @api_key = api_key
1114 @telemetry_runtime_producer = telemetry_runtime_producer
1215 @flag_sets_filter = @config . flag_sets_filter
16+ @spec_version = SplitIoClient ::Spec ::FeatureFlags ::SPEC_VERSION
17+ @last_proxy_check_timestamp = 0
18+ @clear_storage = false
1319 end
1420
1521 def since ( since , since_rbs , fetch_options = { cache_control_headers : false , till : nil , sets : nil } )
1622 start = Time . now
23+
24+ if check_last_proxy_check_timestamp
25+ puts "switching to new spec"
26+ @spec_version = SplitIoClient ::Spec ::FeatureFlags ::SPEC_VERSION
27+ @config . logger . debug ( "Switching to new Feature flag spec #{ @spec_version } and fetching." )
28+ since = -1
29+ since_rbs = -1
30+ fetch_options = { cache_control_headers : false , till : nil , sets : nil }
31+ end
32+
33+ if @spec_version == Splits ::SPEC_1_1
34+ params = { s : @spec_version , since : since }
35+ else
36+ params = { s : @spec_version , since : since , rbSince : since_rbs }
37+ end
1738
18- params = { s : SplitIoClient ::Spec ::FeatureFlags ::SPEC_VERSION , since : since , rbSince : since_rbs }
1939 params [ :sets ] = @flag_sets_filter . join ( "," ) unless @flag_sets_filter . empty?
2040 params [ :till ] = fetch_options [ :till ] unless fetch_options [ :till ] . nil?
2141 @config . logger . debug ( "Fetching from splitChanges with #{ params } : " )
2242 response = get_api ( "#{ @config . base_uri } /splitChanges" , @api_key , params , fetch_options [ :cache_control_headers ] )
43+
2344 if response . status == 414
2445 @config . logger . error ( "Error fetching feature flags; the amount of flag sets provided are too big, causing uri length error." )
2546 raise ApiException . new response . body , 414
2647 end
48+
49+ if response . status == 400 and sdk_url_overriden? and @spec_version == SplitIoClient ::Spec ::FeatureFlags ::SPEC_VERSION
50+ @config . logger . warn ( "Detected proxy response error, changing spec version from #{ @spec_version } to #{ Splits ::SPEC_1_1 } and re-fetching." )
51+ @spec_version = Splits ::SPEC_1_1
52+ @last_proxy_check_timestamp = Time . now
53+ return since ( since , 0 , fetch_options = { cache_control_headers : fetch_options [ :cache_control_headers ] , till : fetch_options [ :till ] ,
54+ sets : fetch_options [ :sets ] } )
55+ end
56+
2757 if response . success?
28- result = objects_with_segment_names ( response . body )
58+ result = JSON . parse ( response . body , symbolize_names : true )
59+ if @spec_version == Splits ::SPEC_1_1
60+ result = convert_to_newSPEC ( result )
61+ end
62+
63+ result = objects_with_segment_names ( result )
2964
65+ if @spec_version == SplitIoClient ::Spec ::FeatureFlags ::SPEC_VERSION
66+ @clear_storage = @last_proxy_check_timestamp != 0
67+ @last_proxy_check_timestamp = 0
68+ end
69+
3070 unless result [ :ff ] [ :d ] . empty?
3171 @config . split_logger . log_if_debug ( "#{ result [ :ff ] [ :d ] . length } feature flags retrieved. since=#{ since } " )
3272 end
@@ -52,10 +92,13 @@ def since(since, since_rbs, fetch_options = { cache_control_headers: false, till
5292 end
5393 end
5494
95+ def clear_storage
96+ @clear_storage
97+ end
98+
5599 private
56100
57- def objects_with_segment_names ( objects_json )
58- parsed_objects = JSON . parse ( objects_json , symbolize_names : true )
101+ def objects_with_segment_names ( parsed_objects )
59102 parsed_objects [ :segment_names ] = Set . new
60103 parsed_objects [ :segment_names ] =
61104 parsed_objects [ :ff ] [ :d ] . each_with_object ( Set . new ) do |split , splits |
@@ -76,6 +119,14 @@ def objects_with_segment_names(objects_json)
76119
77120 parsed_objects
78121 end
122+
123+ def check_last_proxy_check_timestamp
124+ @spec_version == Splits ::SPEC_1_1 and ( ( Time . now - @last_proxy_check_timestamp ) >= Splits ::PROXY_CHECK_INTERVAL_SECONDS )
125+ end
126+
127+ def convert_to_newSPEC ( body )
128+ { :ff => { :d => body [ :splits ] , :s => body [ :since ] , :t => body [ :till ] } , :rbs => { :d => [ ] , :s => -1 , :t => -1 } }
129+ end
79130 end
80131 end
81132end
0 commit comments