Skip to content

Commit

Permalink
Only stop polling on a successful request from SSE
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieSinn committed Aug 19, 2024
1 parent 94d8af6 commit 3198d18
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def initialize(sdkKey, local_bucketing, wait_for_init)
@local_bucketing = local_bucketing
@sdkKey = sdkKey
@sse_url = ""
@sse_polling = false
@config_e_tag = ""
@config_last_modified = ""
@logger = local_bucketing.options.logger
Expand Down Expand Up @@ -59,7 +60,7 @@ def fetch_config(min_last_modified: -1)
})

begin
# Blind parse the LM string to check if it's a valid date.
# Blind parse the lastmodified string to check if it's a valid date.
# This short circuits the rest of the checks if it's not set
stored_date = Date.parse(@config_last_modified)
if @config_last_modified != ""
Expand Down Expand Up @@ -142,8 +143,8 @@ def set_config(config, etag, lastmodified)
if parsed_config['sse'] != nil
raw_url = "#{parsed_config['sse']['hostname']}#{parsed_config['sse']['path']}"
if @sse_url != raw_url && raw_url != ""
@sse_url = raw_url
stop_sse
@sse_url = raw_url
init_sse(@sse_url)
end
end
Expand All @@ -162,9 +163,10 @@ def get_config_url
def start_polling(sse)
if sse
@config_poller.shutdown if @config_poller.running?
@config_poller = Concurrent::TimerTask.new({ execution_interval: 60 *10 }) do |_|
@config_poller = Concurrent::TimerTask.new({ execution_interval: 60 * 10 }) do |_|
fetch_config
end
@sse_polling = sse
end
@polling_enabled = true
@config_poller.execute if @polling_enabled && (!@sse_active || sse)
Expand All @@ -177,10 +179,10 @@ def stop_polling()

def stop_sse
return unless @enable_sse
@polling_enabled = true
@sse_active = false
@sse_polling = false
@sse_client.close if @sse_client
start_polling(false)
start_polling(@sse_polling)
end

def close
Expand All @@ -194,29 +196,32 @@ def init_sse(path)
@sse_active = true
@sse_client = SSE::Client.new(path) do |client|
client.on_event do |event|

parsed_json = JSON.parse(event.data)
handle_sse(parsed_json)
end
client.on_error do |error|
@logger.debug("SSE Error: #{error.message}")
end
end
stop_polling
start_polling(true)
end

def handle_sse(eventData)
if eventData["data"] == nil
def handle_sse(event_data)
unless @sse_polling
stop_polling
start_polling(true)
end
if event_data["data"] == nil
return
end
@logger.debug("SSE Message received: #{eventData["data"]}")
parsed_event_data = JSON.parse(eventData["data"])
@logger.debug("SSE: Message received: #{event_data["data"]}")
parsed_event_data = JSON.parse(event_data["data"])

last_modified = parsed_event_data["lastModified"]
event_type = parsed_event_data["type"]

if event_type == "refetchConfig" || event_type == nil
@logger.debug("Re-fetching new config with TS: #{last_modified}")
@logger.debug("SSE: Re-fetching new config with TS: #{last_modified}")
fetch_config(min_last_modified: last_modified / 1000)
end
end
Expand Down

0 comments on commit 3198d18

Please sign in to comment.