Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: resume preparation after isolated upload failures

Richie McIlroya9a628e6dd7a
diff --git a/apps/desktop-gpui/src/upload.rs b/apps/desktop-gpui/src/upload.rs
index e9666c14b..f186f10c9 100644
--- a/apps/desktop-gpui/src/upload.rs
+++ b/apps/desktop-gpui/src/upload.rs
@@ -638,6 +638,7 @@ async fn upload_segments(
let mut preparation_interval = tokio::time::interval(Duration::from_secs(30));
preparation_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut preparation_request = None;
+ let mut next_preparation_request = tokio::time::Instant::now();
let mut next_prefetch = SEGMENT_URL_PREFETCH + 1;
let prefetched = checked_segment_step(&cancel, || async {
Ok(transport.prefetch(video_id, 1, SEGMENT_URL_PREFETCH).await)
@@ -662,6 +663,7 @@ async fn upload_segments(
};
tokio::select! {
_ = preparation_interval.tick(), if preparation_enabled && preparation_request.is_none() => {
+ if tokio::time::Instant::now() < next_preparation_request { continue; }
preparation_batch = preparation.next_batch(manifest.video_segments.iter().map(|segment| segment.index), manifest.audio_segments.iter().map(|segment| segment.index));
if !preparation_batch.is_empty() {
preparation_request = Some(Box::pin(transport.prepare(video_id, preparation_batch.clone())));
@@ -672,9 +674,12 @@ async fn upload_segments(
match response {
Ok(Some(prepared)) => preparation.acknowledge(&preparation_batch, &prepared),
Ok(None) => preparation_enabled = false,
- Err(error) => tracing::debug!(%error, "Optional recording preparation unavailable"),
+ Err(error) => {
+ preparation.request_failed();
+ tracing::debug!(%error, "Optional recording preparation unavailable");
+ }
}
- if preparation.exhausted() { preparation_enabled = false; }
+ next_preparation_request = tokio::time::Instant::now() + preparation.retry_delay();
}
permission = async { permission.unwrap().await }, if !authorized => {
permission.map_err(|_| "Instant completion was not authorized".to_string())?;
diff --git a/apps/desktop/src-tauri/src/upload/preparation.rs b/apps/desktop/src-tauri/src/upload/preparation.rs
index ac75de52d..f8a5025a2 100644
--- a/apps/desktop/src-tauri/src/upload/preparation.rs
+++ b/apps/desktop/src-tauri/src/upload/preparation.rs
@@ -36,6 +36,7 @@ pub(super) fn start(
let context = session.context();
Task(tokio::spawn(inherit_upload_context(context, async move {
let mut preparation = Preparation::default();
+ let mut next_request = tokio::time::Instant::now();
let mut interval = tokio::time::interval(Duration::from_secs(30));
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
loop {
@@ -43,6 +44,9 @@ pub(super) fn start(
_ = session.cancelled() => return,
_ = interval.tick() => {}
}
+ if tokio::time::Instant::now() < next_request {
+ continue;
+ }
let batch = {
let state = state.lock().unwrap_or_else(|error| error.into_inner());
preparation.next_batch(
@@ -62,11 +66,12 @@ pub(super) fn start(
preparation.acknowledge(&batch, &prepared);
}
Ok(None) => return,
- Err(error) => tracing::debug!(%error, "Optional recording preparation unavailable"),
- }
- if preparation.exhausted() {
- return;
+ Err(error) => {
+ preparation.request_failed();
+ tracing::debug!(%error, "Optional recording preparation unavailable");
+ }
}
+ next_request = tokio::time::Instant::now() + preparation.retry_delay();
}
})))
}
diff --git a/crates/recording/src/upload_preparation.rs b/crates/recording/src/upload_preparation.rs
index d1a6e9714..bc74ec5bd 100644
--- a/crates/recording/src/upload_preparation.rs
+++ b/crates/recording/src/upload_preparation.rs
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
+use std::time::Duration;
#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "lowercase")]
@@ -18,6 +19,7 @@ pub struct Segment {
pub struct Preparation {
attempts: BTreeMap<Segment, u8>,
acknowledged: BTreeSet<Segment>,
+ consecutive_failures: u32,
}
impl Preparation {
@@ -51,18 +53,29 @@ impl Preparation {
}
pub fn acknowledge(&mut self, requested: &[Segment], prepared: &[Segment]) {
+ let before = self.acknowledged.len();
self.acknowledged.extend(
prepared
.iter()
.filter(|segment| requested.contains(segment))
.copied(),
);
+ if self.acknowledged.len() > before {
+ self.consecutive_failures = 0;
+ } else {
+ self.request_failed();
+ }
+ }
+
+ pub fn request_failed(&mut self) {
+ self.consecutive_failures = self.consecutive_failures.saturating_add(1);
}
- pub fn exhausted(&self) -> bool {
- self.attempts
- .iter()
- .any(|(segment, attempts)| *attempts >= 2 && !self.acknowledged.contains(segment))
+ pub fn retry_delay(&self) -> Duration {
+ if self.consecutive_failures == 0 {
+ return Duration::ZERO;
+ }
+ Duration::from_secs((30u64 << self.consecutive_failures.min(4)).min(300))
}
}
@@ -113,15 +126,42 @@ mod tests {
},
],
);
- assert!(!preparation.exhausted());
let retry = preparation.next_batch([1, 2, 99], [1]);
assert_eq!(retry.len(), 3);
- assert!(preparation.exhausted());
preparation.acknowledge(&retry, &retry);
- assert!(!preparation.exhausted());
assert!(preparation.next_batch([1, 2, 99], [1]).is_empty());
}
+ #[test]
+ fn an_exhausted_fragment_does_not_disable_later_uploads() {
+ let mut preparation = Preparation::default();
+ assert_eq!(preparation.next_batch([1], []).len(), 1);
+ assert_eq!(preparation.next_batch([1], []).len(), 1);
+ assert_eq!(
+ preparation.next_batch([1, 2], []),
+ vec![Segment {
+ track: Track::Video,
+ index: 2
+ }]
+ );
+ }
+
+ #[test]
+ fn backs_off_outages_and_resets_after_real_progress() {
+ let mut preparation = Preparation::default();
+ let batch = preparation.next_batch([1], []);
+ preparation.acknowledge(&batch, &[]);
+ assert_eq!(preparation.retry_delay(), Duration::from_secs(60));
+ preparation.request_failed();
+ assert_eq!(preparation.retry_delay(), Duration::from_secs(120));
+ for _ in 0..10 {
+ preparation.request_failed();
+ }
+ assert_eq!(preparation.retry_delay(), Duration::from_secs(300));
+ preparation.acknowledge(&batch, &batch);
+ assert_eq!(preparation.retry_delay(), Duration::ZERO);
+ }
+
#[test]
fn preserves_wire_format_without_initialization_fragments() {
let batch = Preparation::default().next_batch([0, 1], []);