Skip to content

Commit

Permalink
Replace threading.Lock with a boolean flag in _WebsocketClient (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkkekus authored Sep 24, 2024
1 parent 8e6fe72 commit 4721373
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions packages/atproto_firehose/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import random
import socket
import threading
import time
import traceback
import typing as t
Expand Down Expand Up @@ -137,8 +136,7 @@ class _WebsocketClient(_WebsocketClientBase):
def __init__(self, method: str, base_uri: str, params: t.Optional[t.Dict[str, t.Any]] = None) -> None:
super().__init__(method, base_uri, params)

# TODO(DXsmiley): Not sure if this should be a Lock or not, the async is using an Event now
self._stop_lock = threading.Lock()
self._stopped = False

self._on_message_callback: t.Optional[OnMessageCallback] = None
self._on_callback_error_callback: t.Optional[OnCallbackErrorCallback] = None
Expand Down Expand Up @@ -173,15 +171,15 @@ def start(
self._on_message_callback = on_message_callback
self._on_callback_error_callback = on_callback_error_callback

while not self._stop_lock.locked():
while not self._stopped:
try:
if self._reconnect_no != 0:
time.sleep(self._get_reconnection_delay())

with self._get_client() as client:
self._reconnect_no = 0

while not self._stop_lock.locked():
while not self._stopped:
raw_frame = client.recv()
if isinstance(raw_frame, str):
# skip text frames (should not be occurred)
Expand All @@ -199,17 +197,13 @@ def start(
if should_stop:
break

if self._stop_lock.locked():
self._stop_lock.release()

def stop(self) -> None:
"""Unsubscribe and stop the Firehose client.
Returns:
:obj:`None`
"""
if not self._stop_lock.locked():
self._stop_lock.acquire()
self._stopped = True


class _AsyncWebsocketClient(_WebsocketClientBase):
Expand Down

0 comments on commit 4721373

Please sign in to comment.