From 18ca00f19526aa50989632dc7460adedc5322abf Mon Sep 17 00:00:00 2001 From: Amit Mathew Date: Tue, 8 Apr 2014 18:22:35 -0400 Subject: [PATCH] Made BufferedConsumer's _flush_endpoint more thread-safe by making sure only events that are sent are cleared from the buffer. This prevents a race condition in the mixpanel-python-async where events were dropped if queued up while other events were being sent. See https://github.com/jessepollak/mixpanel-python-async/issues/4 for more information. --- mixpanel/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mixpanel/__init__.py b/mixpanel/__init__.py index b04e36d..70acb79 100644 --- a/mixpanel/__init__.py +++ b/mixpanel/__init__.py @@ -371,12 +371,13 @@ def flush(self): def _flush_endpoint(self, endpoint): buf = self._buffers[endpoint] while buf: - batch = buf[:self._max_size] + batch_size = min(len(buf), self._max_size) + batch = buf[:batch_size] batch_json = '[{0}]'.format(','.join(batch)) try: self._consumer.send(endpoint, batch_json) except MixpanelException as e: e.message = 'batch_json' e.endpoint = endpoint - buf = buf[self._max_size:] + buf = buf[batch_size:] self._buffers[endpoint] = buf