From 4c6fed4b10c55742a0a8b1f593d062a7e47c8f09 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 18 Apr 2023 17:47:17 +0200 Subject: [PATCH 1/3] create ws on connect --- nostr/relay.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nostr/relay.py b/nostr/relay.py index db9cacf..7595d9a 100644 --- a/nostr/relay.py +++ b/nostr/relay.py @@ -43,8 +43,10 @@ class Relay: self.proxy: dict = {} self.lock = Lock() self.queue = Queue() + + def connect(self, ssl_options: dict = None, proxy: dict = None): self.ws = WebSocketApp( - url, + self.url, on_open=self._on_open, on_message=self._on_message, on_error=self._on_error, @@ -52,8 +54,6 @@ class Relay: on_ping=self._on_ping, on_pong=self._on_pong, ) - - def connect(self, ssl_options: dict = None, proxy: dict = None): self.ssl_options = ssl_options self.proxy = proxy if not self.connected: From ea9fc18c151cd7fd500c8367be6f7952619cbb82 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 18 Apr 2023 17:50:41 +0200 Subject: [PATCH 2/3] reconnect on on_close --- nostr/relay.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nostr/relay.py b/nostr/relay.py index 7595d9a..2649a4d 100644 --- a/nostr/relay.py +++ b/nostr/relay.py @@ -129,6 +129,10 @@ class Relay: def _on_close(self, class_obj, status_code, message): self.connected = False + if self.error_threshold and self.error_counter > self.error_threshold: + pass + else: + self.check_reconnect() pass def _on_message(self, class_obj, message: str): @@ -139,10 +143,6 @@ class Relay: def _on_error(self, class_obj, error): self.connected = False self.error_counter += 1 - if self.error_threshold and self.error_counter > self.error_threshold: - pass - else: - self.check_reconnect() def _on_ping(self, class_obj, message): return From 49f206eab305439a3105a43d7f0b607a14eea03d Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:54:38 +0200 Subject: [PATCH 3/3] exponential delay for reconnect --- nostr/relay.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nostr/relay.py b/nostr/relay.py index 2649a4d..7fb4baa 100644 --- a/nostr/relay.py +++ b/nostr/relay.py @@ -35,7 +35,7 @@ class Relay: self.reconnect: bool = True self.shutdown: bool = False self.error_counter: int = 0 - self.error_threshold: int = 0 + self.error_threshold: int = 100 self.num_received_events: int = 0 self.num_sent_events: int = 0 self.num_subscriptions: int = 0 @@ -76,7 +76,7 @@ class Relay: pass self.connected = False if self.reconnect: - time.sleep(1) + time.sleep(self.error_counter**2) self.connect(self.ssl_options, self.proxy) @property