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
12 changes: 11 additions & 1 deletion packages/app-lib/src/api/instance/shared/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ pub(super) async fn send_bytes_request_to_url(
"Sending shared instances API request"
);

let mut request = shared_instances_client(url)
let mut request = shared_instances_upload_client(url)
.request(method.clone(), url)
.bearer_auth(credentials.session)
.header(reqwest::header::CONTENT_TYPE, "application/octet-stream")
Expand Down Expand Up @@ -1063,3 +1063,13 @@ pub(super) fn shared_instances_client(
&INSECURE_REQWEST_CLIENT
}
}

pub(super) fn shared_instances_upload_client(
base_url: &str,
) -> &'static reqwest::Client {
if base_url.starts_with("https://") {
&NO_TIMEOUT_REQWEST_CLIENT
} else {
&INSECURE_NO_TIMEOUT_REQWEST_CLIENT
}
}
5 changes: 4 additions & 1 deletion packages/app-lib/src/api/instance/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use crate::state::{
ContentSourceKind, EditInstance, ModLoader, ModrinthCredentials,
ProjectType, SharedInstanceRole, State,
};
use crate::util::fetch::{INSECURE_REQWEST_CLIENT, REQWEST_CLIENT};
use crate::util::fetch::{
INSECURE_NO_TIMEOUT_REQWEST_CLIENT, INSECURE_REQWEST_CLIENT,
NO_TIMEOUT_REQWEST_CLIENT, REQWEST_CLIENT,
};
use chrono::{DateTime, Utc};
use reqwest::{Method, StatusCode};
use serde::de::DeserializeOwned;
Expand Down
30 changes: 24 additions & 6 deletions packages/app-lib/src/util/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::num::NonZeroU32;
use std::path::Path;
use std::pin::Pin;
use std::sync::{Arc, LazyLock};
use std::time::{self, Duration, Instant, SystemTime};
use std::time::{Duration, Instant, SystemTime};
use tokio::sync::Semaphore;
use tokio::{fs::File, io::AsyncReadExt, io::AsyncWriteExt};
use tracing::{debug, info};
Expand Down Expand Up @@ -330,28 +330,46 @@ fn duration_seconds_ceil(duration: Duration) -> u64 {
.saturating_add(u64::from(duration.subsec_nanos() > 0))
}

fn reqwest_client_builder_with_timeout() -> reqwest::ClientBuilder {
reqwest_client_builder().read_timeout(Duration::from_secs(30))
}

fn reqwest_client_builder() -> reqwest::ClientBuilder {
reqwest::Client::builder()
.connect_timeout(time::Duration::from_secs(15))
.read_timeout(time::Duration::from_secs(30))
.tcp_keepalive(Some(time::Duration::from_secs(10)))
.connect_timeout(Duration::from_secs(15))
.tcp_keepalive(Some(Duration::from_secs(10)))
.user_agent(crate::launcher_user_agent())
}

pub static INSECURE_REQWEST_CLIENT: LazyLock<reqwest::Client> =
LazyLock::new(|| {
reqwest_client_builder()
reqwest_client_builder_with_timeout()
.build()
.expect("client configuration should be valid")
});

pub static REQWEST_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
reqwest_client_builder()
reqwest_client_builder_with_timeout()
.https_only(true)
.build()
.expect("client configuration should be valid")
});

pub static INSECURE_NO_TIMEOUT_REQWEST_CLIENT: LazyLock<reqwest::Client> =
LazyLock::new(|| {
reqwest_client_builder()
.build()
.expect("client configuration should be valid")
});

pub static NO_TIMEOUT_REQWEST_CLIENT: LazyLock<reqwest::Client> =
LazyLock::new(|| {
reqwest_client_builder()
.https_only(true)
.build()
.expect("client configuration should be valid")
});

const FETCH_ATTEMPTS: usize = 2;

pub type FetchProgressFn<'a> = dyn FnMut(
Expand Down
Loading