Skip to content

Commit

Permalink
remove restart logic from main, reconnect instead of restarting the c…
Browse files Browse the repository at this point in the history
…lient
  • Loading branch information
f321x committed Oct 3, 2024
1 parent 1957a7a commit 212ed88
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
27 changes: 10 additions & 17 deletions coordinator/src/escrow_coordinator/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -81,15 +80,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);
Expand Down Expand Up @@ -137,16 +134,12 @@ impl EscrowCoordinator {
Ok((trade_hash, contract))
}

pub async fn restart_nostr_client(
mut self,
keys: Keys,
relays: Vec<String>,
) -> anyhow::Result<Self> {
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(())
}
}
9 changes: 1 addition & 8 deletions coordinator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 212ed88

Please sign in to comment.