From 2eca2a0cc1b79b5218ed10804317ee71c6008b05 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Tue, 27 Jul 2021 16:30:42 +0200 Subject: [PATCH] Experimental: use Notify for Debtor credit flow --- src/actor.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index ae01486..d40f67d 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -27,6 +27,7 @@ use std::sync::Weak; use std::sync::atomic::{AtomicI64, AtomicU64, Ordering}; use tokio::select; +use tokio::sync::Notify; use tokio::sync::mpsc::{unbounded_channel, UnboundedSender, UnboundedReceiver}; use tokio_util::sync::CancellationToken; @@ -92,7 +93,7 @@ struct EventBuffer { pub struct Debtor { id: u64, debt: Arc, - // notify: Notify, + notify: Notify, } #[derive(Debug)] @@ -394,7 +395,7 @@ impl Debtor { Arc::new(Debtor { id, debt, - // notify: Notify::new(), + notify: Notify::new(), }) } @@ -410,17 +411,17 @@ impl Debtor { pub fn repay(&self, token_count: usize) { let token_count: i64 = token_count.try_into().expect("manageable token count"); let _old_debt = self.debt.fetch_sub(token_count, Ordering::Relaxed); - // if _old_debt - token_count <= *SYNDICATE_CREDIT { - // self.notify.notify_one(); - // } + if _old_debt - token_count <= *SYNDICATE_CREDIT { + self.notify.notify_one(); + } } pub async fn ensure_clear_funds(&self) { let limit = *SYNDICATE_CREDIT; - tokio::task::yield_now().await; + // tokio::task::yield_now().await; while self.balance() > limit { - tokio::task::yield_now().await; - // self.notify.notified().await; + // tokio::task::yield_now().await; + self.notify.notified().await; } } }