fix: do not double re-connect

This commit is contained in:
Vlad Stan 2023-06-21 17:12:26 +03:00
parent 322679e7c5
commit d08e91b2c7

View file

@ -22,6 +22,8 @@ class RelayManager:
def add_relay( def add_relay(
self, url: str, read: bool = True, write: bool = True, subscriptions={} self, url: str, read: bool = True, write: bool = True, subscriptions={}
): ):
if url in self.relays:
return
policy = RelayPolicy(read, write) policy = RelayPolicy(read, write)
relay = Relay(url, policy, self.message_pool, subscriptions.copy()) relay = Relay(url, policy, self.message_pool, subscriptions.copy())
self.relays[url] = relay self.relays[url] = relay
@ -42,17 +44,19 @@ class RelayManager:
def open_connections(self, ssl_options: dict = None, proxy: dict = None): def open_connections(self, ssl_options: dict = None, proxy: dict = None):
for relay in self.relays.values(): for relay in self.relays.values():
if relay.url not in self.threads:
self.threads[relay.url] = threading.Thread( self.threads[relay.url] = threading.Thread(
target=relay.connect, target=relay.connect,
args=(ssl_options, proxy), args=(ssl_options, proxy),
name=f"{relay.url}-thread", name=f"{relay.url}-thread",
daemon=True, daemon=True,
) )
self.threads[relay.url].start() self.threads[relay.url].start()
if relay.url not in self.queue_threads:
self.queue_threads[relay.url] = threading.Thread( self.queue_threads[relay.url] = threading.Thread(
target=relay.queue_worker, target=relay.queue_worker,
args=(lambda: relay.shutdown,),
name=f"{relay.url}-queue", name=f"{relay.url}-queue",
daemon=True, daemon=True,
) )