Added poll_balance_with_timeout method (#1062)

* Added poll_balance_with_timeout method

- updated bench-tps, fullnode and wallet to use this method instead
  of repeatedly calling poll_get_balance()

* Address review comments

- Revert some changes to use wrapper poll_get_balance()

* Reverting bench-tps to use poll_get_balance

- The original code is checking if the balance has been updated,
  instead of just retrieving the balance. The logic is different
  than poll_balance_with_timeout()

* Reverting wallet to use poll_get_balance

- The break condition in the loop is different than poll_balance_with_timeout().
  It's checking if the balance has been updated.
This commit is contained in:
Pankaj Garg
2018-08-25 18:24:25 -07:00
committed by anatoly yakovenko
parent ad159e0906
commit 50661e7b8d
3 changed files with 47 additions and 32 deletions

52
src/thin_client.rs Executable file → Normal file
View File

@@ -242,37 +242,49 @@ impl ThinClient {
self.last_id.expect("some last_id")
}
pub fn poll_get_balance(&mut self, pubkey: &Pubkey) -> io::Result<i64> {
let mut balance_result;
let mut balance_value = -1;
let now = Instant::now();
loop {
balance_result = self.get_balance(pubkey);
if balance_result.is_ok() {
balance_value = *balance_result.as_ref().unwrap();
}
if balance_value > 0 || now.elapsed().as_secs() > 1 {
break;
}
sleep(Duration::from_millis(100));
}
pub fn submit_poll_balance_metrics(elapsed: &Duration) {
metrics::submit(
influxdb::Point::new("thinclient")
.add_tag("op", influxdb::Value::String("get_balance".to_string()))
.add_field(
"duration_ms",
influxdb::Value::Integer(timing::duration_as_ms(&now.elapsed()) as i64),
influxdb::Value::Integer(timing::duration_as_ms(elapsed) as i64),
)
.to_owned(),
);
if balance_value >= 0 {
Ok(balance_value)
} else {
assert!(balance_result.is_err());
balance_result
}
pub fn poll_balance_with_timeout(
&mut self,
pubkey: &Pubkey,
polling_frequency: &Duration,
timeout: &Duration,
) -> io::Result<i64> {
let now = Instant::now();
loop {
let balance = match self.get_balance(&pubkey) {
Ok(bal) => bal,
Err(e) => {
sleep(*polling_frequency);
if now.elapsed() > *timeout {
ThinClient::submit_poll_balance_metrics(&now.elapsed());
return Err(e);
}
-1
}
};
if balance >= 0 {
ThinClient::submit_poll_balance_metrics(&now.elapsed());
return Ok(balance);
}
}
}
pub fn poll_get_balance(&mut self, pubkey: &Pubkey) -> io::Result<i64> {
self.poll_balance_with_timeout(pubkey, &Duration::from_millis(100), &Duration::from_secs(1))
}
/// Poll the server to confirm a transaction.
pub fn poll_for_signature(&mut self, signature: &Signature) -> io::Result<()> {
let now = Instant::now();