-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
review comments - balance enquiry debouncer
- Loading branch information
1 parent
d4f118a
commit 32e2399
Showing
7 changed files
with
193 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
applications/tari_console_wallet/src/ui/state/debouncer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2020. The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
use crate::ui::state::AppStateInner; | ||
use log::*; | ||
use std::{ | ||
sync::Arc, | ||
time::{Duration, Instant}, | ||
}; | ||
use tari_wallet::output_manager_service::handle::OutputManagerHandle; | ||
use tokio::{ | ||
sync::{broadcast, RwLock}, | ||
time, | ||
}; | ||
|
||
const LOG_TARGET: &str = "wallet::console_wallet::debouncer"; | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct BalanceEnquiryDebouncer { | ||
app_state_inner: Arc<RwLock<AppStateInner>>, | ||
output_manager_service: OutputManagerHandle, | ||
balance_enquiry_cooldown_period: Duration, | ||
tx: broadcast::Sender<()>, | ||
} | ||
|
||
impl BalanceEnquiryDebouncer { | ||
pub fn new( | ||
app_state_inner: Arc<RwLock<AppStateInner>>, | ||
balance_enquiry_cooldown_period: Duration, | ||
output_manager_service: OutputManagerHandle, | ||
) -> Self { | ||
// This channel must only be size 1; the debouncer will ensure that the balance is updated timeously | ||
let (tx, _) = broadcast::channel(1); | ||
Self { | ||
app_state_inner, | ||
output_manager_service, | ||
balance_enquiry_cooldown_period, | ||
tx, | ||
} | ||
} | ||
|
||
pub async fn run(mut self) { | ||
let balance_enquiry_events = &mut self.tx.subscribe(); | ||
let mut shutdown_signal = self.app_state_inner.read().await.get_shutdown_signal(); | ||
let delay = time::sleep(self.balance_enquiry_cooldown_period); | ||
tokio::pin!(delay); | ||
|
||
debug!(target: LOG_TARGET, "Balance enquiry debouncer starting"); | ||
if let Ok(balance) = self.output_manager_service.get_balance().await { | ||
trace!( | ||
target: LOG_TARGET, | ||
"Initial balance: available {}, incoming {}, outgoing {}", | ||
balance.available_balance, | ||
balance.pending_incoming_balance, | ||
balance.pending_outgoing_balance | ||
); | ||
let mut inner = self.app_state_inner.write().await; | ||
if let Err(e) = inner.refresh_balance(balance).await { | ||
warn!(target: LOG_TARGET, "Error refresh app_state: {}", e); | ||
} | ||
} | ||
loop { | ||
tokio::select! { | ||
_ = &mut delay => { | ||
if let Ok(result) = time::timeout( | ||
self.balance_enquiry_cooldown_period, | ||
balance_enquiry_events.recv() | ||
).await { | ||
match result { | ||
Ok(_) => { | ||
let start_time = Instant::now(); | ||
match self.output_manager_service.get_balance().await { | ||
Ok(balance) => { | ||
trace!( | ||
target: LOG_TARGET, | ||
"Updating balance ({} ms): available {}, incoming {}, outgoing {}", | ||
start_time.elapsed().as_millis(), | ||
balance.available_balance, | ||
balance.pending_incoming_balance, | ||
balance.pending_outgoing_balance | ||
); | ||
let mut inner = self.app_state_inner.write().await; | ||
if let Err(e) = inner.refresh_balance(balance).await { | ||
warn!(target: LOG_TARGET, "Error refresh app_state: {}", e); | ||
} | ||
} | ||
Err(e) => { | ||
warn!(target: LOG_TARGET, "Could not obtain balance ({})", e); | ||
} | ||
} | ||
} | ||
Err(broadcast::error::RecvError::Lagged(n)) => { | ||
trace!(target: LOG_TARGET, "Balance enquiry debouncer lagged {} update requests", n); | ||
continue; | ||
} | ||
Err(broadcast::error::RecvError::Closed) => { | ||
info!( | ||
target: LOG_TARGET, | ||
"Balance enquiry debouncer shutting down because the channel was closed" | ||
); | ||
break; | ||
} | ||
} | ||
} | ||
}, | ||
_ = shutdown_signal.wait() => { | ||
info!( | ||
target: LOG_TARGET, | ||
"Balance enquiry debouncer shutting down because the shutdown signal was received" | ||
); | ||
break; | ||
}, | ||
} | ||
} | ||
} | ||
|
||
pub fn get_sender(self) -> broadcast::Sender<()> { | ||
self.tx | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.