Skip to content

Commit de84f35

Browse files
committed
refactor: use metrics counter as an abstaction
1 parent a813ba0 commit de84f35

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use crate::core::GitReference;
55
use crate::util::errors::CargoResult;
6-
use crate::util::{network, Config, IntoUrl, Progress};
6+
use crate::util::{network, Config, IntoUrl, MetricsCounter, Progress};
77
use anyhow::{anyhow, Context as _};
88
use cargo_util::{paths, ProcessBuilder};
99
use curl::easy::List;
@@ -695,9 +695,9 @@ pub fn with_fetch_options(
695695
let mut progress = Progress::new("Fetch", config);
696696
network::with_retry(config, || {
697697
with_authentication(url, git_config, |f| {
698-
let mut last_recv = 0; // in Byte
699698
let mut last_update = Instant::now();
700699
let mut rcb = git2::RemoteCallbacks::new();
700+
let mut counter = MetricsCounter::<10>::new(0);
701701
rcb.credentials(f);
702702
rcb.transfer_progress(|stats| {
703703
let indexed_deltas = stats.indexed_deltas();
@@ -711,18 +711,16 @@ pub fn with_fetch_options(
711711
} else {
712712
// Receiving objects.
713713
let duration = last_update.elapsed();
714-
let recv = stats.received_bytes();
715-
let rate = (recv - last_recv) as f32 / duration.as_secs_f32();
716-
if duration > Duration::from_secs(3) {
717-
last_recv = recv;
714+
if duration > Duration::from_millis(300) {
715+
counter.add(stats.received_bytes());
718716
last_update = Instant::now();
719717
}
720718
fn format_bytes(bytes: f32) -> (&'static str, f32) {
721719
static UNITS: [&str; 5] = ["", "K", "M", "G", "T"];
722720
let i = (bytes.log2() / 10.0).min(4.0) as usize;
723721
(UNITS[i], bytes / 1024_f32.powi(i as i32))
724722
}
725-
let (unit, rate) = format_bytes(rate);
723+
let (unit, rate) = format_bytes(counter.rate());
726724
format!(", {:.2}{}iB/s", rate, unit)
727725
};
728726
progress

0 commit comments

Comments
 (0)