Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ cidre = { git = "https://github.com/CapSoftware/cidre", rev = "bf84b67079a8" }

# https://github.com/CapSoftware/posthog-rs/commit/c7e9712be2f9a9122b1df685d5a067afa5415288
posthog-rs = { git = "https://github.com/CapSoftware/posthog-rs", rev = "c7e9712be2f9a9122b1df685d5a067afa5415288" }

# https://github.com/CapSoftware/reqwest/commit/9b5ecbd5210a9510fde766015cabb724c1e70d2e
reqwest = { git = "https://github.com/CapSoftware/reqwest", rev = "9b5ecbd5210a9510fde766015cabb724c1e70d2e" }
2 changes: 1 addition & 1 deletion apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ rodio = "0.19.0"
png = "0.17.13"
device_query = "4.0.1"
base64 = "0.22.1"
reqwest = { version = "0.12.7", features = ["json", "stream", "multipart"] }
reqwest = { version = "0.12.24", features = ["json", "stream", "multipart"] }
dotenvy_macro = "0.15.7"
global-hotkey = "0.7.0"
rand = "0.8.5"
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,7 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) {
app.manage(EditorWindowIds::default());
#[cfg(target_os = "macos")]
app.manage(crate::platform::ScreenCapturePrewarmer::default());
app.manage(upload::RetryableClient::default());

tokio::spawn({
let camera_feed = camera_feed.clone();
Expand Down
88 changes: 51 additions & 37 deletions apps/desktop/src-tauri/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
web_api::{AuthedApiError, ManagerExt},
};
use async_stream::{stream, try_stream};
use axum::http::Uri;
use bytes::Bytes;
use cap_project::{RecordingMeta, S3UploadMeta, UploadMeta};
use cap_utils::spawn_actor;
Expand All @@ -22,13 +21,13 @@ use specta::Type;
use std::{
collections::HashMap,
io,
ops::Deref,
path::{Path, PathBuf},
pin::pin,
str::FromStr,
sync::{Arc, Mutex, PoisonError},
time::Duration,
};
use tauri::{AppHandle, ipc::Channel};
use tauri::{AppHandle, Manager, ipc::Channel};
use tauri_plugin_clipboard_manager::ClipboardExt;
use tauri_specta::Event;
use tokio::{
Expand Down Expand Up @@ -596,23 +595,42 @@ pub fn from_pending_file_to_chunks(
.instrument(Span::current())
}

fn retryable_client(host: String) -> reqwest::ClientBuilder {
reqwest::Client::builder().retry(
reqwest::retry::for_host(host)
.classify_fn(|req_rep| {
match req_rep.status() {
// Server errors
Some(s) if s.is_server_error() || s == StatusCode::TOO_MANY_REQUESTS => {
req_rep.retryable()
}
// Network errors
None => req_rep.retryable(),
_ => req_rep.success(),
}
})
.max_retries_per_request(5)
.max_extra_load(5.0),
)
pub struct RetryableClient(reqwest::Result<reqwest::Client>);

impl Default for RetryableClient {
fn default() -> Self {
Self(
reqwest::Client::builder()
.retry(
reqwest::retry::always()
.classify_fn(|req_rep| {
match req_rep.status() {
// Server errors
Some(s)
if s.is_server_error()
|| s == StatusCode::TOO_MANY_REQUESTS =>
{
req_rep.retryable()
}
// Network errors
None => req_rep.retryable(),
_ => req_rep.success(),
}
})
.max_retries_per_request(5)
.max_extra_load(5.0),
)
.build(),
)
}
}

impl Deref for RetryableClient {
type Target = reqwest::Result<reqwest::Client>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

/// Takes an incoming stream of bytes and individually uploads them to S3.
Expand Down Expand Up @@ -726,19 +744,16 @@ fn multipart_uploader(
}

let size = chunk.len();
let url = Uri::from_str(&presigned_url).map_err(|err| {
format!("uploader/part/{part_number}/invalid_url: {err:?}")
})?;
let mut req =
retryable_client(url.host().unwrap_or("<unknown>").to_string())
.build()
.map_err(|err| {
format!("uploader/part/{part_number}/client: {err:?}")
})?
.put(&presigned_url)
.header("Content-Length", chunk.len())
.timeout(Duration::from_secs(5 * 60))
.body(chunk);
let mut req = app
.state::<RetryableClient>()
.as_ref()
.map_err(|err| {
format!("uploader/part/{part_number}/client: {err:?}")
})?
.put(&presigned_url)
.header("Content-Length", chunk.len())
.timeout(Duration::from_secs(5 * 60))
.body(chunk);

Comment thread
oscartbeaumont marked this conversation as resolved.
if let Some(md5_sum) = &md5_sum {
req = req.header("Content-MD5", md5_sum);
Expand Down Expand Up @@ -811,10 +826,9 @@ pub async fn singlepart_uploader(
) -> Result<(), AuthedApiError> {
let presigned_url = api::upload_signed(&app, request).await?;

let url = Uri::from_str(&presigned_url)
.map_err(|err| format!("singlepart_uploader/invalid_url: {err:?}"))?;
let resp = retryable_client(url.host().unwrap_or("<unknown>").to_string())
.build()
let resp = app
.state::<RetryableClient>()
.as_ref()
.map_err(|err| format!("singlepart_uploader/client: {err:?}"))?
.put(&presigned_url)
.header("Content-Length", total_size)
Expand Down
2 changes: 1 addition & 1 deletion crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2024"
publish = false

[dependencies]
reqwest = "0.12.23"
reqwest = "0.12.24"

[lints]
workspace = true
Loading