Skip to content

Commit 35155ff

Browse files
committed
feat: Enable gzip support for event payloads
1 parent 30b7ac0 commit 35155ff

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

contract-tests/service.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
'secure-mode-hash',
3535
'tags',
3636
'migrations',
37+
'event-gzip',
3738
'event-sampling',
3839
'context-comparison',
3940
'polling-gzip',

launchdarkly-server-sdk.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
3838
spec.add_runtime_dependency "semantic", "~> 1.6"
3939
spec.add_runtime_dependency "concurrent-ruby", "~> 1.1"
4040
spec.add_runtime_dependency "ld-eventsource", "2.2.2"
41+
spec.add_runtime_dependency "zlib", "~> 3.1"
4142
# Please keep ld-eventsource dependency as an exact version so that bugfixes to
4243
# that LD library are always associated with a new SDK version.
4344

lib/ldclient-rb/impl/event_sender.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require "securerandom"
44
require "http"
5+
require "stringio"
6+
require "zlib"
57

68
module LaunchDarkly
79
module Impl
@@ -42,14 +44,17 @@ def send_event_data(event_data, description, is_diagnostic)
4244
@logger.debug { "[LDClient] sending #{description}: #{event_data}" }
4345
headers = {}
4446
headers["content-type"] = "application/json"
47+
headers["content-encoding"] = "gzip"
4548
Impl::Util.default_http_headers(@sdk_key, @config).each { |k, v| headers[k] = v }
4649
unless is_diagnostic
4750
headers["X-LaunchDarkly-Event-Schema"] = CURRENT_SCHEMA_VERSION.to_s
4851
headers["X-LaunchDarkly-Payload-ID"] = payload_id
4952
end
53+
gzip = Zlib::GzipWriter.new(StringIO.new)
54+
gzip << event_data
5055
response = http_client.request("POST", uri, {
5156
headers: headers,
52-
body: event_data,
57+
body: gzip.close.string,
5358
})
5459
rescue StandardError => exn
5560
@logger.warn { "[LDClient] Error sending events: #{exn.inspect}." }

spec/http_util.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require "webrick"
22
require "webrick/httpproxy"
33
require "webrick/https"
4+
require "stringio"
5+
require "zlib"
46

57
class StubHTTPServer
68
attr_reader :requests, :port
@@ -73,14 +75,13 @@ def record_request(req, res)
7375
@requests_queue << [req, req.body]
7476
end
7577

76-
def await_request
77-
r = @requests_queue.pop
78-
r[0]
79-
end
80-
8178
def await_request_with_body
8279
r = @requests_queue.pop
83-
[r[0], r[1]]
80+
body = r[1]
81+
82+
gz = Zlib::GzipReader.new(StringIO.new(body.to_s))
83+
84+
[r[0], gz.read]
8485
end
8586
end
8687

spec/impl/event_sender_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def with_sender_and_server
3737
expect(result.must_shutdown).to be false
3838
expect(result.time_from_server).not_to be_nil
3939

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

6565
expect(result.success).to be true
66-
req = server.await_request
67-
expect(req.body).to eq fake_data
66+
req, body = server.await_request_with_body
67+
expect(body).to eq fake_data
6868
expect(req.host).to eq "fake-event-server"
6969
end
7070
end

0 commit comments

Comments
 (0)