Skip to content

Commit

Permalink
feat: Enable gzip support for event payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Apr 19, 2024
1 parent 30b7ac0 commit 35155ff
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions contract-tests/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'secure-mode-hash',
'tags',
'migrations',
'event-gzip',
'event-sampling',
'context-comparison',
'polling-gzip',
Expand Down
1 change: 1 addition & 0 deletions launchdarkly-server-sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "semantic", "~> 1.6"
spec.add_runtime_dependency "concurrent-ruby", "~> 1.1"
spec.add_runtime_dependency "ld-eventsource", "2.2.2"
spec.add_runtime_dependency "zlib", "~> 3.1"
# Please keep ld-eventsource dependency as an exact version so that bugfixes to
# that LD library are always associated with a new SDK version.

Expand Down
7 changes: 6 additions & 1 deletion lib/ldclient-rb/impl/event_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require "securerandom"
require "http"
require "stringio"
require "zlib"

module LaunchDarkly
module Impl
Expand Down Expand Up @@ -42,14 +44,17 @@ def send_event_data(event_data, description, is_diagnostic)
@logger.debug { "[LDClient] sending #{description}: #{event_data}" }
headers = {}
headers["content-type"] = "application/json"
headers["content-encoding"] = "gzip"
Impl::Util.default_http_headers(@sdk_key, @config).each { |k, v| headers[k] = v }
unless is_diagnostic
headers["X-LaunchDarkly-Event-Schema"] = CURRENT_SCHEMA_VERSION.to_s
headers["X-LaunchDarkly-Payload-ID"] = payload_id
end
gzip = Zlib::GzipWriter.new(StringIO.new)
gzip << event_data
response = http_client.request("POST", uri, {
headers: headers,
body: event_data,
body: gzip.close.string,
})
rescue StandardError => exn
@logger.warn { "[LDClient] Error sending events: #{exn.inspect}." }
Expand Down
13 changes: 7 additions & 6 deletions spec/http_util.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "webrick"
require "webrick/httpproxy"
require "webrick/https"
require "stringio"
require "zlib"

class StubHTTPServer
attr_reader :requests, :port
Expand Down Expand Up @@ -73,14 +75,13 @@ def record_request(req, res)
@requests_queue << [req, req.body]
end

def await_request
r = @requests_queue.pop
r[0]
end

def await_request_with_body
r = @requests_queue.pop
[r[0], r[1]]
body = r[1]

gz = Zlib::GzipReader.new(StringIO.new(body.to_s))

[r[0], gz.read]
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/impl/event_sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def with_sender_and_server
expect(result.must_shutdown).to be false
expect(result.time_from_server).not_to be_nil

req = server.await_request
expect(req.body).to eq fake_data
req, body = server.await_request_with_body
expect(body).to eq fake_data
expect(req.header).to include({
"authorization" => [ sdk_key ],
"content-type" => [ "application/json" ],
Expand All @@ -63,8 +63,8 @@ def with_sender_and_server
result = es.send_event_data(fake_data, "", false)

expect(result.success).to be true
req = server.await_request
expect(req.body).to eq fake_data
req, body = server.await_request_with_body
expect(body).to eq fake_data
expect(req.host).to eq "fake-event-server"
end
end
Expand Down

0 comments on commit 35155ff

Please sign in to comment.