Skip to content

Commit d324706

Browse files
committed
f Use tokio::sync::broadcast channel
1 parent 34ca17e commit d324706

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/wallet.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
3333
use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey, Signing};
3434
use bitcoin::{ScriptBuf, Transaction, TxOut, Txid};
3535

36-
use std::mem;
3736
use std::ops::{Deref, DerefMut};
3837
use std::sync::{Arc, Mutex, RwLock};
3938
use std::time::Duration;
4039

4140
enum WalletSyncStatus {
4241
Completed,
43-
InProgress { subscribers: Vec<tokio::sync::oneshot::Sender<Result<(), Error>>> },
42+
InProgress { subscribers: tokio::sync::broadcast::Sender<Result<(), Error>> },
4443
}
4544

4645
pub struct Wallet<D, B: Deref, E: Deref, L: Deref>
@@ -89,9 +88,9 @@ where
8988
}
9089

9190
pub(crate) async fn sync(&self) -> Result<(), Error> {
92-
if let Some(sync_receiver) = self.register_or_subscribe_pending_sync() {
91+
if let Some(mut sync_receiver) = self.register_or_subscribe_pending_sync() {
9392
log_info!(self.logger, "Sync in progress, skipping.");
94-
return sync_receiver.await.map_err(|e| {
93+
return sync_receiver.recv().await.map_err(|e| {
9594
debug_assert!(false, "Failed to receive wallet sync result: {:?}", e);
9695
log_error!(self.logger, "Failed to receive wallet sync result: {:?}", e);
9796
Error::WalletOperationFailed
@@ -310,26 +309,25 @@ where
310309

311310
fn register_or_subscribe_pending_sync(
312311
&self,
313-
) -> Option<tokio::sync::oneshot::Receiver<Result<(), Error>>> {
312+
) -> Option<tokio::sync::broadcast::Receiver<Result<(), Error>>> {
314313
let mut sync_status_lock = self.sync_status.lock().unwrap();
315314
match sync_status_lock.deref_mut() {
316315
WalletSyncStatus::Completed => {
317316
// We're first to register for a sync.
318-
*sync_status_lock = WalletSyncStatus::InProgress { subscribers: Vec::new() };
317+
let (tx, _) = tokio::sync::broadcast::channel(1);
318+
*sync_status_lock = WalletSyncStatus::InProgress { subscribers: tx };
319319
None
320320
},
321321
WalletSyncStatus::InProgress { subscribers } => {
322322
// A sync is in-progress, we subscribe.
323-
let (tx, rx) = tokio::sync::oneshot::channel();
324-
subscribers.push(tx);
323+
let rx = subscribers.subscribe();
325324
Some(rx)
326325
},
327326
}
328327
}
329328

330329
fn propagate_result_to_subscribers(&self, res: Result<(), Error>) {
331330
// Send the notification to any other tasks that might be waiting on it by now.
332-
let mut waiting_subscribers = Vec::new();
333331
{
334332
let mut sync_status_lock = self.sync_status.lock().unwrap();
335333
match sync_status_lock.deref_mut() {
@@ -339,22 +337,27 @@ where
339337
},
340338
WalletSyncStatus::InProgress { subscribers } => {
341339
// A sync is in-progress, we notify subscribers.
342-
mem::swap(&mut waiting_subscribers, subscribers);
340+
if subscribers.receiver_count() > 0 {
341+
match subscribers.send(res) {
342+
Ok(_) => (),
343+
Err(e) => {
344+
debug_assert!(
345+
false,
346+
"Failed to send wallet sync result to subscribers: {:?}",
347+
e
348+
);
349+
log_error!(
350+
self.logger,
351+
"Failed to send wallet sync result to subscribers: {:?}",
352+
e
353+
);
354+
},
355+
}
356+
}
343357
*sync_status_lock = WalletSyncStatus::Completed;
344358
},
345359
}
346360
}
347-
348-
for sender in waiting_subscribers {
349-
sender.send(res).unwrap_or_else(|e| {
350-
debug_assert!(false, "Failed to send wallet sync result to subscribers: {:?}", e);
351-
log_error!(
352-
self.logger,
353-
"Failed to send wallet sync result to subscribers: {:?}",
354-
e
355-
);
356-
});
357-
}
358361
}
359362
}
360363

0 commit comments

Comments
 (0)