From 2d029982691698ea0e0fe379ee31f7ce70840218 Mon Sep 17 00:00:00 2001 From: f321x Date: Thu, 3 Oct 2024 23:49:38 +0200 Subject: [PATCH] remove restart logic from main, reconnect instead of restarting the client --- coordinator/src/escrow_coordinator/mod.rs | 27 +++++++++-------------- coordinator/src/main.rs | 9 +------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/coordinator/src/escrow_coordinator/mod.rs b/coordinator/src/escrow_coordinator/mod.rs index 40d5ec1..6acbcf6 100644 --- a/coordinator/src/escrow_coordinator/mod.rs +++ b/coordinator/src/escrow_coordinator/mod.rs @@ -1,5 +1,4 @@ use super::*; -use anyhow::anyhow; use cashu_escrow_common::model::TradeContract; use cdk::nuts::SecretKey as CDKSecretKey; use hashes::hex::DisplayHex; @@ -79,15 +78,13 @@ impl EscrowCoordinator { } } } else if RelayPoolNotification::Shutdown == notification { - break Err(anyhow!( - "Got shutdown notification, breaking coordinator loop!" - )); + error!("Got shutdown notification, restarting nostr client!"); + self.reconnect_nostr_client().await?; } } Err(RecvError::Closed) => { - break Err(anyhow!( - "Got closed error from channel, breaking coordinator loop!" - )) + error!("Got closed error from channel, restarting nostr client..."); + self.reconnect_nostr_client().await?; } Err(RecvError::Lagged(count)) => { warn!("Lost {} events, resuming after that...", count); @@ -135,16 +132,12 @@ impl EscrowCoordinator { Ok((trade_hash, contract)) } - pub async fn restart_nostr_client( - mut self, - keys: Keys, - relays: Vec, - ) -> anyhow::Result { - warn!("Restarting nostr client..."); - self.nostr_client.client.shutdown().await?; + async fn reconnect_nostr_client(&self) -> anyhow::Result<()> { + self.nostr_client.client.disconnect().await?; + warn!("Reconnecting nostr client in 60 seconds..."); tokio::time::sleep(std::time::Duration::from_secs(60)).await; - self.nostr_client = NostrClient::new(keys, relays).await?; - info!("Nostr client restarted!"); - Ok(self) + self.nostr_client.client.connect().await; + info!("Nostr client reconnected sucessfully!"); + Ok(()) } } diff --git a/coordinator/src/main.rs b/coordinator/src/main.rs index 439ec90..8b84543 100644 --- a/coordinator/src/main.rs +++ b/coordinator/src/main.rs @@ -28,12 +28,5 @@ async fn main() -> anyhow::Result<()> { nostr_client.public_key().to_bech32()? ); info!("Starting service and waiting for trades..."); - let mut coordinator = EscrowCoordinator::new(nostr_client)?; - while let Err(err) = coordinator.run().await { - error!("Coordinator loop exited with error: {:?}", err); - coordinator = coordinator - .restart_nostr_client(keys.clone(), relays.clone()) - .await?; - } - Ok(()) + return EscrowCoordinator::new(nostr_client)?.run().await; }