Skip to content

Commit

Permalink
Raise exception on large message
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidPal committed Mar 1, 2022
1 parent 1156338 commit 9542b45
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ MANIFEST
build
.eggs
*.bat
.vscode/
.vscode/
.idea/
.python-version
11 changes: 8 additions & 3 deletions segment/analytics/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import logging
import numbers
import atexit
import json

from dateutil.tz import tzutc
from six import string_types

from segment.analytics.utils import guess_timezone, clean
from segment.analytics.consumer import Consumer
from segment.analytics.request import post
from segment.analytics.consumer import Consumer, MAX_MSG_SIZE
from segment.analytics.request import post, DatetimeSerializer
from segment.analytics.version import VERSION

try:
Expand Down Expand Up @@ -37,7 +38,6 @@ class DefaultConfig(object):
thread = 1
upload_interval = 0.5
upload_size = 100
max_retries = 10

"""Create a new Segment client."""
log = logging.getLogger('segment')
Expand Down Expand Up @@ -273,6 +273,11 @@ def _enqueue(self, msg):
msg = clean(msg)
self.log.debug('queueing: %s', msg)

# Check message size.
msg_size = len(json.dumps(msg, cls=DatetimeSerializer).encode())
if msg_size > MAX_MSG_SIZE:
raise RuntimeError('Message exceeds 32kb limit. (%s)', str(msg))

# if send is False, return msg as if it was successfully queued
if not self.send:
return True, msg
Expand Down

0 comments on commit 9542b45

Please sign in to comment.