Skip to content

Commit 33ba5d6

Browse files
authored
feat: increase message and batch sizes (#108)
1 parent 3515c40 commit 33ba5d6

File tree

6 files changed

+22
-12
lines changed

6 files changed

+22
-12
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ jobs:
6262
6363
- name: Run posthog tests
6464
run: |
65-
python setup.py test
65+
pytest --verbose --timeout=30

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.1.0 - 2023-12-04
2+
3+
1. Increase maximum event size and batch size
4+
15
## 3.0.2 - 2023-08-17
26

37
1. Returns the current flag property with $feature_flag_called events, to make it easier to use in experiments

posthog/consumer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
except ImportError:
1313
from Queue import Empty
1414

15-
MAX_MSG_SIZE = 32 << 10
1615

17-
# Our servers only accept batches less than 500KB. Here limit is set slightly
18-
# lower to leave space for extra data that will be added later, eg. "sentAt".
19-
BATCH_SIZE_LIMIT = 475000
16+
MAX_MSG_SIZE = 900 * 1024 # 900KiB per event
17+
18+
# The maximum request body size is currently 20MiB, let's be conservative
19+
# in case we want to lower it in the future.
20+
BATCH_SIZE_LIMIT = 5 * 1024 * 1024
2021

2122

2223
class Consumer(Thread):
@@ -104,7 +105,7 @@ def next(self):
104105
item = queue.get(block=True, timeout=self.flush_interval - elapsed)
105106
item_size = len(json.dumps(item, cls=DatetimeSerializer).encode())
106107
if item_size > MAX_MSG_SIZE:
107-
self.log.error("Item exceeds 32kb limit, dropping. (%s)", str(item))
108+
self.log.error("Item exceeds 900kib limit, dropping. (%s)", str(item))
108109
continue
109110
items.append(item)
110111
total_size += item_size

posthog/test/test_consumer.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,20 @@ def test_pause(self):
145145
def test_max_batch_size(self):
146146
q = Queue()
147147
consumer = Consumer(q, TEST_API_KEY, flush_at=100000, flush_interval=3)
148-
track = {"type": "track", "event": "python event", "distinct_id": "distinct_id"}
148+
properties = {}
149+
for n in range(0, 500):
150+
properties[str(n)] = "one_long_property_value_to_build_a_big_event"
151+
track = {"type": "track", "event": "python event", "distinct_id": "distinct_id", "properties": properties}
149152
msg_size = len(json.dumps(track).encode())
150-
# number of messages in a maximum-size batch
151-
n_msgs = int(475000 / msg_size)
153+
# Let's capture 8MB of data to trigger two batches
154+
n_msgs = int(8_000_000 / msg_size)
152155

153156
def mock_post_fn(_, data, **kwargs):
154157
res = mock.Mock()
155158
res.status_code = 200
156-
self.assertTrue(len(data.encode()) < 500000, "batch size (%d) exceeds 500KB limit" % len(data.encode()))
159+
request_size = len(data.encode())
160+
# Batches close after the first message bringing it bigger than BATCH_SIZE_LIMIT, let's add 10% of margin
161+
self.assertTrue(request_size < (5 * 1024 * 1024) * 1.1, "batch size (%d) higher than limit" % request_size)
157162
return res
158163

159164
with mock.patch("posthog.request._session.post", side_effect=mock_post_fn) as mock_post:

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.0.2"
1+
VERSION = "3.1.0"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"flake8-print",
2525
"pre-commit",
2626
],
27-
"test": ["mock>=2.0.0", "freezegun==0.3.15", "pylint", "flake8", "coverage", "pytest"],
27+
"test": ["mock>=2.0.0", "freezegun==0.3.15", "pylint", "flake8", "coverage", "pytest", "pytest-timeout"],
2828
"sentry": ["sentry-sdk", "django"],
2929
}
3030

0 commit comments

Comments
 (0)