Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,39 @@ def initialize(api_key, config = Config.default)
@worker = create_worker()
end

def flush()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may cause thread-safety issues if there is a regularly-scheduled flush happening at the same time as a manual flush.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Changed so that we handle the case where the pop method has fewer elements than we expect. The underlying queue is thread-safe, so the rest of this should be ok.

events = []
num_events = @queue.length()
begin
num_events.times do
events << @queue.pop(true)
end
rescue
end


if !events.empty?()
res =
@client.post (@config.base_uri + "/api/events/bulk") do |req|
req.headers['Authorization'] = 'api_key ' + @api_key
req.headers['User-Agent'] = 'RubyClient/' + LaunchDarkly::VERSION
req.headers['Content-Type'] = 'application/json'
req.body = events.to_json
req.options.timeout = @config.read_timeout
req.options.open_timeout = @config.connect_timeout
end
if res.status != 200
@config.logger.error("[LDClient] Unexpected status code while processing events: #{res.status}")
end
end
end


def create_worker()
Thread.new do
while true do
begin
events = []
num_events = @queue.length()
num_events.times do
events << @queue.pop()
end

if !events.empty?()
res =
@client.post (@config.base_uri + "/api/events/bulk") do |req|
req.headers['Authorization'] = 'api_key ' + @api_key
req.headers['User-Agent'] = 'RubyClient/' + LaunchDarkly::VERSION
req.headers['Content-Type'] = 'application/json'
req.body = events.to_json
req.options.timeout = @config.read_timeout
req.options.open_timeout = @config.connect_timeout
end
if res.status != 200
@config.logger.error("[LDClient] Unexpected status code while processing events: #{res.status}")
end
end
flush()

sleep(@config.flush_interval)
rescue Exception => exn
Expand Down