Expand description
Time-decayed download rate and ETA estimation.
This is the single owner of all download speed / ETA math. The download
manager owns one RateEstimator per shard group and ships the values it
produces on the wire; every renderer (CLI progress bars, Tauri GUI, web UI)
displays those values verbatim. Renderers must never re-derive a rate from
byte deltas — doing so is what let the CLI and the GUI disagree with each
other, and with the operating system’s own network monitor.
§Why not an exponentially weighted average of instantaneous rates?
Progress arrives in bursts. On the hf-xet fast path the byte counter comes
from stating the partially-written file, and the chunk cache flushes to
disk in large steps even while the network rate is perfectly flat. Dividing
a burst by the short interval it landed in yields an enormous instantaneous
rate, and weighting that into a running average still leaks a visible spike.
Instead this decays bytes and elapsed time separately and reports their ratio:
decay = exp(-dt / TAU)
accum_bytes = accum_bytes * decay + delta_bytes
accum_time = accum_time * decay + dt
rate = accum_bytes / accum_timeA burst adds to the numerator and the denominator, so it contributes
exactly its own weight and can never spike. Intervals that carry no bytes
still add to accum_time, so a stall decays the reported rate toward zero
rather than freezing it at the last value seen.
Two further properties matter for how the manager drives this:
- The first sample only establishes a baseline and yields no rate. A resumed download reports its whole on-disk size in the first event; counting that as bytes transferred “just now” is what produced multi-GB/s readings.
- A byte count that moves backwards re-baselines instead of underflowing. Per-shard counters restart at zero on every shard, so the manager feeds aggregate bytes; this is the safety net for the fallback path where shard sizes are unknown and the aggregate is not monotonic.
Structs§
- Rate
Estimator - Time-decayed estimate of download throughput and time remaining.
Constants§
- ETA_TAU 🔒
- Time constant for the ETA average.
- RATE_
TAU 🔒 - Time constant for the rate average.
- WARMUP_
SECS 🔒 - Minimum accumulated observation time before any rate is reported.