Skip to content

Commit

Permalink
feat: Add option to enable event payload compression (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 authored Jul 25, 2024
1 parent d152455 commit 87c56c9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions contract-tests/client_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, tag, config):

if config.get("events") is not None:
events = config["events"]
opts["enable_event_compression"] = events.get("enableGzip", False)
if events.get("baseUri") is not None:
opts["events_uri"] = events["baseUri"]
if events.get("capacity") is not None:
Expand Down
2 changes: 2 additions & 0 deletions contract-tests/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def status():
'secure-mode-hash',
'tags',
'migrations',
'event-gzip',
'optional-event-gzip',
'event-sampling',
'polling-gzip',
'inline-context',
Expand Down
9 changes: 8 additions & 1 deletion ldclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ def __init__(self,
http: HTTPConfig=HTTPConfig(),
big_segments: Optional[BigSegmentsConfig]=None,
application: Optional[dict]=None,
hooks: Optional[List[Hook]]=None):
hooks: Optional[List[Hook]]=None,
enable_event_compression: bool=False):
"""
:param sdk_key: The SDK key for your LaunchDarkly account. This is always required.
:param base_uri: The base URL for the LaunchDarkly server. Most users should use the default
Expand Down Expand Up @@ -241,6 +242,7 @@ def __init__(self,
:class:`HTTPConfig`.
:param application: Optional properties for setting application metadata. See :py:attr:`~application`
:param hooks: Hooks provide entrypoints which allow for observation of SDK functions.
:param enable_event_compression: Whether or not to enable GZIP compression for outgoing events.
"""
self.__sdk_key = sdk_key

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

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

@property
def enable_event_compression(self) -> bool:
return self.__enable_event_compression

@property
def data_source_update_sink(self) -> Optional[DataSourceUpdateSink]:
"""
Expand Down
7 changes: 6 additions & 1 deletion ldclient/impl/events/event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import uuid
import queue
import urllib3
import gzip
from ldclient.config import Config
from datetime import timedelta
from random import Random
Expand Down Expand Up @@ -559,19 +560,23 @@ def _post_events_with_retry(
):
hdrs = _headers(config)
hdrs['Content-Type'] = 'application/json'
if config.enable_event_compression:
hdrs['Content-Encoding'] = 'gzip'

if payload_id:
hdrs['X-LaunchDarkly-Event-Schema'] = str(__CURRENT_EVENT_SCHEMA__)
hdrs['X-LaunchDarkly-Payload-ID'] = payload_id
can_retry = True
context = "posting %s" % events_description
data = gzip.compress(bytes(body, 'utf-8')) if config.enable_event_compression else body
while True:
next_action_message = "will retry" if can_retry else "some events were dropped"
try:
r = http_client.request(
'POST',
uri,
headers=hdrs,
body=body,
body=data,
timeout=urllib3.Timeout(connect=config.http.connect_timeout, read=config.http.read_timeout),
retries=0
)
Expand Down

0 comments on commit 87c56c9

Please sign in to comment.