Skip to content

Commit 87c56c9

Browse files
authored
feat: Add option to enable event payload compression (#300)
1 parent d152455 commit 87c56c9

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

contract-tests/client_entity.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(self, tag, config):
4141

4242
if config.get("events") is not None:
4343
events = config["events"]
44+
opts["enable_event_compression"] = events.get("enableGzip", False)
4445
if events.get("baseUri") is not None:
4546
opts["events_uri"] = events["baseUri"]
4647
if events.get("capacity") is not None:

contract-tests/service.py

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def status():
7070
'secure-mode-hash',
7171
'tags',
7272
'migrations',
73+
'event-gzip',
74+
'optional-event-gzip',
7375
'event-sampling',
7476
'polling-gzip',
7577
'inline-context',

ldclient/config.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ def __init__(self,
175175
http: HTTPConfig=HTTPConfig(),
176176
big_segments: Optional[BigSegmentsConfig]=None,
177177
application: Optional[dict]=None,
178-
hooks: Optional[List[Hook]]=None):
178+
hooks: Optional[List[Hook]]=None,
179+
enable_event_compression: bool=False):
179180
"""
180181
:param sdk_key: The SDK key for your LaunchDarkly account. This is always required.
181182
:param base_uri: The base URL for the LaunchDarkly server. Most users should use the default
@@ -241,6 +242,7 @@ def __init__(self,
241242
:class:`HTTPConfig`.
242243
:param application: Optional properties for setting application metadata. See :py:attr:`~application`
243244
:param hooks: Hooks provide entrypoints which allow for observation of SDK functions.
245+
:param enable_event_compression: Whether or not to enable GZIP compression for outgoing events.
244246
"""
245247
self.__sdk_key = sdk_key
246248

@@ -274,6 +276,7 @@ def __init__(self,
274276
self.__big_segments = BigSegmentsConfig() if not big_segments else big_segments
275277
self.__application = validate_application_info(application or {}, log)
276278
self.__hooks = [hook for hook in hooks if isinstance(hook, Hook)] if hooks else []
279+
self.__enable_event_compression = enable_event_compression
277280
self._data_source_update_sink: Optional[DataSourceUpdateSink] = None
278281

279282
def copy_with_new_sdk_key(self, new_sdk_key: str) -> 'Config':
@@ -459,6 +462,10 @@ def hooks(self) -> List[Hook]:
459462
"""
460463
return self.__hooks
461464

465+
@property
466+
def enable_event_compression(self) -> bool:
467+
return self.__enable_event_compression
468+
462469
@property
463470
def data_source_update_sink(self) -> Optional[DataSourceUpdateSink]:
464471
"""

ldclient/impl/events/event_processor.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import uuid
1313
import queue
1414
import urllib3
15+
import gzip
1516
from ldclient.config import Config
1617
from datetime import timedelta
1718
from random import Random
@@ -559,19 +560,23 @@ def _post_events_with_retry(
559560
):
560561
hdrs = _headers(config)
561562
hdrs['Content-Type'] = 'application/json'
563+
if config.enable_event_compression:
564+
hdrs['Content-Encoding'] = 'gzip'
565+
562566
if payload_id:
563567
hdrs['X-LaunchDarkly-Event-Schema'] = str(__CURRENT_EVENT_SCHEMA__)
564568
hdrs['X-LaunchDarkly-Payload-ID'] = payload_id
565569
can_retry = True
566570
context = "posting %s" % events_description
571+
data = gzip.compress(bytes(body, 'utf-8')) if config.enable_event_compression else body
567572
while True:
568573
next_action_message = "will retry" if can_retry else "some events were dropped"
569574
try:
570575
r = http_client.request(
571576
'POST',
572577
uri,
573578
headers=hdrs,
574-
body=body,
579+
body=data,
575580
timeout=urllib3.Timeout(connect=config.http.connect_timeout, read=config.http.read_timeout),
576581
retries=0
577582
)

0 commit comments

Comments
 (0)