Skip to content

Commit

Permalink
pool: add SyncProgress::percentage
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Oct 24, 2024
1 parent c13dc14 commit 35fdb39
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions crates/nostr-relay-pool/src/relay/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@ impl SyncProgress {
pub fn channel() -> (Sender<Self>, Receiver<Self>) {
watch::channel(SyncProgress::default())
}

/// Calculate progress %
#[inline]
pub fn percentage(&self) -> f64 {
if self.total > 0 {
self.current as f64 / self.total as f64
} else {
0.0
}
}
}

/// Sync (negentropy reconciliation) options
Expand Down
6 changes: 3 additions & 3 deletions crates/nostr-sdk/examples/negentropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ async fn main() -> Result<()> {

tokio::spawn(async move {
while rx.changed().await.is_ok() {
let SyncProgress { total, current } = *rx.borrow_and_update();
if total > 0 {
println!("{:.2}%", (current as f64 / total as f64) * 100.0);
let progress = *rx.borrow_and_update();
if progress.total > 0 {
println!("{:.2}%", progress.percentage() * 100.0);
}
}
});
Expand Down

0 comments on commit 35fdb39

Please sign in to comment.