Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace threading.Lock with a boolean flag in _WebsocketClient #402

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading