use std::time::{Duration, Instant};
use crate::backoff::BackoffPolicy;
use crate::engine::{PassOutcome, PassReport, SyncEngine};
use crate::ledger::ConsentRequest;
use crate::traits::RecordMapper;
#[derive(Debug, Clone, PartialEq)]
pub struct SyncBudget {
pub max_passes: u32,
pub max_elapsed: Duration,
pub max_inline_delay_secs: f64,
}
impl Default for SyncBudget {
fn default() -> Self {
Self {
max_passes: 1,
max_elapsed: Duration::from_secs(60),
max_inline_delay_secs: 0.0,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SchedulePolicy {
pub backoff: BackoffPolicy,
pub idle_secs: f64,
pub min_secs: f64,
pub cancelled_secs: f64,
}
impl Default for SchedulePolicy {
fn default() -> Self {
Self {
backoff: BackoffPolicy::default(),
idle_secs: 900.0,
min_secs: 15.0,
cancelled_secs: 30.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkOutcome {
Success,
Retry,
Failure,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SyncReport {
pub passes: u32,
pub outcome: PassOutcome,
pub uploaded: usize,
pub downloaded: usize,
pub tombstones_applied: usize,
pub tombstones_written: usize,
pub quarantined: usize,
pub elapsed_ms: u64,
pub work: WorkOutcome,
pub next_delay_secs: Option<f64>,
pub consent_required: Option<ConsentRequest>,
}
pub fn sync_once<M: RecordMapper + 'static>(
engine: &mut SyncEngine<M>,
budget: &SyncBudget,
policy: &SchedulePolicy,
) -> SyncReport {
let started = Instant::now();
let mut passes = 0u32;
let mut total = PassReport {
outcome: PassOutcome::Synced,
uploaded: 0,
downloaded: 0,
tombstones_applied: 0,
tombstones_written: 0,
quarantined: 0,
blocked_reuploads: 0,
retry_after_secs: None,
};
let max_passes = budget.max_passes.max(1);
loop {
let rep = engine.run_pass();
passes += 1;
total.uploaded += rep.uploaded;
total.downloaded += rep.downloaded;
total.tombstones_applied += rep.tombstones_applied;
total.tombstones_written += rep.tombstones_written;
total.quarantined += rep.quarantined;
total.blocked_reuploads += rep.blocked_reuploads;
let progress =
rep.uploaded + rep.downloaded + rep.tombstones_applied + rep.tombstones_written;
total.outcome = rep.outcome;
total.retry_after_secs = rep.retry_after_secs;
if !should_continue(
&total.outcome,
total.retry_after_secs,
progress,
passes,
max_passes,
started.elapsed(),
budget,
) {
break;
}
}
let failures = engine.ledger().consecutive_failures;
let consent_required = match &total.outcome {
PassOutcome::ConsentRequired(req) => Some(req.clone()),
_ => None,
};
SyncReport {
passes,
work: work_outcome(&total.outcome),
next_delay_secs: next_delay(&total.outcome, total.retry_after_secs, failures, policy),
outcome: total.outcome,
uploaded: total.uploaded,
downloaded: total.downloaded,
tombstones_applied: total.tombstones_applied,
tombstones_written: total.tombstones_written,
quarantined: total.quarantined,
elapsed_ms: started.elapsed().as_millis() as u64,
consent_required,
}
}
pub fn should_continue(
outcome: &PassOutcome,
retry_after_secs: Option<f64>,
progress: usize,
passes_run: u32,
max_passes: u32,
elapsed: Duration,
budget: &SyncBudget,
) -> bool {
if passes_run >= max_passes || elapsed >= budget.max_elapsed {
return false;
}
if !matches!(outcome, PassOutcome::Pending(_)) || progress == 0 {
return false;
}
match retry_after_secs {
Some(s) => s.is_finite() && s <= budget.max_inline_delay_secs,
None => true,
}
}
pub fn work_outcome(outcome: &PassOutcome) -> WorkOutcome {
match outcome {
PassOutcome::Synced => WorkOutcome::Success,
PassOutcome::Pending(_)
| PassOutcome::Deferred { .. }
| PassOutcome::Unavailable(_)
| PassOutcome::Cancelled
| PassOutcome::Failed(_) => WorkOutcome::Retry,
PassOutcome::Disabled | PassOutcome::ConsentRequired(_) => WorkOutcome::Failure,
}
}
pub fn next_delay(
outcome: &PassOutcome,
retry_after_secs: Option<f64>,
failures: u32,
policy: &SchedulePolicy,
) -> Option<f64> {
let engine = retry_after_secs.filter(|s| s.is_finite() && *s > 0.0);
let secs = match outcome {
PassOutcome::Disabled | PassOutcome::ConsentRequired(_) => return None,
PassOutcome::Synced => policy.idle_secs,
PassOutcome::Pending(_) => engine.unwrap_or(policy.backoff.pending_floor_secs),
PassOutcome::Deferred { secs } => {
if secs.is_finite() && *secs > 0.0 {
*secs
} else {
policy.min_secs
}
}
PassOutcome::Cancelled => engine.unwrap_or(policy.cancelled_secs),
PassOutcome::Unavailable(_) | PassOutcome::Failed(_) => {
engine.unwrap_or_else(|| policy.backoff.delay_secs(failures.max(1), None))
}
};
Some(secs.max(policy.min_secs))
}
#[cfg(test)]
mod tests {
use super::*;
fn p() -> SchedulePolicy {
SchedulePolicy::default()
}
#[test]
fn synced_waits_the_idle_period() {
assert_eq!(next_delay(&PassOutcome::Synced, None, 0, &p()), Some(900.0));
}
#[test]
fn disabled_and_consent_schedule_nothing() {
assert_eq!(next_delay(&PassOutcome::Disabled, None, 0, &p()), None);
let req = ConsentRequest {
from: "a".into(),
to: "b".into(),
};
assert_eq!(
next_delay(&PassOutcome::ConsentRequired(req.clone()), None, 0, &p()),
None
);
assert_eq!(work_outcome(&PassOutcome::Disabled), WorkOutcome::Failure);
assert_eq!(
work_outcome(&PassOutcome::ConsentRequired(req)),
WorkOutcome::Failure
);
}
#[test]
fn pending_uses_the_engine_figure_else_the_floor() {
let o = PassOutcome::Pending("x".into());
assert_eq!(next_delay(&o, Some(45.0), 0, &p()), Some(45.0));
assert_eq!(next_delay(&o, None, 0, &p()), Some(30.0));
assert_eq!(work_outcome(&o), WorkOutcome::Retry);
}
#[test]
fn deferred_keeps_the_remaining_window_and_never_goes_below_the_minimum() {
assert_eq!(
next_delay(&PassOutcome::Deferred { secs: 240.0 }, Some(240.0), 3, &p()),
Some(240.0)
);
assert_eq!(
next_delay(&PassOutcome::Deferred { secs: 0.4 }, Some(0.4), 3, &p()),
Some(15.0)
);
assert_eq!(
next_delay(&PassOutcome::Deferred { secs: f64::NAN }, None, 0, &p()),
Some(15.0)
);
}
#[test]
fn failures_prefer_the_engine_figure_and_fall_back_to_the_exponential() {
let f = PassOutcome::Failed("e".into());
assert_eq!(next_delay(&f, Some(600.0), 2, &p()), Some(600.0));
assert_eq!(next_delay(&f, None, 1, &p()), Some(15.0));
assert_eq!(next_delay(&f, None, 2, &p()), Some(30.0));
assert_eq!(next_delay(&f, None, 4, &p()), Some(120.0));
assert_eq!(next_delay(&f, None, 50, &p()), Some(900.0));
assert_eq!(next_delay(&f, None, 0, &p()), Some(15.0));
let u = PassOutcome::Unavailable("signed out".into());
assert_eq!(next_delay(&u, None, 3, &p()), Some(60.0));
assert_eq!(work_outcome(&u), WorkOutcome::Retry);
}
#[test]
fn non_finite_or_non_positive_engine_figures_are_ignored() {
let f = PassOutcome::Failed("e".into());
assert_eq!(next_delay(&f, Some(f64::INFINITY), 2, &p()), Some(30.0));
assert_eq!(next_delay(&f, Some(-5.0), 2, &p()), Some(30.0));
assert_eq!(next_delay(&f, Some(0.0), 2, &p()), Some(30.0));
}
#[test]
fn cancelled_retries_soon() {
assert_eq!(
next_delay(&PassOutcome::Cancelled, None, 0, &p()),
Some(30.0)
);
assert_eq!(work_outcome(&PassOutcome::Cancelled), WorkOutcome::Retry);
assert_eq!(work_outcome(&PassOutcome::Synced), WorkOutcome::Success);
}
#[test]
fn a_larger_minimum_lifts_every_delay() {
let mut pol = p();
pol.min_secs = 60.0;
assert_eq!(
next_delay(&PassOutcome::Pending("x".into()), Some(30.0), 0, &pol),
Some(60.0)
);
assert_eq!(
next_delay(&PassOutcome::Failed("e".into()), None, 1, &pol),
Some(60.0)
);
}
fn cont(
outcome: &PassOutcome,
retry: Option<f64>,
progress: usize,
passes: u32,
budget: &SyncBudget,
elapsed: Duration,
) -> bool {
should_continue(
outcome,
retry,
progress,
passes,
budget.max_passes,
elapsed,
budget,
)
}
#[test]
fn the_default_budget_is_one_pass() {
let b = SyncBudget::default();
let pending = PassOutcome::Pending("x".into());
assert!(!cont(&pending, Some(30.0), 5, 1, &b, Duration::ZERO));
}
#[test]
fn a_drain_loop_needs_pending_progress_a_short_wait_and_budget_left() {
let b = SyncBudget {
max_passes: 3,
max_elapsed: Duration::from_secs(10),
max_inline_delay_secs: 1.0,
};
let pending = PassOutcome::Pending("x".into());
assert!(cont(&pending, Some(0.5), 2, 1, &b, Duration::from_secs(1)));
assert!(cont(&pending, None, 2, 1, &b, Duration::from_secs(1)));
assert!(!cont(
&pending,
Some(30.0),
2,
1,
&b,
Duration::from_secs(1)
));
assert!(!cont(&pending, Some(0.5), 0, 1, &b, Duration::from_secs(1)));
assert!(!cont(&pending, Some(0.5), 2, 3, &b, Duration::from_secs(1)));
assert!(!cont(
&pending,
Some(0.5),
2,
1,
&b,
Duration::from_secs(10)
));
assert!(!cont(&PassOutcome::Synced, None, 2, 1, &b, Duration::ZERO));
assert!(!cont(
&PassOutcome::Failed("e".into()),
Some(0.5),
2,
1,
&b,
Duration::ZERO
));
assert!(!cont(
&PassOutcome::Deferred { secs: 0.5 },
Some(0.5),
2,
1,
&b,
Duration::ZERO
));
}
}