specify I/O timeout

We need to be a bit conservative here due to buffer bloat - in the
upload case on a slow link, it can appear that no I/O is happening
when a buffer is draining.
This commit is contained in:
Damien Elmes 2020-08-26 18:30:51 +10:00
parent 7f15d06c58
commit 06e00be985
3 changed files with 27 additions and 5 deletions

View File

@ -8,7 +8,7 @@ use crate::media::files::{
add_file_from_ankiweb, data_for_file, mtime_as_i64, normalize_filename, AddedFile, add_file_from_ankiweb, data_for_file, mtime_as_i64, normalize_filename, AddedFile,
}; };
use crate::media::MediaManager; use crate::media::MediaManager;
use crate::version; use crate::{sync::Timeouts, version};
use bytes::Bytes; use bytes::Bytes;
use reqwest::{multipart, Client, Response}; use reqwest::{multipart, Client, Response};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -155,9 +155,11 @@ where
host_number: u32, host_number: u32,
log: Logger, log: Logger,
) -> MediaSyncer<'_, P> { ) -> MediaSyncer<'_, P> {
let timeouts = Timeouts::new();
let client = Client::builder() let client = Client::builder()
.connect_timeout(Duration::from_secs(30)) .connect_timeout(Duration::from_secs(timeouts.connect_secs))
.timeout(Duration::from_secs(60)) .timeout(Duration::from_secs(timeouts.request_secs))
.io_timeout(Duration::from_secs(timeouts.io_secs))
.build() .build()
.unwrap(); .unwrap();
let endpoint = media_sync_endpoint(host_number); let endpoint = media_sync_endpoint(host_number);

View File

@ -72,14 +72,33 @@ struct SanityCheckIn {
full: bool, full: bool,
} }
pub struct Timeouts {
pub connect_secs: u64,
pub request_secs: u64,
pub io_secs: u64,
}
impl Timeouts {
pub fn new() -> Self {
Timeouts {
connect_secs: 30,
/// This is smaller than the I/O limit because it is just a
/// default - some longer-running requests override it.
request_secs: 60,
io_secs: 300,
}
}
}
#[derive(Serialize)] #[derive(Serialize)]
struct Empty {} struct Empty {}
impl HTTPSyncClient { impl HTTPSyncClient {
pub fn new(hkey: Option<String>, host_number: u32) -> HTTPSyncClient { pub fn new(hkey: Option<String>, host_number: u32) -> HTTPSyncClient {
let timeouts = Timeouts::new();
let client = Client::builder() let client = Client::builder()
.connect_timeout(Duration::from_secs(30)) .connect_timeout(Duration::from_secs(timeouts.connect_secs))
.timeout(Duration::from_secs(60)) .timeout(Duration::from_secs(timeouts.request_secs))
.io_timeout(Duration::from_secs(timeouts.io_secs))
.build() .build()
.unwrap(); .unwrap();
let skey = guid(); let skey = guid();

View File

@ -21,6 +21,7 @@ use flate2::write::GzEncoder;
use flate2::Compression; use flate2::Compression;
use futures::StreamExt; use futures::StreamExt;
use http_client::HTTPSyncClient; use http_client::HTTPSyncClient;
pub use http_client::Timeouts;
use itertools::Itertools; use itertools::Itertools;
use reqwest::{multipart, Client, Response}; use reqwest::{multipart, Client, Response};
use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};