WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions apps/desktop/src-tauri/src/captions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use ffmpeg::{
format::{self as avformat},
software::resampling,
};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::fs::File;
Expand All @@ -22,6 +21,8 @@ use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextPar
// Re-export caption types from cap_project
pub use cap_project::{CaptionSegment, CaptionSettings};

use crate::http_client;

// Convert the project type's float precision from f32 to f64 for compatibility
#[derive(Debug, Serialize, Deserialize, Type, Clone)]
pub struct CaptionData {
Expand Down Expand Up @@ -1051,6 +1052,7 @@ impl DownloadProgress {
#[specta::specta]
#[instrument(skip(window))]
pub async fn download_whisper_model(
app: AppHandle,
window: Window,
model_name: String,
output_path: String,
Expand All @@ -1067,8 +1069,8 @@ pub async fn download_whisper_model(
};

// Create the client and download the model
let client = Client::new();
let response = client
let response = app
.state::<http_client::HttpClient>()
.get(model_url)
.send()
.await
Expand Down
59 changes: 59 additions & 0 deletions apps/desktop/src-tauri/src/http_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! We reuse clients so we get connection pooling and also so the retry policy can handle backing off across requests.

use std::ops::Deref;

use reqwest::StatusCode;

pub struct HttpClient(reqwest::Client);

impl Default for HttpClient {
fn default() -> Self {
Self(reqwest::Client::new())
}
}

impl Deref for HttpClient {
type Target = reqwest::Client;

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

pub struct RetryableHttpClient(reqwest::Result<reqwest::Client>);

impl Default for RetryableHttpClient {
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 RetryableHttpClient {
type Target = reqwest::Result<reqwest::Client>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
3 changes: 3 additions & 0 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod flags;
mod frame_ws;
mod general_settings;
mod hotkeys;
mod http_client;
mod logging;
mod notifications;
mod permissions;
Expand Down Expand Up @@ -2193,6 +2194,8 @@ 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(http_client::HttpClient::default());
app.manage(http_client::RetryableHttpClient::default());

tokio::spawn({
let camera_feed = camera_feed.clone();
Expand Down
54 changes: 15 additions & 39 deletions apps/desktop/src-tauri/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use crate::{
UploadProgress, VideoUploadInfo,
api::{self, PresignedS3PutRequest, PresignedS3PutRequestMethod, S3VideoMeta, UploadedPart},
http_client::RetryableHttpClient,
posthog::{PostHogEvent, async_capture_event},
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 @@ -24,11 +24,10 @@ use std::{
io,
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,25 +595,6 @@ 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),
)
}

/// Takes an incoming stream of bytes and individually uploads them to S3.
///
/// Note: It's on the caller to ensure the chunks are sized correctly within S3 limits.
Expand Down Expand Up @@ -726,19 +706,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::<RetryableHttpClient>()
.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);

if let Some(md5_sum) = &md5_sum {
req = req.header("Content-MD5", md5_sum);
Expand Down Expand Up @@ -811,10 +788,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::<RetryableHttpClient>()
.as_ref()
.map_err(|err| format!("singlepart_uploader/client: {err:?}"))?
.put(&presigned_url)
.header("Content-Length", total_size)
Expand Down
Loading
Loading