Skip to content

Commit

Permalink
Merge pull request #128 from launchdarkly/eb/ch61092/event-payload-id
Browse files Browse the repository at this point in the history
add event payload ID header
  • Loading branch information
eli-darkly authored Jan 15, 2020
2 parents 0c90251 + 1cfcd52 commit 640387c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/ldclient-rb/events.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "concurrent"
require "concurrent/atomics"
require "concurrent/executors"
require "securerandom"
require "thread"
require "time"

Expand Down Expand Up @@ -359,6 +360,7 @@ def run(sdk_key, config, client, payload, formatter)
events_out = formatter.make_output_events(payload.events, payload.summary)
res = nil
body = events_out.to_json
payload_id = SecureRandom.uuid
(0..1).each do |attempt|
if attempt > 0
config.logger.warn { "[LDClient] Will retry posting events after 1 second" }
Expand All @@ -374,6 +376,7 @@ def run(sdk_key, config, client, payload, formatter)
req["Authorization"] = sdk_key
req["User-Agent"] = "RubyClient/" + LaunchDarkly::VERSION
req["X-LaunchDarkly-Event-Schema"] = CURRENT_SCHEMA_VERSION.to_s
req["X-LaunchDarkly-Payload-ID"] = payload_id
req["Connection"] = "keep-alive"
res = client.request(req)
rescue StandardError => exn
Expand Down
34 changes: 32 additions & 2 deletions spec/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,29 @@
expect(hc.get_request["authorization"]).to eq "sdk_key"
end

it "sends unique payload IDs" do
@ep = subject.new("sdk_key", default_config, hc)
e = { kind: "identify", user: user }

@ep.add_event(e)
@ep.flush
@ep.wait_until_inactive
req0 = hc.get_request

@ep.add_event(e)
@ep.flush
@ep.wait_until_inactive
req1 = hc.get_request

id0 = req0["x-launchdarkly-payload-id"]
id1 = req1["x-launchdarkly-payload-id"]
expect(id0).not_to be_nil
expect(id0).not_to eq ""
expect(id1).not_to be nil
expect(id1).not_to eq ""
expect(id1).not_to eq id0
end

def verify_unrecoverable_http_error(status)
@ep = subject.new("sdk_key", default_config, hc)
e = { kind: "identify", user: user }
Expand All @@ -442,8 +465,15 @@ def verify_recoverable_http_error(status)
@ep.flush
@ep.wait_until_inactive

expect(hc.get_request).not_to be_nil
expect(hc.get_request).not_to be_nil
req0 = hc.get_request
expect(req0).not_to be_nil
req1 = hc.get_request
expect(req1).not_to be_nil
id0 = req0["x-launchdarkly-payload-id"]
expect(id0).not_to be_nil
expect(id0).not_to eq ""
expect(req1["x-launchdarkly-payload-id"]).to eq id0

expect(hc.get_request).to be_nil # no 3rd request

# now verify that a subsequent flush still generates a request
Expand Down

0 comments on commit 640387c

Please sign in to comment.