use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, AtomicU64, Ordering};
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PushPhase {
#[default]
Scanning,
Hashing,
Connecting,
Negotiating,
Uploading,
Committing,
}
impl PushPhase {
fn as_u8(self) -> u8 {
match self {
PushPhase::Scanning => 0,
PushPhase::Hashing => 1,
PushPhase::Negotiating => 2,
PushPhase::Uploading => 3,
PushPhase::Committing => 4,
PushPhase::Connecting => 5,
}
}
fn from_u8(byte: u8) -> PushPhase {
match byte {
1 => PushPhase::Hashing,
2 => PushPhase::Negotiating,
3 => PushPhase::Uploading,
4 => PushPhase::Committing,
5 => PushPhase::Connecting,
_ => PushPhase::Scanning,
}
}
}
pub type PushProgressFn = Arc<dyn Fn(&PushProgressSnapshot) + Send + Sync>;
const HOOK_BYTE_STEP: u64 = 102_400;
#[derive(Debug, Clone, Default)]
pub struct PushProgress {
inner: Arc<ProgressCounters>,
}
impl PushProgress {
pub fn new() -> PushProgress {
PushProgress::default()
}
pub fn with_hook(hook: PushProgressFn) -> PushProgress {
PushProgress {
inner: Arc::new(ProgressCounters {
hook: Some(hook),
..ProgressCounters::default()
}),
}
}
pub fn snapshot(&self) -> PushProgressSnapshot {
self.inner.snapshot()
}
pub(crate) fn set_phase(&self, phase: PushPhase) {
self.inner.set_phase(phase);
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PushProgressSnapshot {
pub phase: PushPhase,
pub objects_total: u64,
pub objects_needed: u64,
pub objects_sent: u64,
pub bytes_sent: u64,
pub payload_bytes: u64,
pub content_bytes: u64,
pub bytes_total: u64,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct PushStats {
pub objects_total: u64,
pub objects_needed: u64,
pub objects_sent: u64,
pub bytes_sent: u64,
pub payload_bytes: u64,
pub elapsed: Duration,
}
#[derive(Default)]
struct ProgressCounters {
phase: AtomicU8,
objects_total: AtomicU64,
objects_needed: AtomicU64,
objects_sent: AtomicU64,
bytes_sent: AtomicU64,
payload_bytes: AtomicU64,
content_bytes: AtomicU64,
bytes_total: AtomicU64,
hook: Option<PushProgressFn>,
}
impl fmt::Debug for ProgressCounters {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProgressCounters")
.field("counters", &self.snapshot())
.field("hook", &self.hook.is_some())
.finish()
}
}
impl ProgressCounters {
fn snapshot(&self) -> PushProgressSnapshot {
PushProgressSnapshot {
phase: PushPhase::from_u8(self.phase.load(Ordering::Relaxed)),
objects_total: self.objects_total.load(Ordering::Relaxed),
objects_needed: self.objects_needed.load(Ordering::Relaxed),
objects_sent: self.objects_sent.load(Ordering::Relaxed),
bytes_sent: self.bytes_sent.load(Ordering::Relaxed),
payload_bytes: self.payload_bytes.load(Ordering::Relaxed),
content_bytes: self.content_bytes.load(Ordering::Relaxed),
bytes_total: self.bytes_total.load(Ordering::Relaxed),
}
}
fn call_hook(&self) {
if let Some(hook) = &self.hook {
hook(&self.snapshot());
}
}
fn set_phase(&self, phase: PushPhase) {
let before = self.phase.swap(phase.as_u8(), Ordering::Relaxed);
if before != phase.as_u8() {
self.call_hook();
}
}
fn object_sent(&self) {
self.objects_sent.fetch_add(1, Ordering::Relaxed);
self.call_hook();
}
fn content(&self, n: u64) {
let before = self.content_bytes.fetch_add(n, Ordering::Relaxed);
if before / HOOK_BYTE_STEP != before.wrapping_add(n) / HOOK_BYTE_STEP {
self.call_hook();
}
}
}
#[derive(Debug)]
pub(crate) struct Counters {
own: ProgressCounters,
caller: Option<Arc<ProgressCounters>>,
start: Instant,
}
impl Counters {
pub(crate) fn new(caller: Option<&PushProgress>) -> Counters {
Counters {
own: ProgressCounters::default(),
caller: caller.map(|progress| Arc::clone(&progress.inner)),
start: Instant::now(),
}
}
fn each(&self, f: impl Fn(&ProgressCounters)) {
f(&self.own);
if let Some(caller) = &self.caller {
f(caller);
}
}
pub(crate) fn phase(&self, phase: PushPhase) {
self.each(|c| c.set_phase(phase));
}
pub(crate) fn offered(&self, n: u64) {
self.each(|c| {
c.objects_total.fetch_add(n, Ordering::Relaxed);
});
}
pub(crate) fn needed(&self, n: u64) {
self.each(|c| {
c.objects_needed.fetch_add(n, Ordering::Relaxed);
});
}
pub(crate) fn object_sent(&self) {
self.each(ProgressCounters::object_sent);
}
pub(crate) fn content(&self, n: u64) {
self.each(|c| c.content(n));
}
pub(crate) fn content_total(&self, n: u64) {
self.each(|c| {
c.bytes_total.fetch_add(n, Ordering::Relaxed);
});
}
pub(crate) fn has_caller(&self) -> bool {
self.caller.is_some()
}
pub(crate) fn wire(&self, n: u64) {
self.each(|c| {
c.bytes_sent.fetch_add(n, Ordering::Relaxed);
});
}
pub(crate) fn payload(&self, n: u64) {
self.each(|c| {
c.payload_bytes.fetch_add(n, Ordering::Relaxed);
});
}
pub(crate) fn stats(&self) -> PushStats {
let s = self.own.snapshot();
PushStats {
objects_total: s.objects_total,
objects_needed: s.objects_needed,
objects_sent: s.objects_sent,
bytes_sent: s.bytes_sent,
payload_bytes: s.payload_bytes,
elapsed: self.start.elapsed(),
}
}
}