Skip to content

Commit

Permalink
followup to #41
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffthibault committed Feb 16, 2023
1 parent 4e80a9c commit 37cb66b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 7 additions & 5 deletions nostr/relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ class Relay:
url: str
message_pool: MessagePool
policy: RelayPolicy = RelayPolicy()
proxy_config: RelayProxyConnectionConfig = None
ssl_options: Optional[dict] = None
proxy_config: RelayProxyConnectionConfig = None

def __post_init__(self):
self.queue = Queue()
self.subscriptions: dict[str, Subscription] = {}
self.num_sent_events: int = 0
self.connected: bool = False
self.reconnect: bool = True
self.error_counter: int = 0
Expand Down Expand Up @@ -82,8 +83,11 @@ def queue_worker(self):
while True:
if self.connected:
message = self.queue.get()
self.num_sent_events += 1
self.ws.send(message)
try:
self.ws.send(message)
self.num_sent_events += 1
except:
self.queue.put(message)
else:
time.sleep(0.1)

Expand Down Expand Up @@ -112,11 +116,9 @@ def to_json_object(self) -> dict:

def _on_open(self, class_obj):
self.connected = True
pass

def _on_close(self, class_obj, status_code, message):
self.connected = False
pass

def _on_message(self, class_obj, message: str):
self.message_pool.add_message(message, self.url)
Expand Down
7 changes: 4 additions & 3 deletions nostr/relay_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ def add_relay(
).start()

threading.Thread(
target=relay.queue_worker,
name=f"{relay.url}-queue",
).start()
target=relay.queue_worker,
name=f"{relay.url}-queue",
daemon=True
).start()

time.sleep(1)

Expand Down

0 comments on commit 37cb66b

Please sign in to comment.