nostrclient/nostr/relay.py
Vlad Stan 16ae9d15a1
Stabilize (#24)
* refactor: clean-up

* refactor: extra logs plus try-catch

* refactor: do not use bare `except`

* refactor: clean-up redundant fields

* chore: pass code checks

* chore: code format

* refactor: code clean-up

* fix: refactoring stuff

* refactor: remove un-used file

* chore: code clean-up

* chore: code clean-up

* chore: code-format fix

* refactor: remove nostr.client wrapper

* refactor: code clean-up

* chore: code format

* refactor: remove `RelayList` class

* refactor: extract smaller methods with try-catch

* fix: better exception handling

* fix: remove redundant filters

* fix: simplify event

* chore: code format

* fix: code check

* fix: code check

* fix: simplify `REQ`

* fix: more clean-ups

* refactor: use simpler method

* refactor: re-order and rename

* fix: stop logic

* fix: subscription close before disconnect

* chore: play commit
2023-11-01 17:46:42 +02:00

126 lines
3.7 KiB
Python

import asyncio
import json
import time
from queue import Queue
from typing import List
from loguru import logger
from websocket import WebSocketApp
from .message_pool import MessagePool
from .subscription import Subscription
class Relay:
def __init__(self, url: str, message_pool: MessagePool) -> None:
self.url = url
self.message_pool = message_pool
self.connected: bool = False
self.reconnect: bool = True
self.shutdown: bool = False
self.error_counter: int = 0
self.error_threshold: int = 100
self.error_list: List[str] = []
self.notice_list: List[str] = []
self.last_error_date: int = 0
self.num_received_events: int = 0
self.num_sent_events: int = 0
self.num_subscriptions: int = 0
self.queue = Queue()
def connect(self):
self.ws = WebSocketApp(
self.url,
on_open=self._on_open,
on_message=self._on_message,
on_error=self._on_error,
on_close=self._on_close,
on_ping=self._on_ping,
on_pong=self._on_pong,
)
if not self.connected:
self.ws.run_forever(ping_interval=10)
def close(self):
try:
self.ws.close()
except Exception as e:
logger.warning(f"[Relay: {self.url}] Failed to close websocket: {e}")
self.connected = False
self.shutdown = True
@property
def error_threshold_reached(self):
return self.error_threshold and self.error_counter >= self.error_threshold
@property
def ping(self):
ping_ms = int((self.ws.last_pong_tm - self.ws.last_ping_tm) * 1000)
return ping_ms if self.connected and ping_ms > 0 else 0
def publish(self, message: str):
self.queue.put(message)
def publish_subscriptions(self, subscriptions: List[Subscription] = []):
for s in subscriptions:
json_str = json.dumps(["REQ", s.id] + s.filters)
self.publish(json_str)
async def queue_worker(self):
while True:
if self.connected:
try:
message = self.queue.get(timeout=1)
self.num_sent_events += 1
self.ws.send(message)
except Exception as _:
pass
else:
await asyncio.sleep(1)
if self.shutdown:
logger.warning(f"[Relay: {self.url}] Closing queue worker.")
return
def close_subscription(self, id: str) -> None:
try:
self.publish(json.dumps(["CLOSE", id]))
except Exception as e:
logger.debug(f"[Relay: {self.url}] Failed to close subscription: {e}")
def add_notice(self, notice: str):
self.notice_list = [notice] + self.notice_list
def _on_open(self, _):
logger.info(f"[Relay: {self.url}] Connected.")
self.connected = True
self.shutdown = False
def _on_close(self, _, status_code, message):
logger.warning(
f"[Relay: {self.url}] Connection closed."
+ f" Status: '{status_code}'. Message: '{message}'."
)
self.close()
def _on_message(self, _, message: str):
self.num_received_events += 1
self.message_pool.add_message(message, self.url)
def _on_error(self, _, error):
logger.warning(f"[Relay: {self.url}] Error: '{str(error)}'")
self._append_error_message(str(error))
self.close()
def _on_ping(self, *_):
return
def _on_pong(self, *_):
return
def _append_error_message(self, message):
self.error_counter += 1
self.error_list = [message] + self.error_list
self.last_error_date = int(time.time())