Skip to content

TrickleClient: improve error handling in send_data_loop #27

@eliteprox

Description

@eliteprox

Investigate performance and error handling for send_data_loop:

async def _send_data_loop(self):
"""Send data to the server every 333ms, batching all available items."""
try:
while not self.stop_event.is_set() and not self.error_event.is_set():
# Wait for send_data_interval or until stop/error event is set
if await self._wait_for_interval(self.send_data_interval):
break # Stop or error event was set, exit loop
# Pull all available items from the data_queue
data_items = []
while len(self.data_queue) > 0 and not self.stop_event.is_set() and not self.error_event.is_set():
data = self.data_queue.popleft()
if data is None:
# Sentinel value to stop loop
if data_items:
# Send any remaining items before stopping
break
else:
return # No items to send, just stop
else:
data_items.append(data)
# Send all collected data items
if len(data_items) > 0:
try:
data_str = json.dumps(data_items) + "\n"
except Exception as e:
logger.error(f"Error serializing data items: {e}")
continue
await self.protocol.publish_data(data_str)
except Exception as e:
logger.error(f"Error in data sending loop: {e}")

We could write additional tests to validate performance of the data publisher code path in client.py

I think we should wrap this in a try/except block as well, and should allow us to remove the check for the queue length.

Originally posted by @pschroedl in #25 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions